我正在尝试构建一个表单,允许用户创建一个新帖子,该帖子的标签和该标签的TagType,所有这些都有一个提交按钮。
我的模型设置如下:
class Post < ActiveRecord::Base
belongs_to :user
has_many :post_tag_relationships, dependent: :destroy
has_many :tags, through: :post_tag_relationships
.
.
end
class Tag < ActiveRecord::Base
has_many :reverse_post_tag_relationships, class_name: "PostTagRelationship"
has_many :posts, through: :reverse_post_tag_relationships,
class_name: "PostTagRelationship"
belongs_to :tag_type
.
.
end
class TagType < ActiveRecord::Base
has_many :tags
.
.
end
在表单所在的页面控制器中,我的方法定义如下:
@post = current_user.posts.build
@tag = @post.tags.build
@tag_type = @tag.tag_type.build
如果我只包含post和tag方法,我的表单就会显示正常,如:
<%= form_for(@post) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<div class="field">
<%= f.text_area :content, placeholder: "Compose new post..." %>
</div>
<%= fields_for(@tag) do |u| %>
<div class="field">
<%= u.text_area :name, placeholder: "Tag" %>
</div>
<% end %>
<%= f.submit "Post", class: "btn btn-large btn-primary" %>
<% end %>
但是当我添加fields_for(@tag_type)时,使用:
<%= fields_for(@tag_type) do |y| %>
<div class="field">
<%= y.select :name %>
</div>
<% end %>
我为NilClass获取了一个未定义的方法'model name':Class
我对Rails很新,我的猜测是它与标签belongs_to tag_types(而帖子has_many标签)有关。如果有人知道修复,那将非常感激。感谢。