当我尝试将嵌套表单集成到我的rails应用程序(rails 3.1.3,ruby 1.9.2)时,我遇到了问题
首先,将嵌套表单添加到我的gemfile
gem "nested_form", :git => 'git://github.com/ryanb/nested_form.git'
然后,我运行捆绑安装,然后是 rails g nested_form:install ,到目前为止一切正常。
我转到下一步,将 nested_form 添加到javascript_include_tag
#views/layouts/application.html.erb
javascript_include_tag "application", "nested_form"
Everthing仍然没问题,我检查了HTML元素并看到包含了 nested_form.js 。然后,我尝试修改我的表单
#views/supplier/new.html.erb
<%= nested_form_for @supplier do |f| %>
<%= f.text_field :name %>
<% render "supplier_products", :f => f %>
<%= f.submit %>
<% end %>
#views/supplier/_supplier_products.html.erb
<%= f.fields_for :supplier_products do |supplier_product_form| %>
<%= supplier_product_form.text_area :description %>
<%= supplier_product_form.link_to_remove "Remove this product", supplier_product_form %>
<% end %>
<%= f.link_to_add "Add a product", :supplier_products %>
#controller/supplier.rb
has_many :supplier_products
accepts_nested_attributes_for :supplier_products, :allow_destroy => true
#controller/supplier_product.rb
belongs_to :supplier
但是当我尝试导航到供应商创建页面时,我收到了以下错误
undefined method `stringify_keys' for "javascript:void(0)":String
我试着一遍又一遍地查看我的代码,但我看不出问题。我从SOF阅读了很多网站,文章和主题,他们的解决方案都没有解决这个问题(重启服务器,重新安装gem,切换ruby版本等)
所以,我不知道如何解决这个问题。我现在需要你的建议。请:)
最后
通过修复
中的以下行解决了问题<% render "supplier_products", :f => f %>
到
<% render "supplier_products", :locals => {:f => f} %>