Adnan
Adnan

Reputation: 26350

Read from params[] in Rails

I use:

<%= select( "payment", "id", { "Visa" => "1", "Mastercard" => "2"}) %>

and I get this in HTML

<select id="payment_id" name="payment[id]"><option value="2">Mastercard</option>
<option value="1">Visa</option></select>

now how can I read the payment[id] with params[], if I use params[payment[id]] I get an error.

Upvotes: 2

Views: 3030

Answers (3)

user851096
user851096

Reputation: 11

params[:payment][:id] and params[:payment][:id] is the same on surface , but in fact , in ruby ,you can't access the payment's id with params[:payment][:id]. because rails has changed the usage of it.

Upvotes: 0

shingara
shingara

Reputation: 46914

I suppose is better to have

params[:payment][:id]

Params is a hash and can be contain some other hash.

Upvotes: 3

Joseph Rodriguez
Joseph Rodriguez

Reputation: 1143

This one had me stumbled for a couple of hours when I first started with ruby/rails. In your controller and views you can access the payment's id with either:

params[:payment][:id]

or...

params['payment']['id']

Many people prefer using symbols (:symbol) over strings because of memory usage, no matter how small the gains...

Upvotes: 3

Related Questions