Why the controller does not “see” the value entered on the form

I've got this on a Rails 3.0.3 view:

<%= form_tag line_items_path(:product_id => @product), :remote => true  do %>
  <%= number_field_tag (:amount, 1, { :size => 3, :min => 1}) %>
  <%= submit_tag t('button.add_to_cart'), :name => nil %>
<% end %>

The view renders quite okay. In the line_items_controller's create method I try to access the number field:

@cart = current_cart
product = Product.find(params[:product_id])
@line_item = @cart.add_product(product.id, params[:amount])

That did not work, so I tried to examine the number field by printing it to the console:

p params[:amount]

That printed "nil" no matter what I've entered on the form field. I also printed the whole params hash with

p params

and got

{"product_id"=>"1", "action"=>"create", "controller"=>"line_items", "locale"=>"fi"}

i.e. the amount field is not there, which explains the "nil", but I'm a bit (ok, a lot) confused here, as I've read the form_tag documentation, and gathered that the controller should see the field contents via params[:amount], yet it doesn't.

Please help me. What am I doing wrong?



I feel so stupid right now... The form works now, when it is compeletely inside a <td> tag, like this:

   <td colspan="2">
      <%= form_tag line_items_path(:product_id => @product), :remote => true  do  %>
          <%= number_field_tag(:amount, 1, :size => 3, :min => 1) %>
          <%= submit_tag t('button.add_to_cart'), :name => nil %>
      <% end %>

I had the form_tag, and the corresponding end before the <tr> tag, and after the </tr> tag, respectively.

Thank you for your time!

Need Your Help

Benchmarking SQL Server Execution Plans For Automated Testing?

sql sql-server performance sql-server-2008 testing

I'm using SQL Server 2008. I noticed that when I run a sproc and include an execution plan, SQL Server will include an alert if I'm "missing" an index and will recommend one for me to add in order to

Observable collection removing item only once, when tried again doesnt work - Windows phone 8 - C#

c# visual-studio windows-phone-8 observablecollection

I am writing my first application for WP8 platform in C#. I implemented three datatypes namely locationModel which has locationGroups. Each locationGroup has a ObservableCollection of type location...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.