开发者

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开发者_StackOverflow中文版", "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.


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.


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"
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜