我对如何利用一种老板:通过关系来形成一种nes形的形式感到困惑。 我使用了Railscast,我看见,这份指南,以及Stackflow和谷其他地方的许多问题。
我试图通过文章的形式制造标签。 我的法典根据来自不同来源的许多信息,经历了许多变迁,而现在我还没有工作过。
文章课
class Article < ActiveRecord::Base
attr_accessible :content, :heading, :image, :tag_ids, :tags, :tag_name, :tag_attributes
belongs_to :user
has_many :comments, :dependent => :destroy
has_many :article_tags
has_many :tags, :through => :article_tags
accepts_nested_attributes_for :tags, :reject_if => proc { |attributes| attributes[ tag_name ].blank? }
...
end
tag子
class Tag < ActiveRecord::Base
attr_accessible :tag_name
has_many :article_tags
has_many :articles, :through => :article_tags
end
A class for article_tags
class ArticleTag < ActiveRecord::Base
belongs_to :article
belongs_to :tag
end
我的文章“控制者”。
def new
@article = Article.new
@tags = Tag.find(:all)
article_tag = @article.article_tags.build()
@article_tags = @article.tags.all
@article.article_tags.build.build_tag
3.times do
article_tag = @article.article_tags.build()
end
end
And my form for articles is currently like this (I have gone back and forth between nesting the fields_for :tags inside the fields_for :article_tags or just letting them be on their own):
<%= form_for @article , :html => { :multipart => true } do |f| %>
...excerpted...
<%= f.fields_for :article_tags do |t| %>
<%= t.fields_for :tags do |ta| %>
<%= ta.label :tag_name, "Tag name" %>
<%= ta.text_field :tag_name %>
<% end %>
<% end %>
我认识到这一点可能很不舒服;此时此刻,我很抱着新意,我不想 figure。 我是否必须增加条款内容——控制者创造? 这是否与“可获取”的范围有关? 或者我是否做了完全不同的事?
EDIT:
此处是请求参数,然后是Hck所建议的改动,并创设了新的条款,选择了现有标的3,并同时试图创建一个新的主角:
Started POST "/articles" for 127.0.0.1 at 2011-08-10 19:05:46 +1000 Processing by ArticlesController#create as HTML Parameters: {"utf8"=>"✓", "authenticity_token"=>"5CQuV4RWfFZD1uDjv1DrZbIe+GB/sDQ6yiAETZutmZ4=", "article"=>{"heading"=>"Test heading", "content"=>"Test Content", "tag_ids"=>["3"], "article_tags"=>{"tags"=>{"tag_name"=>"Test tag"}}}, "commit"=>"Submit"} User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1 WARNING: Can t mass-assign protected attributes: article_tags Tag Load (0.4ms) SELECT "tags".* FROM "tags" WHERE "tags"."id" = 3 LIMIT 1 AREL (0.4ms) INSERT INTO "articles" ("content", "user_id", "created_at", "updated_at", "heading", "image_file_name", "image_content_type", "image_file_size") VALUES ( Test Content , 1, 2011-08-10 09:05:46.228951 , 2011-08-10 09:05:46.228951 , Test heading , NULL, NULL, NULL) AREL (0.2ms) INSERT INTO "article_tags" ("article_id", "tag_id", "created_at", "updated_at") VALUES (88, 3, 2011-08-10 09:05:46.243076 , 2011-08-10 09:05:46.243076 ) [paperclip] Saving attachments. Redirected to [localhost] Completed 302 Found in 212ms
而且,如果我补充说:第_条对准条款的允许和再尝试,我会:
Started POST "/articles" for 127.0.0.1 at 2011-08-10 19:11:49 +1000 Processing by ArticlesController#create as HTML Parameters: {"utf8"=>"✓", "authenticity_token"=>"5CQuV4RWfFZD1uDjv1DrZbIe+GB/sDQ6yiAETZutmZ4=", "article"=>{"heading"=>"Test heading", "content"=>"Test content", "tag_ids"=>["3"], "article_tags"=>{"tags"=>{"tag_name"=>"Test tag "}}}, "commit"=>"Submit"} User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1 Tag Load (0.4ms) SELECT "tags".* FROM "tags" WHERE "tags"."id" = 3 LIMIT 1 Completed in 119ms
ActiveRecord::AssociationTypeMismatch (ArticleTag(#2165285820) expected, got Array(#2151973780)): app/controllers/articles_controller.rb:32:in `create