Abi*_*bid 5 forms ruby-on-rails nested-forms ruby-on-rails-3
我收到了错误
ActionView::Template::Error (undefined method `values_at' for nil:NilClass):
Run Code Online (Sandbox Code Playgroud)
当使用nested_form gem时
这是我的代码
楷模
class Poll < ActiveRecord::Base
attr_accessible :question, :poll_answers_attributes
has_many :poll_answers
accepts_nested_attributes_for :poll_answers
end
class PollAnswer < ActiveRecord::Base
belongs_to :poll
attr_accessible :answer
end
Run Code Online (Sandbox Code Playgroud)
视图
=nested_form_for [:admin, @poll], mutipart: true, class: "form-horizontal" do |f|
.span6
.control-group
=f.label :question, class: "control-label"
.controls
=f.text_field :question, rows: "5", class: "span5"
= f.link_to_add "Add a Answer", :poll_answers
=f.submit "Create Post", class: "btn btn-primary"
Run Code Online (Sandbox Code Playgroud)
堆栈跟踪
/home/abid/.bundler/ruby/1.9.1/nested_form-6232dd853c27/lib/nested_form/builder_mixin.rb:41:in `block in link_to_add'
/home/abid/.bundler/ruby/1.9.1/nested_form-6232dd853c27/lib/nested_form/view_helper.rb:53:in `call'
/home/abid/.bundler/ruby/1.9.1/nested_form-6232dd853c27/lib/nested_form/view_helper.rb:53:in `after_nested_form_callbacks'
/home/abid/.bundler/ruby/1.9.1/nested_form-6232dd853c27/lib/nested_form/view_helper.rb:8:in `block in nested_form_for'
Run Code Online (Sandbox Code Playgroud)
有任何想法吗 ?
Sag*_*idi 12
正如nested_form gem的文档中所提到的,您必须在link_to_load按钮之前提及嵌套对象的field_for.我以前没有使用过这个宝石,但是经过文档后,我猜这个.
这里的表格看起来像
<%= nested_form_for [:admin, @poll], mutipart: true, class: "form-horizontal" do |f| %>
<%= f.text_field :question %>
<%= f.fields_for :poll_answers do |poll_ans_form| %>
<%= poll_ans_form.text_field :name %>
<%= poll_ans_form.link_to_remove "Remove this task" %>
<% end %>
<p><%= f.link_to_add "Add a Answer", :poll_answers %></p>
<% end %>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4027 次 |
| 最近记录: |