Rails 3 Link_to :remote is not triggering RJS
I'm working to setup an AJAX action in rails 3 with the following code. The AJAX part of the code seems to work, but it does not request the开发者_开发百科 correct file and my respond_to serves it the regular HTML.
The routing information:
resources :zones do
resources :records
end
controller:
def new
@zone = Zone.new
respond_to do |format|
format.html
format.js
end
end
Link in view (haml):
= link_to 'Add a zone →', new_zone_path, :remote=>true
Generated HTML from link_to (also notice the failed rendering of the html entity...but thats another issue):
<a href="/zones/new" data-remote="true">Add a zone &#8594;</a>
For kicks, a directory listing of the view/zones. I'm not sure I am doing this quite right, so I have both new.js.rjs and new.rjs. They both have the same content, but are never picked up by the action.
| `~zones/
| |-_form.html.haml
| |-_record.html.haml
| |-edit.html.haml
| |-index.html.haml
| |-new.html.haml
| |-new.js.rjs
| |-new.rjs
| `-show.html.haml
Lastly, the server log from when I click the link:
Started GET "/zones/new" for 127.0.0.1 at Wed Dec 29 00:04:03 -0700 2010
Processing by ZonesController#new as */*
User Load (0.4ms) SELECT "users".* FROM "users" WHERE ("users"."id" = 1) LIMIT 1
Rendered zones/_form.html.haml (22.1ms)
Rendered zones/new.html.haml within layouts/application (34.9ms)
Completed 200 OK in 80ms (Views: 42.0ms | ActiveRecord: 0.4ms)
As you can see, it is rendering the .html file for the request. Now, for testing, I hit the page http://localhost:3000/zones/new.js directly. And it serves up new.js.rjs. Also, the javascript remote call is working. Firebug shows the request and response, but its requesting the wrong page.
Also for testing I did this:
= link_to "Add a zone", '/zones/new.js', :remote=>true
Which works fine (rjs is downloaded and executed and works correctly) for the javascript but it doesn't have the nice failover for javascript-disabled systems.
For what it is worth I am using jquery.
I feel like I am missing something in the routing or the link syntax but all the examples I can find online and in the documentation seem to show exactly what I am doing. Whats the catch?
Thanks.
You need to explicitly tell Rails that you want the js
format:
= link_to 'Add a zone →', new_zone_path(:format => :js), :remote=>true
As a way of explanation: You have to specify the .js extension because Rails doesn't discriminate. In many cases, you might want to fetch html or json with Ajax--not just javascript. Rails will let you fetch whatever content in whatever format, which is why you have to specify it.
This problem also can be solved with this syntax (includes data-type
declaration)
= link_to 'Add a zone →', new_zone_path, remote: true, "data-type" => "script"
In case you just have .js.erb
file , no .html
file to render, it's not necessary to assign the data-type
of request (Rails automatically detect that JS only way to response)
The main point here is you have to make sure that your request is considered to be executed as JS. It means your request in console should look like this:
Started ...
Processing by ABCController#method as JS
精彩评论