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

Community
  • 1
  • 1
  • 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`. – user229044 Aug 01 '11 at 16:21
  • 3.0.7. Can't work out the link_to options for this version so any help appreciated thanks. – Tom Cartwright Aug 02 '11 at 18:12

4 Answers4

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.

apneadiving
  • 114,565
  • 26
  • 219
  • 213
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 %>
Pat McGee
  • 379
  • 3
  • 9
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.

lucapette
  • 20,564
  • 6
  • 65
  • 59
  • 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? – Tom Cartwright Aug 02 '11 at 15:09
  • Sorry, I replied very quickly. I edited my reply and tried it. It should work now :) – lucapette Aug 02 '11 at 19:01
  • 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. – Tom Cartwright Aug 02 '11 at 19:48
-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' ) %>" );
Nick Res
  • 2,154
  • 5
  • 30
  • 48
  • 1
    downvotes should require an explanation... Otherwise I'm left with not know what is wrong with my suggestion. – Nick Res Jul 01 '14 at 13:00