Hinchy
Hinchy

Reputation: 683

Can't access rails post data

I've spent the last couple of hours slowly losing my mind while trying to figure something out. I'm submitting a form in rails, everything works fine until I try to access the params. In the console I can see the following:

Parameters: {"authenticity_token"=>"3mdEW2lHhkzpZbDsJCu8ZEV/wbq2YB/ztNR0RLTMZDs=", "utf8"=>"✓", "project"=>{"name"=>"woeij", "client"=>"iwej", "description"=>"oiejdoiew woeij"}, "id"=>"13"}

As you can see I'm sending name, client, description and id. I can access ID fine with something like:

@id = params[:id]

However, when I try to access name, client, or description in the same way they're all empty. If I do:

@project = params[:project]

I get:

namewoeijclientiwejdescriptionoiejdoiew woeij 

Would someone mind explaining what I'm doing wrong? And why I can't just get "woeij" when I do:

@name = params[:name]

Sorry for the stupid question, big thanks as always.

Upvotes: 0

Views: 1815

Answers (2)

Paige Ruten
Paige Ruten

Reputation: 176743

You have a hash inside a hash. After you do:

@project = params[:project]

You have all your project parameters inside that hash. You can select them like this:

@project[:name]   #=> "woeij"
@project[:client] #=> "iwej"

You can also select them in one go like this:

params[:project][:description] #=> "oiejdoiew woeij"

Upvotes: 3

apneadiving
apneadiving

Reputation: 115541

Attributes are nested, do

params[:project][:name]

to retrieve name.

A really cool tool in the rails console is the y: if you type y params they'll be presented really nicely.

Upvotes: 8

Related Questions