3

Using Rails 3 and jquery, I'm trying to setup a simple 3 tab navigation for my post#show page, rendering _post _comments and _related partials into the view when clicked.

These partials don't have their own controller actions, currently.

What would be the cleanest Rails way to handle these ajax calls?

I tried this but it's not working:

Post controller:

def tab  
  respond_to do |format|
    format.js
  end
end

Post show view:

<%= link_to "Comments", {:action => 'tab'}, :remote => true %>

/views/posts/tab.js.erb

$('#postshow').html("<%= escape_javascript(render('comments')) %>");

However, if I do this, when I try to open my Track Show page i get hungup with this error "No route matches {:action=>"tab", :controller=>"posts"}"

i thought i defined an action for tab that should render the tab.js.erb... But more importantly this seems a little clunky to just switch around between three partials.

Any help is much appreciated. Thanks.

2 Answers 2

1

Your action lies in PostsController, so you should write:

<%= link_to "Comments", {:controller => "posts", :action => "tab"}, :remote => true %>
Sign up to request clarification or add additional context in comments.

2 Comments

My bad, I actually just copied that code wrong, just fixed it in my post. I am using the posts controller and the error says No route matches {:controller=>"posts", :action=>"tab"}. I did try putting :controller => "posts" into the link_to block and it didn't help. I'm sure I'm doing plenty wrong here but why would it say no route matches when I definitely do have a tab route in posts controller? that confuses me.
Then you have some problem in routes.rb. Can you post the line from this file here?
0

Ok, got it. Just needed to add the resource to routes.rb... thought that part just magically happened.

put this in routes.rb

resources :posts do
  ...
  member do
    get 'tab'
  end
end

and now it finds the route and renders the javascript.

having said that, if anyone reads this post and can point out a way cleaner or simpler way to set up ajax links for rendered partials, that would be super sweet.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.