What is the right way to access this param value?

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:inconfirm’

What is the correct way to access my parameter values?

Thanks, - Dave

On Jan 23, 10:02 pm, laredotornado [email protected] 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”

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 :wink:

-Rob

Rob B. http://agileconsultingllc.com
[email protected]

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs