English 中文(简体)
铁路中的 Nest鱼形式
原标题:Nested forms in Rails using has_many :through

我对如何利用一种老板:通过关系来形成一种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

问题回答

用于替换<条码<____________________________________________________________________________________________________________________________________________________________________________

我并不认为你必须打上该条的标语。 两条只是各条款和当事方之间的结合。 你可以简单地在文章中创造新的主角,因为你们已经与它们有联系。 我认为,它来自“接受者-特工”。 审判。

<%= form_for @article , :html => { :multipart => true } do |f| %>
 ...excerpted...
  <%= f.fields_for :tags, Tag.new do |t| %>
    <%= t.label :tag_name, "Tag name" %>
    <%= t.text_field :name %>
  <% end %>
<% end %>

Also, you should try to mass assign it instead of saving every attribute piece by piece by using private params. I had a nested forms problem before too, so you can take a look at how I wrote my code: Cannot save record to database RAILS nested forms

The only thing I left out there was the private params section, which I recommended you to do.

private
    def venue_params
      params.require(:venue).permit(:name, :address, :discount, :latitude, :longitude, :tags_attributes =>[:name],:tag_ids => [])
    end

我也写了一张关于nes形式问题的博客,这样你也能看一看。

rel=“nofollow noreferer”>http://minling.github.io/





相关问题
rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

SSL slowness in EC2

We ve deployed our rails app to EC2. In our setup, we have two proxies on small instances behind round-robin DNS. These run nginx load balancers for a dynamically growing and shrinking farm of web ...

Auth-code with A-Za-z0-9 to use in an URL parameter

As part of a web application I need an auth-code to pass as a URL parameter. I am currently using (in Rails) : Digest::SHA1.hexdigest((object_id + rand(255)).to_s) Which provides long strings like : ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

activerecord has_many :through find with one sql call

I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent => :destroy has_many :roles, :through => :permissions end class Permission < ActiveRecord::...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签