I have no idea why this is not working, I'm learning rails and I'm following a book it says to do it like this:
<%= form_for([@article,@article.comments.new ], :remote=>true, :html => {:style=>'display: none;' }) do |f|%>
<div class="field">
<%= f.label :name %>
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :email %>
<%= f.text_field :email %>
</div>
<div class="field">
<%= f.label :body %>
<%= f.text_area :body %>
</div>
<%= f.submit %>
<% end %>
However it does not work, and when I check the request in firebug the url doesnt end in .js, do you have any ideas why?
Check the controller: does it have a respond_to block?
If you generated your app with rails generate scaffold, it will have something like
If it does have it, just take the whole block out, from respond_to to end; or alternatively replace the whole block with the single line
or alternatively edit the block by adding the extra line shown here
Either should work in your case.
This error occurs when you have not add jquery_ujs file. You just included the jquery file.
So you need to add both files manually in you view or require them in application.js or any other file which you are using for specific layout.
Depending upon your scenario, you can follow 1st or 2nd solution.
1st solution:
2nd solution:
Require both jquery and jquery_ujs in app/assets/application.js
Include application.js file in your specific layout file.
Note:
Also add csrf tag in layout file for both solutions.
I'm asuming that you have below code in your Gemfile and already installed this Gem.
When you check the request made in firebug, just because the url did not end with .js, it does not mean that it was not called from javascript. To verify that you should check the request header to see what the Accept parameter is. If it says "application/javascript" then it's all good.
Secondly, a very common problem when starting to try out the :remote => true is that the required javascript libraries are not included in your code. So my guess is that the following code is missing from your layout:
If that is the case, just include it inside the
<head>
tag of your layout.Change:
for
I suggest you reading the API on the related section : http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html
Most likely what happens is that you are missing the
rails.js
file, which handles that for you, whether you are using prototype or jquery.If you are using jQuery, the easiest way to get all the needed files is using the
jquery-rails
gem. This will add a generator to install jquery and the needed rails.js.type something like inside your rails application root:
And then, inside your
application.html.erb
add the lineor explicitly (do not forget to include your jquery separately):
[EDIT: for Rails 3.1 or greater using the asset pipeline]
Use the jquery-rails gem (as mentioned above) and add the following lines to the
app/assets/javascripts/application.js
(if they are not there already) :Hope this helps!
Check your
/app/assets/javascript/application.js
fileIt should be included below files in there.
jquery_ujs
is responsible for workingremote=> true
feature.Happy Coding!!