如何在Ruby on Rails中获取参数?

时间:2023-01-14 12:18:23

I want to get the value of material_sort_id

我想获得material_sort_id的值

Started GET "/materials?utf8=%E2%9C%93&material%5Bmaterial_sort_id%5D=1&material%5Bbrand_id%5D=1&commit=%E6%9F%A5%E8%AF%A2" for 127.0.0.1 at 2015-05-28 16:06:34 +0800
Processing by MaterialsController#index as HTML
        Parameters: {"utf8"=>"✓", "material"=>{"material_sort_id"=>"1", "brand_id"=>"1"}, "commit"=>"查询"}

this is my code :

这是我的代码:

puts params[:material_sort_id].present?

I got false. How do I get it?

我弄错了。我怎么得到它?

4 个解决方案

#1


6  

params[:material][:material_sort_id]

You can get it like this

你可以这样得到它

#2


1  

Loop 
{
    //Code   
    puts params[:material][:material_sort_id];
}

#3


1  

According to the logs

根据日志

"material" => {"material_sort_id"=>"1", "brand_id"=>"1"}, "commit"=>"查询"}

Your params[:material_sort_id] is inside the "material" key so you can't access directly. It's something like you have 2 Hash the 1st Hash key contains another Hash

您的参数[:material_sort_id]位于“材料”键内,因此您无法直接访问。这就像你有2哈希第一哈希键包含另一个哈希

e.g { "a" => { "b"=> "1", "c" => "2" } }

To access you can use following.

要访问您可以使用以下。

params[:material][:material_sort_id]

To Check whether its present or not you can use following.

要检查是否存在,您可以使用以下内容。

params[:material][:material_sort_id].present?

#4


0  

It might possible that url is not having material hash itself. In that case you can write

url可能没有材料哈希本身。在那种情况下你可以写

params[:material].try(:material_sort_id)

In above case it will query material_sort_id on material only if material hash is not nil. If it is not nil it will return expected result else return false instead of any error

在上面的情况下,只有当材料散列不是nil时,它才会在材料上查询material_sort_id。如果它不是nil则返回预期结果,否则返回false而不是任何错误

#1


6  

params[:material][:material_sort_id]

You can get it like this

你可以这样得到它

#2


1  

Loop 
{
    //Code   
    puts params[:material][:material_sort_id];
}

#3


1  

According to the logs

根据日志

"material" => {"material_sort_id"=>"1", "brand_id"=>"1"}, "commit"=>"查询"}

Your params[:material_sort_id] is inside the "material" key so you can't access directly. It's something like you have 2 Hash the 1st Hash key contains another Hash

您的参数[:material_sort_id]位于“材料”键内,因此您无法直接访问。这就像你有2哈希第一哈希键包含另一个哈希

e.g { "a" => { "b"=> "1", "c" => "2" } }

To access you can use following.

要访问您可以使用以下。

params[:material][:material_sort_id]

To Check whether its present or not you can use following.

要检查是否存在,您可以使用以下内容。

params[:material][:material_sort_id].present?

#4


0  

It might possible that url is not having material hash itself. In that case you can write

url可能没有材料哈希本身。在那种情况下你可以写

params[:material].try(:material_sort_id)

In above case it will query material_sort_id on material only if material hash is not nil. If it is not nil it will return expected result else return false instead of any error

在上面的情况下,只有当材料散列不是nil时,它才会在材料上查询material_sort_id。如果它不是nil则返回预期结果,否则返回false而不是任何错误