jsav
jsav

Reputation: 109

Rails: Post parameter is null

From a javascript file I am having post request with parameters:

  Parameters: {"interview"=>{"participants"=>"1,2,3", "start_time"=>"2020-05-28T09:32:00.000Z", "end_time"=>"2020-05-28T10:32:00.000Z"}}

And i am accessing this in controller's create as:

puts("create entry")
@start_time =  params[:start_time]
@end_time =  params[:end_time]
p(@start_time)
p(@end_time)
participants = params[:participants].try(:split, ",")
p(participants) 

But when check the values of p and puts are such as:

create entry
nil
nil
nil

I could not understand what is the reason behind this.

Upvotes: 0

Views: 295

Answers (1)

Sebastián Palma
Sebastián Palma

Reputation: 33420

They're all "under" the interview key, so it should be:

@start_time = params[:interview][:start_time]
@end_time = params[:interview][:end_time]
participants = params[:interview][:participants].try(:split, ',')

Better in this case using dig:

participants = params.dig(:interview, :participants).try(:split, ',')
...

Or storing params[:interview] and giving a "default" value if nil:

interview = params[:interview] || {}

Upvotes: 1

Related Questions