# Text\_field\_tag - different parameter name

**URL:** <https://www.ruby-forum.com/t/text-field-tag-different-parameter-name/129712>\
**Category:** Rails\
**Created:** [February 20, 2008, 10:01pm UTC](https://www.ruby-forum.com/t/text-field-tag-different-parameter-name/129712 "2008-02-20T22:01:04Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![edgy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ac91a4/32.png) [@edgy](https://www.ruby-forum.com/u/edgy)\
**Post date:** [February 20, 2008, 10:01pm UTC](https://www.ruby-forum.com/t/text-field-tag-different-parameter-name/129712/1 "2008-02-20T22:01:04Z")

</div>

Hi, I have one problem with validation

I am creating e-shop and I have products. This view look like this:

\<% for product in @category.products %\>

```
  <%= product.title %><br />
  Price: <br />
  <div style="color:red; font-size:large; font-weight:bold; margin-

```

bottom:5px"\>\<%= number\_with\_delimiter(product.price, " ")%\> Kè\</  
div\>  
\<%= link\_to “Details”, {:action =\> “product”, :id =\>  
product.name\_seo} %\>

```
    <% form_tag :action => "add_to_cart" do %>
      <p>
        <%= text_field_tag :quantity, "1", :size => 1 %>
        <%= submit_tag "Buy" %>
        </p>
    <% end %>

```

\<% end %\>

In controller I need get value of text\_field\_tag (@value =  
params[:quantity] )  
Problem is with validation : ID “quantity” already defined

I need something like this \<%= text\_field\_tag “quantity\_”+product.id,  
“1”, :size =\> 1 %\> .

This I get different name of text\_field, but I don’t know, How can I  
get params to controller

Thank you

---

<div class="post-metadata">

**Author:** ![edgy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ac91a4/32.png) [@edgy](https://www.ruby-forum.com/u/edgy)\
**Post date:** [February 21, 2008, 2:37pm UTC](https://www.ruby-forum.com/t/text-field-tag-different-parameter-name/129712/2 "2008-02-21T14:37:37Z")

</div>

I’ll go out on a limb and guess that you might have an object like  
‘LineItem’ in your domain where:

class LineItem  
belongs\_to :order  
has\_one :product  
…  
end

LineItem has quantity and maybe discount\_rate as attributes. With  
that in mind:

\<% @category.products.each do |product| %\>

```
  <%= product.title %><br />
  Price: <br />
  <div style="color:red; font-size:large; font-weight:bold;

```

margin-  
bottom:5px"\>\<%= number\_with\_delimiter(product.price, " ")%\> Kè\</  
div\>  
\<%= link\_to “Details”, {:action =\> “product”, :id =\>  
product.name\_seo} %\>

```
    <% form_for :line_item, LineItem.new(:product=>product), :url

```

=\> url\_for(:controller=\>:order, :action=\>:add\_to\_cart) do |f| %\>  
\<%= f.hidden\_field :product\_id %\>

\<%= f.text\_field :quantity, “1”, :size =\> 1 %\>  
\<%= submit\_tag “Buy” %\>

  
\<% end %\> 
\<% end %\>

There are other improvements that you could make here, but the core of  
what’s going on is that you’re wrapping the info going back to the  
add\_to\_cart action in a line\_item hash. The main change was using  
form\_for rather than form\_tag so that you could wrap the product\_id  
(you’ll need that, right?) along with the quantity in a line\_item hash  
(controller’s perspective). To create an instance you’d:

item = LineItem.new( params[:line\_item])

in your controller and it’d be loaded with both the quantity AND  
product.

HTH,  
AndyV

HTH,  
AndyV
