将哈希传递给强大的参数

时间:2014-03-13 14:30:56

标签: ruby-on-rails mongoid

我有各种各样的属性,我试图坚持,因为它们存在于params中,它们看起来像这样:

Parameters: 
{"utf8"=>"✓", "authenticity_token"=>"5hUuVqqmPulYVPsCP6GF4Tya3QWpDQXBQJVckbDPxRA=", 
"program"=>{"name"=>"Save categories", "description"=>"", 
"category_ids"=>{"0"=>"53029921d2b38bf8da000015", "1"=>"", "2"=>""}, "eligibility"=>"",
"how_to_access"=>"", "street_address"=>"", "city"=>"", "state"=>"", "zip"=>"", 
"neighborhood_ids"=>["", "52dda579d2b38bf39d000025", "52dda579d2b38bf39d00002e"],
"county"=>"", "phone"=>"", "email"=>"", "website"=>""}, "commit"=>"Update Program",
"id"=>"5320c2dbd2b38b90d7000004"}

具体来说,除了category_ids hash之外,其他所有东西都在工作:

"category_ids"=>{"0"=>"53029921d2b38bf8da000015", "1"=>"", "2"=>""}

在我使用这些类别创建和更新的各种控制器中,这是我强大的参数

def program_params
  params.require(:program).permit(:name, :slug, :description, :cost, :min_age, :max_age, :how_to_access, :min_grade,
                                :max_grade, :start_date, :end_date, :notes, :tag_list, :eligibility,
                                :street_address, :state, :city, :zip, :county, :phone, :email, :website, neighborhood_ids: [], category_ids: [])
end

具体来说,我不确定这部分应该如何写:

category_ids: []

我会尝试不同的东西,直到我弄清楚如何坚​​持它。事实上,我会传递这些参数并且没有任何东西得到保存,所以我想我需要明确说明[:0,:1,:2],但我真的不知道该怎么做,或者这是正确的解决方案

编辑:

新的params看起来像这样

"category_ids"=>{"one"=>"53029921d2b38bf8da000006", "two"=>"53029921d2b38bf8da00001b", "three"=>"53029921d2b38bf8da000004"}

表单中的实际html在我这里搞砸了。解决!

1 个答案:

答案 0 :(得分:2)

params.require(:program).permit(:name, :slug, :description, :cost, :min_age, :max_age, :how_to_access, :min_grade, :max_grade, :start_date, :end_date, :notes, :tag_list, :eligibility, :street_address, :state, :city, :zip, :county, :phone, :email, :website, neighborhood_ids: [], category_ids: ['1','2', '3'])

您无法将数字转换为符号。