嵌套在Rails 3.2中的嵌套表单下

时间:2013-11-26 13:39:16

标签: ruby-on-rails nested-attributes

我需要帮助建立这种情况的关系:

我有3个模型:CampaignCampaignPostUser

我想实现像ff:

这样的东西
  1. 广告系列包含多个campaign_posts
  2. 用户每个广告系列都有多个CampaignPost
  3. CampaignPost属于用户
  4. CampaignPost属于Campaign
  5. 样机:http://d.pr/i/uXq6

    在创建全新的广告系列时,current_user可以将CampaignPost添加到广告系列中,最终CampaignPost将按用户进行分组。并将计算一个广告系列中每个用户的帖子数量

    我首先做的是在用户模型中嵌套CampaignPost:

    user.rb:

    # Campaign Posts
      has_many :campaign_posts
      accepts_nested_attributes_for :campaign_posts, :allow_destroy => true, 
                                    :reject_if => lambda { |a| a[:post].blank? }
    

    campaign_post.rb

    class CampaignPost < ActiveRecord::Base
      attr_accessible :campaign_id, :post, :user_id
    
      belongs_to :user
      belongs_to :campaign
    end
    

    campaign.rb

      # Users
      has_and_belongs_to_many :users
    
      has_many :campaign_posts
    

    我的编辑表单:

    = simple_form_for(@campaign, remote: true) do |f|
      .admin_tracker_wrapper
        .new_campaign_header_bg
          .new_campaign_header
            = image_tag('/assets/admin_wrench_03.png')
            %b= "Admin"
        .new_campaign_bg#new_campaign_bg1
          .inputs#inputs_new
            = f.input :name, label: false, placeholder: "Enter campaign name.."
            = f.text_area :objective, placeholder: "Enter objective.."
    
            = submit_tag "Edit", class:"edit_campaign_btn"
    
        - @campaign.campaign_posts.build
        .new_campaign_bg#new_campaign_bg2
          - @campaign.users.each do |user|
            .post_header
              .row-fluid
                .span1
                  .circle
                    - if user.photo_url.blank?
                      = image_tag 'avatar.png'
                    - else
                      = image_tag user.photo_url, :alt=>''
                .span2.post_header_full_name
                  %span.full_name= user.full_name
                  %br
                  - if @campaign.campaign_posts.count <= 1
                    %span.posts_count= "#{@campaign.campaign_posts.count} Post"
                  - else
                    %span.posts_count= "#{@campaign.campaign_posts.count} Posts"
                .span1.pull-right
                  %a{href: "#", class:"title"} &nbsp;
            %hr
            %br
            #d-form
              - @campaign.campaign_posts.each.with_index(1) do |campaign,index|
                /= campaign.post
                .inputs#inputs_edit
                  = user.fields_for :campaign_posts, campaign do |c|
                    %span.post_index= "#{index} . "
                    &nbsp;
                    = c.text_field :post
                    = submit_tag "Enter", class:"enter_campaign_btn"
                    %hr
    

    在我的表单中,我将@campaign作为父表单,然后通过@campaign.users嵌套用户, 然后为fields_for

    准备好campaign_posts

    但是fields_for campaign_posts没有显示,我认为关系或表单本身有问题。

    任何变通办法都将受到赞赏。

2 个答案:

答案 0 :(得分:0)

我认为您需要查看fields_for的工作原理。在railscasts网站上有一些非常好的教程:http://railscasts.com/episodes/196-nested-model-form-revised

据我所知,代码user.fields_for应该引发错误。 fields_for是FormBuilder上的一种方法,不应出现在用户模型上。

因此,当您在@campaign的上下文中进行编辑时(这就是您在表单中开始的内容)。您需要确保所有内容都映射到该广告系列。 IE需要在广告系列中使用accepts_nested_attributes_for,而不是用户等。

答案 1 :(得分:0)

我认为问题在于这一行:

- @campaign.campaign_posts.build

对构建的调用将实例化该类的一个(或多个)实例(CampaignPost)并将它们返回给您,但将它们添加到campaign_posts集合中。该视图假定它们已添加到集合中。

我会考虑使用控制器将campaign_posts的集合作为实例变量检索,然后在视图中使用它。

@campaign_posts = @campaign.campaign_posts.count.zero? ? [@campaign.campaign_posts.build] : @campaign.campaign_posts

另外,@ campaign.campaign_posts.each.with_index(1)是拼写错误(@ campaign.campaign_posts.each_with_index)吗?