1

I am trying to setup a navigation bar on my home page that loads a partial into a div element when the user clicks on the links. I have followed the steps in this post and modified them as I thought I needed: Rails 3 - link_to to call partial using jquery ajax

My code:

views/pages/home.html.erb:

<%= link_to "Files", :action => 'load_upload_partial', :remote => true %>

. . .

<div id="main_frame"></div>

pages_controller:

def load_upload_partial  
    respond_to do | format |  
      format.js {render :layout => false}  
    end
end

/views/uploads/load_upload_partial.js.erb:

$("#main_frame").html( "<%= escape_javascript( render( :partial => "/upload/upload_form" ) %>" );

The partial in this example is just a form. When I click on the link I get a blank page with this is the address bar: http://localhost:3000/load_upload_partial?remote=true

This makes me think that link is not triggering an ajax GET request (if that's the correct terminology). If that is the case is there anything I need to be adding to my application.js file? I followed the railscast #136 on rails and jquery but couldn't figure out which bits of the application.js code applied to my case. Any thoughts are much appreciated. Thanks. Tom

2
  • 1
    What version of Rails are you using? I can't say why, but the problem is Rails is including :remote => true in the url_options argument to link_to. Commented Aug 1, 2011 at 16:21
  • 3.0.7. Can't work out the link_to options for this version so any help appreciated thanks. Commented Aug 2, 2011 at 18:12

4 Answers 4

4

I now tend to avoid using RJS like you're intending to. I prefer creating js templates with tools like handlebars.

I only use ajax to get raw data and then recreate the partial client side. It's much better for server load and data transfer.

Sign up to request clarification or add additional context in comments.

Comments

0

I think you have to change your link_to to:

<%= link_to "Files", {:action => 'load_upload_partial' }, remote => true %>

The problem is related to the method signature of link_to.

3 Comments

Thanks for the reply. I changed the link_to line to include { :remote => true } but it caused an exception on loading the page: /app/views/pages/home.html.erb:8: syntax error, unexpected ')', expecting tASSOC ...partial', { :remote => true } );@output_buffer.safe_concat(' I am using Rails 3.0.7 if that changes anything?
Sorry, I replied very quickly. I edited my reply and tried it. It should work now :)
Thanks for the reply. It is still not working - the link is 'dead' but I am sending text/javascript now, which is progress. I am receiving a 500 internal server error so I guess that my js.erb file must be incorrect. Will have a proper root around on that one. Thanks for help.
0

ran into this same problem - the hint was in the malformed html produced - take a look at the value of the href attribute of the tag produced by your code. I bet it looks funky. Here's the correct code:

    <%= link_to "Files", your_path, action: 'load_upload_partial', remote: true %>

Comments

-1

have you tried using .load() instead of .html()?

Also, try using this line instead:

$("#main_frame").html( "<%= escape_javascript( render( :partial => '/upload/upload_form' ) %>" );

1 Comment

downvotes should require an explanation... Otherwise I'm left with not know what is wrong with my suggestion.

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.