我會通過Rails工作指南(http://guides.rubyonrails.org/getting_started.html),and我停留在第11項‘構建多元雛型’Ruby on Rails的指南:未定義的方法`fields_for」的零:NilClass
的鍛鍊; Tibial的這部分解釋。有關包括另一種形式中一個模型......
錯誤在新建/編輯後的表單字段:未定義的方法`fields_for」的零:NilClass
Extracted source (around line #2):
1: <% @post.tags.build %>
2: <%= form.fields_for :tags do | tag_form | %>
3: <div class="field">
4: <%= tag_form.label :name, 'Tag: ' %>
5: <%= tag_form.text_field :name %>
的代碼是完全一樣每一個練習(我甚至有絕望和複製粘貼代碼直接從樣本)
我的代碼張貼在下面..我花了幾個小時比較這與指南中的代碼,它是完全一樣的。任何人都可以指出我可能會出錯嗎?
許多在此先感謝。
這裏是我的,包括部分
型號/ post.rb代碼:
class Post < ActiveRecord::Base
attr_accessible :content, :name, :title, :tags_attributes
validates :name, :presence =>true
validates :title, :presence =>true,
:length => { :minimum => 5 }
has_many :comments, :dependent => :destroy
has_many :tags
accepts_nested_attributes_for :tags, :allow_destroy => :true,
:reject_if => proc { |attrs| attrs.all? { |k, v| v.blank? } }
end
型號/ tag.rb:
class Tag < ActiveRecord::Base
belongs_to :post
attr_accessible :name
end
帖子/ _form .html.erb:
個<% @post.tags.build %>
<%= form_for(@post) do |post_form| %>
<% if @post.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@post.errors.count, "error") %> prohibited this post from being saved:</h2>
<ul>
<% @post.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= post_form.label :name %><br />
<%= post_form.text_field :name %>
</div>
<div class="field">
<%= post_form.label :title %><br />
<%= post_form.text_field :title %>
</div>
<div class="field">
<%= post_form.label :content %><br />
<%= post_form.text_area :content %>
</div>
<h2>Tags</h2>
<%= render :partial => 'tags/form',
:locals => {:from => post_form} %>
<div class="actions">
<%= post_form.submit %>
</div>
<% end %>
標籤/ _form.html.erb:
<%= form.fields_for :tags do | tag_form | %>
<div class="field">
<%= tag_form.label :name, 'Tag: ' %>
<%= tag_form.text_field :name %>
</div>
<% unless tag_form.object.nil? || tag_form.object.new_record? %>
<div class="field">
<%= tag_form.label :_destroy, 'Remove: ' %>
<%= tag_form.check_box :_destroy %>
</div>
<% end %>
<% end %>
一個錯字嗎? ':locals => {:from => post_form}' – mikej 2013-02-27 16:22:10
搜索谷歌,一個建議使用form_for而不是form.fields_for,但這種方式呈現正在發生,但沒有發佈後創建或更新 – user2116155 2013-02-27 16:41:24