1

This is the code I have in rails 3.1

<%= link_to 'All',:action => "bycategory", :id => 'All', :remote => true %>

I have defined a js.erb. This code works perfectly fine i.e invokes the JS when put in table with a certain CSS. When I just change the table id or put in a unordered list or anywhere else, it gives me template error as it looks for the html.erb. Just beats me :(.

one thing I found is that in the Params in the first case apart from the id and the remote , it also sends timestamp In the second case time stamp is missing. Not sure why this is happening Any help will be appreciated, have spent too much time debugging this

user1069240
  • 23
  • 1
  • 5
  • I'm not sure I fully understand the question. The culprit could be that, since the remote action is set on the browser at run time, when the id is not found (or if javascript is not enable on the browser) the link points nowhere. Debug the action on the browser using firebug. – baol Nov 28 '11 at 12:10
  • The inconsistency is what baffles me, one more behavior was if there are were two requests one with remote=> true and another without that in two different rows of the table only one works. – user1069240 Nov 28 '11 at 12:48
  • 1
    Adding `:remote => true` only adds the html attribute `data-remote="true"`, which then should be handled by `jquery-rails`. Is the `data-remote="true"` properly generated when you put the `:remote => true`? – Dominic Goulet Nov 28 '11 at 14:32
  • This is how it looks All i have a form – user1069240 Nov 29 '11 at 07:06
  • Hi Domnic, looks like the issue is the data-remote="true" is not rendered. A bug in rails 3.1 ? – user1069240 Nov 29 '11 at 08:37
  • It is more likely a bug in your setup. Perhaps you are not including jquery? – baol Nov 30 '11 at 15:28

2 Answers2

2

I had the same problem with Rails 3.1.3 and I cured it with rake assets:precompile,but ensure you have in application.js "//= require jquery_ujs" instead of "//= require jquery",this was the main problem and I spent at least 12 hours before I decided do it this way!Terrible!

Andrey
  • 191
  • 2
  • 6
  • Thanks! Your answer led me to the right track. I had the same problems AND actually required jquery_ujs, but I was also caching javascript libraries into one file: `javascript_include_tag *js_libraries, :cache => 'cached_js'`. So I tried to remove :cache parameter and it works :). – Lukas Stejskal Jul 19 '12 at 08:08
1

You might want to put the :action => "bycategory" and :id => 'All' in a hash as

<%= link_to 'All', { :action => "bycategory", :id => 'All' }, :remote => true %>

This will generate the proper html attribute i.e. data-remote="true" otherwise it will simply treat everything following as an http request attribute. Your code will generate -

<a href="/bycategory?id=All&amp;remote=true">All</a>

whereas using the hash will generate something similar to

<a data-remote="true" href="/bycategory/All">All</a>
maverick13
  • 56
  • 4