Ruby-on-rails 未定义的方法“stringify_keys!” 轨道上的红宝石

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1815697/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 22:00:26  来源:igfitidea点击:

undefined method `stringify_keys!' ruby on rails

ruby-on-railsparameters

提问by Lilz

I have this code:

我有这个代码:

def addcar
  @car = Car.new(params[:car])
  render :action => 'list'
end

<% @allcars.each do |cell| %>
  <p>
    <%= link_to cell.to_s, :controller => 'car', :action => 'addcar', :car => cell.to_s %>
  </p>
<% end %>

It's giving me this error:

它给了我这个错误:

undefined method `stringify_keys!' for "Honda":String

未定义的方法“stringify_keys!” 对于“本田”:字符串

I don't understand what is wrong with :car.

我不明白有什么问题:car

回答by ennuikiller

in the addcarmethod, you try to create a new object (createmethod) while transfering just a string to it (params[:car]which apparently is set to "Honda").

addcar方法中,您尝试创建一个新对象(create方法),同时只向它传输一个字符串(params[:car]显然设置为“Honda”)。

createexpects to get an attributes hash and to stringify it's keys for the column names.

create期望获得属性散列并将其字符串化为列名的键。

If you have a column named name in your cars table then try this:

如果您的汽车表中有名为 name 的列,请尝试以下操作:

@car = Car.new(:name => params[:car])