Rails 4参数哈希在更新操作上无法正常工作

时间:2014-02-25 19:47:34

标签: ruby-on-rails ruby ruby-on-rails-4 ruby-2.0

当我创建搜索时,通过params[:search]在rails 4 params hash中正确的参数正常,但在更新操作中,我的一些参数无法通过params[:search]工作。当我查看更新操作的请求时,我发现了我正在寻找的内容(下面的代码),但是当使用rails params hash时,我只得到4个(后面的代码)。有想法该怎么解决这个吗?

我不是在谈论使用search_params功能。我在谈论调试应用程序时,您可以确保参数在哈希中。如果它们不在params哈希值中,则无论您的代码在search_params

中的代码如何,它们都不会显示
"search"=>
  {"gender"=>"2",
   "height_low"=>"60",
   "height_high"=>"70",
   "weight_low"=>"80",
   "weight_high"=>"110",
   "age_low"=>"18",
   "age_high"=>"33",
   "city"=>"fg",
   "province_state"=>"fg",
   "country"=>"CA"}

"search"=>
  {"gender"=>"2", "city"=>"fg", "province_state"=>"fg", "country"=>"CA"}

形式

<%= form_for @search, url: {action: "update"}, html: { method: "post"} do |f| %>
<%= f.label :gender, 'Gender:' %>
<%= f.select(:gender, attractions, :selected => @search.gender) %>
<br />
<%= f.label :height_low, 'Height between:' %>
    <%= f.select(:height_low, heights)  %>
<%= f.select(:height_high, heights) %>
<br />
<%= f.label :weight_low, 'Weight between:' %>
<%= f.select(:weight_low, (80..300).to_a.map{|n| [n, n]})   %>
<%= f.select(:weight_high, (80..300).to_a.map{|n| [n, n]}) %>
<br />
<%= f.label :age_low, 'Age between:' %>
<%= f.select(:age_low, (18..100).to_a.map{|n| [n, n]}) %>
<%= f.select(:age_high, (18..100).to_a.map{|n| [n, n]}) %>
<br />
<%= f.label :city, 'City:' %>
<%= f.text_field :city, required: true %>
<%= f.label :province_state, 'Province or State:' %>
<%= f.text_field :province_state, required: true %>
<%= f.label :country, 'Country:' %>
<%= f.country_code_select :country, [[ 'United States', 'US' ], [ 'Canada', 'CA' ]] %>
<div><%= f.submit "Search" %></div>

在控制器中搜索参数

private
def search_params
    params.require(:search).permit(:age_low,:age_high,:weight_low,:weight_high,:height_low,:height_high,:gender,:screen_name,:city,:province_state,:country)
end 

1 个答案:

答案 0 :(得分:0)

由于没有任何关于你如何使用它的背景信息,因此这种情况并不多。您可能只允许参数哈希中的特定值?例如

def search_params
  params.require(:search).permit(:gender, :city, :province_state, :country)
end

也许提供更多信息。