ciaodarwin
ciaodarwin

Reputation: 481

Ruby on Rails - parameter gives nil result back

parameter

"day" => "2013-11-21"

controller

date = params[:day] # gives me a nil value back
@event = Event.new(date: date, id: date.id)

view

...
<% f.hidden_field :id, :value => :day %>
...

Here are the parameters:

 {"utf8"=>"✓",
 "authenticity_token"=>"lxKzvpGx8nmutI8X8sdZGNaKZ8w1kJEdF/B8ixtqpqA=",
 "event"=>{"title"=>"",
 "description"=>"",
 "day"=>"2013-11-21",
 "start(1i)"=>"2013",
 "start(2i)"=>"11",
 "start(3i)"=>"22",
 "start(4i)"=>"08",
 "start(5i)"=>"00",
 "end(1i)"=>"2013",
 "end(2i)"=>"11",
 "end(3i)"=>"22",
 "end(4i)"=>"08",
 "end(5i)"=>"00"},
 "commit"=>"Create Event"}

why gives it to me a nil value back?

let me know if you need something more

Upvotes: 0

Views: 67

Answers (2)

Marek Lipka
Marek Lipka

Reputation: 51151

I see at least three problems.

  1. Your param is accessible through params[:event][:day], as you should see in the parameters list you posted.
  2. The hidden field you posted doesn't set params[:event][:day] parameter. Actually, it does nothing as it isn't even being rendered (because of <% usage, instead of <%=.
  3. You try to misuse this parameter, calling id on it (in @event = Event.new(date: date, id: date.id) line). Even if you refer to that parameter properly, it will raise an error.

Upvotes: 1

Mandeep
Mandeep

Reputation: 9173

You should use params[:event][:day] instead of params[:day]

Upvotes: 2

Related Questions