Topic: form_remote_tag in Rails 3.0?

Hi  I am new to Rails. Trying to run this tutorial code in Rails 3.0, got this error. Please help. Thanks!

NoMethodError in Store#index

Showing /d/test/depot/app/views/store/index.html.erb where line #11 raised:

undefined method `form_remote_tag' for #<#<Class:0x7f2ae8c6edb8>:0x7f2ae8c6d008>
Extracted source (around line #11):

8:       <%= raw product.description %>
9:       <div class="price-line">
10:         <span class="price"><%= number_to_currency product.price %></span>
11:         <% form_remote_tag :url => {:action => 'add_to_cart', :id => product} do %>
12:             <%= submit_tag "Add to Cart" %>
13:         <% end %>
14:       </div>

Re: form_remote_tag in Rails 3.0?

I believe 'form_remote_tag' was deprecated in Rails 3.0. Use 'form_for @post, :remote => true'. … notes.html

Re: form_remote_tag in Rails 3.0?


I did as you suggested:

        <% form_tag :url => {:action => 'add_to_cart', :id => product}, :remote => true do %>
            <%= submit_tag "Add to Cart" %>
        <% end %>

and Rails generated some strange code which does not call the add_to_cart action.

<form method="post" action="/store/index?url[action]=add_to_cart&amp;url[id]=5&amp;remote=true" accept-charset="UTF-8">
  <div style="margin: 0pt; padding: 0pt; display: inline;">
    <input type="hidden" value="✓" name="utf8"><input type="hidden" value="ya9Aw7pX6K77zxoDsSAFxpxSUxIqSZ3wJLx0jheHEiY=" name="authenticity_token">
  <input type="submit" value="Add to Cart" name="commit">

Re: form_remote_tag in Rails 3.0?

Dear all

I finally got it to work, here the solution:

based on button_to:

        <%= button_to "Add to Cart", {:action => :add_to_cart, :id => product}, :method => 'post', :remote => true %>

based on form_for:

        <% form_for product, :url =>  {:action => 'add_to_cart', :id =>}, :remote => true do %>
            <%= submit_tag "Add to Cart" %>
        <% end %>

I agree button_to is better. I used form because the book sample said so:
(Agile Web Development With Ruby On Rails.pdf 3rd Edition)

On the index page, we’re using button_to to create the link to the add_to_cart
action. Underneath the covers, button_to generates an HTML form. The following
<%= button_to "Add to Cart" , :action => :add_to_cart, :id => product %>
generates HTML that looks something like this:
<form method="post" action="/store/add_to_cart/1" class="button-to">
<input type="submit" value="Add to Cart" />
This is a standard HTML form, so a POST request will be generated when
the user clicks the submit button. We want to change this to send an Ajax
request instead. To do this, we’ll have to code the form explicitly, using a Rails
helper called form_remote_tag. The form_..._tag parts of the name tell you it’s
generating an HTML form, and the remote part tells you it will use Ajax to
create a remote procedure call to your application. So, edit index.html.erb in the
app/views/store directory, replacing the button_to call with something like this:
Download depot_l/app/views/store/index.html.erb
<% form_remote_tag :url => { :action => 'add_to_cart', :id => product } do %>
<%= submit_tag "Add to Cart" %>
<% end %>

Thanks everybody for sharing and replying!