# What is the right way to access this param value?

**URL:** <https://www.ruby-forum.com/t/what-is-the-right-way-to-access-this-param-value/127014>\
**Category:** Ruby\
**Created:** [January 24, 2008, 7:05am UTC](https://www.ruby-forum.com/t/what-is-the-right-way-to-access-this-param-value/127014 "2008-01-24T07:05:33Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![laredotornado](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9dc877/32.png) [@laredotornado](https://www.ruby-forum.com/u/laredotornado)\
**Post date:** [January 24, 2008, 7:05am UTC](https://www.ruby-forum.com/t/what-is-the-right-way-to-access-this-param-value/127014/1 "2008-01-24T07:05:33Z")

</div>

Hi,

I have several fields on my page, distinguished by numbers in the id:

\<% for i in 1 … 5 %\>

  
  
  
  
  
  
  
  
  
  

| \<%=  
text\_field\_tag(“prescription\_number” + i.to\_s, “”) %\> | \<%= text\_field\_tag(“description” +  
i.to\_s, “”) %\> |
| Prescription  
Number | Description |

  
  
\<% end %\>

However, when I try and access the values in my controller,

```
            i = 0
            while params[:prescription_number + i.to_s] != nil and

```

params[:description + i.to\_s] != nil # line 7  
session[:prescription\_number + i.to\_s] =  
params[:prescription\_number + i.to\_s]  
session[:description + i.to\_s] =  
params[:description + i.to\_s]  
i += 1  
end

I get the error

NoMethodError in OrderController#confirm  
undefined method `+' for :prescription_number:Symbol RAILS_ROOT: ./script/../config/.. Application Trace | Framework Trace | Full Trace /usr/local/apache2/htdocs/easyrx/app/controllers/order_controller.rb: 7:in`confirm’

What is the correct way to access my parameter values?

Thanks, - Dave

---

<div class="post-metadata">

**Author:** ![laredotornado](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9dc877/32.png) [@laredotornado](https://www.ruby-forum.com/u/laredotornado)\
**Post date:** [January 24, 2008, 7:35am UTC](https://www.ruby-forum.com/t/what-is-the-right-way-to-access-this-param-value/127014/2 "2008-01-24T07:35:56Z")

</div>

On Jan 23, 10:02 pm, laredotornado [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> <td><%= text_field_tag("description" +
> 
> end
> 
> ```
> 
> What is the correct way to access my parameter values?
> 
> Thanks, - Dave

You’re trying to concatenate a Symbol with a string. This IRB session  
should help explain the difference:

irb(main):019:0\> “Hello”.class  
=\> String  
irb(main):020:0\> “Hello”.intern  
=\> :Hello  
irb(main):021:0\> :Hello.class  
=\> Symbol  
irb(main):022:0\> :Hello + " World"  
NoMethodError: undefined method `+’ for :Hello:Symbol  
from (irb):22  
irb(main):023:0\> :Hello.to\_s + " World"  
=\> “Hello World”

---

<div class="post-metadata">

**Author:** ![laredotornado](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9dc877/32.png) [@laredotornado](https://www.ruby-forum.com/u/laredotornado)\
**Post date:** [January 24, 2008, 2:11pm UTC](https://www.ruby-forum.com/t/what-is-the-right-way-to-access-this-param-value/127014/3 "2008-01-24T14:11:13Z")

</div>

On Jan 24, 2008, at 1:34 AM, zetetic wrote:

> > “”) %\>  
> > However, when I try and access the values in my controller,
> 
> =\> Symbol  
> irb(main):022:0\> :Hello + " World"  
> NoMethodError: undefined method `+’ for :Hello:Symbol  
> from (irb):22  
> irb(main):023:0\> :Hello.to\_s + " World"  
> =\> “Hello World”

While that is a valid explanation of the error, the real problem is  
better solved with:

\<% for i in 1 … 5 %\>

| \<%= text\_field\_tag("prescription\_number[]", "") %\> | \<%= text\_field\_tag("description[], "") %\> |
| Prescription Number | Description |

 \<% end %\>

Note the addition of the “[]” at the end of the name. If you need to  
have an id on these, too, then add an :id=\>“my\_id” to the  
text\_field\_tag.

Then you get back the parameter values in an Array and in the  
controller can do something like:

params[:prescription\_number].each\_with\_index do |  
prescription\_number, i|  
break if prescription\_number.blank? || params[:description]  
[i].blank?  
(session[:prescription\_number] ||= [])[i] = prescription\_number  
(session[:description] ||= [])[i] = params[:description][i]  
end

The ‘||= []’ part is to initialize the value of the session hash to be  
an empty Array if it starts nil.

Having said this, I’d recommend that you consider creating a model to  
hold this data and then you just have one object to store in the  
session (if you really have to do that, of course). This model does  
not have to be backed by ActiveRecord. (Note that I’m going out on a  
limb assuming that you’re using Rails even though you’ve asked in the  
Ruby list 😉

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid
