开发者

Rails Nested Model Form with has_many through Relationship and build call

I'm building a one page form that will create a model and it's relations. I've gone through a ton of resources around the net as well as the complex forms series on Railscasts but can't seem to find the solution to my problem.

Here's what I have.

class Campaign < ActiveRecord::Base
  has_many :artwork_groups
  has_many :artwork_locales, :through => :artwork_groups

  accepts_nested_attributes_for :artwork_groups
  accepts_nested_attributes_for :artwork_locales
end

class ArtworkGroup < ActiveRecord::Base
  belongs_to :campaign
  has_many :a开发者_开发技巧rtwork_locales
end

class CampaignsController < ApplicationController
  def new    
    @campaign = Campaign.new
    @locales = Locale.all

    # Build the artowrk groups and locales for the new campaign
    2.times do
      @campaign.artwork_groups.build do |ag|
        @locales.each do |locale|
          ag.artwork_locales.build(:locale_id => locale.id)
        end
        logger.debug ag.artwork_locales
      end
    end

    @games = GameVersion.all(:order => "game_key").group_by(&:game_family).sort_by{|k| k.first.name }

    respond_to do |format|
      format.html
    end
  end

In my view I have the following (markup removed):

<% form_for @campaign, :html => { :multipart => true } do |campaign| %>

  <%= campaign.error_messages %>
  <%= flash[:campaign_wizard] %>

  <%= campaign.label :title %>
  <%= campaign.text_field :title %>

  <%= campaign.label :url %>
  <%= campaign.text_field :url %>

  <%= campaign.label :start_date %>
  <%= campaign.text_field :start_date, {:class=>"datetimepicker"} %>

  <%= campaign.label :end_date %>
  <%= campaign.text_field :end_date, {:class=>"datetimepicker"} %>

  <% campaign.fields_for :artwork_groups do |aw| %>

    <%= aw.label :name, "Artwork Group Name"  %>
    <%= aw.text_field :name %>

    <% aw.fields_for :artwork_locales do |al| %>

      <%= al.label :locale_id %>
      <%= al.text_field :locale_id %>

      <%= al.label :language %>
      <%= al.text_field :language %>

      <%= al.label :upstate %>
      <%= al.text_field :upstate %>

    <% end %>

  <% end %>

  <%= campaign.submit 'Save Campaign &amp; Publish' %>  

<% end %>

I'm getting the 2 artwork group sections in my view but I'm only getting one artwork locale per artwork group. I was expecting one for each locale I've got loaded. I'm assuming this is an issue with the has_many through relationship or the nested build calls in my controller.

Any help will be greatly appreciated.


Have you tried adding accepts_nested_attributes_for :artwork_locales to ArtworkGroup?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜