[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

What is the right way to access this param value?

(D. Alvarado)

1/24/2008 6:02:00 AM

Hi,

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

<% for i in 1 .. 5 %>
<tr><td>
<table>
<tr>
<td><%=
text_field_tag("prescription_number" + i.to_s, "") %></td>
<td><%= text_field_tag("description" +
i.to_s, "") %></td>
</tr>
<tr>
<td align="center">Prescription
Number</td>
<td align="center">Description</td>
</tr>
</table>
</td></tr>
<% 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
2 Answers

zetetic

1/24/2008 6:31:00 AM

0

On Jan 23, 10:02 pm, laredotornado <laredotorn...@zipmail.com> wrote:
> Hi,
>
> I have several fields on my page, distinguished by numbers in the id:
>
> <% for i in 1 .. 5 %>
> <tr><td>
> <table>
> <tr>
> <td><%=
> text_field_tag("prescription_number" + i.to_s, "") %></td>
> <td><%= text_field_tag("description" +
> i.to_s, "") %></td>
> </tr>
> <tr>
> <td align="center">Prescription
> Number</td>
> <td align="center">Description</td>
> </tr>
> </table>
> </td></tr>
> <% 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

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"

Rob Biedenharn

1/24/2008 1:10:00 PM

0

On Jan 24, 2008, at 1:34 AM, zetetic wrote:
> On Jan 23, 10:02 pm, laredotornado <laredotorn...@zipmail.com> wrote:
>> Hi,
>>
>> I have several fields on my page, distinguished by numbers in the id:
>>
>> <% for i in 1 .. 5 %>
>> <tr><td>
>> <table>
>> <tr>
>> <td><%= text_field_tag("prescription_number" + i.to_s,
>> "") %></td>
>> <td><%= text_field_tag("description" + i.to_s, "") %></td>
>> </tr>
>> <tr>
>> <td align="center">Prescription Number</td>
>> <td align="center">Description</td>
>> </tr>
>> </table>
>> </td></tr>
>> <% 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
>
> 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"

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

<% for i in 1 .. 5 %>
<tr><td>
<table>
<tr>
<td><%= text_field_tag("prescription_number[]", "") %></td>
<td><%= text_field_tag("description[], "") %></td>
</tr>
<tr>
<td align="center">Prescription Number</td>
<td align="center">Description</td>
</tr>
</table>
</td></tr>
<% 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 Biedenharn http://agileconsult...
Rob@AgileConsultingLLC.com