2013-02-20 20 views
0

我在嘗試進行關聯工作時遇到了一些麻煩。Rails - has_many構建方法不保存關聯

我的模式是這樣的:

advertise.rb

class Advertise < ActiveRecord::Base 

    belongs_to :user 
    belongs_to :country 

    has_many :targets 
    # has_many :hss, :through => :targets 

    validates :description, :presence => true 
    validates :url, :presence => true 
    validates :country_id, :presence => true 
    validates :kind, :presence => true 

    attr_accessible :description, :hits, :url, :active, :country_id, :kind 

    KINDS = { 
    '1' => 'Commoditie', 
    '2' => 'Service', 
    } 

    HS = { 
    '1' => 'Section', 
    '2' => 'Chapter', 
    '4' => 'Heading', 
    '5' => 'SubHeading 1', 
    '6' => 'SubHeading 2', 
    } 
end 

hs.rb

class Hs < ActiveRecord::Base 
    attr_accessible :code, :kind 

    has_many :targets 
    has_many :advertises, :through => :targets 
end 

target.rb

class Target < ActiveRecord::Base 
    attr_accessible :advertise_id, :hs_id 

    belongs_to :advertise 
    belongs_to :hs 
end 

advertises_controller.rb

def new 
    @advertise = Advertise.new 

    @countries = Country.all 
    end 

    def create 
    @advertise = current_user.advertises.build(params[:advertise]) 

    if @advertise.save 
     flash[:notice] = 'Advertise created with successful' 
     redirect_to root_path 
    else 
     render :new 
    end 
    end 

form創建一個新的廣告。

/advertises/new.html.haml

%table.table.table-striped 
    %tr 
    %td{:colspan => 2}= advertise.input :url, :required => true 
    %tr 
    %td{:colspan => 2}= advertise.input :description, :required => true, :as => :text, :input_html => { :cols => 50, :rows => 3 } 
    %tr 
    %td{:colspan => 2}= advertise.input :country_id, :collection => @countries, :as => :select, :label => 'Origin', :required => true 
    %tr 
    %td{:colspan => 2}= advertise.input :kind, :collection => Advertise::KINDS.map(&:reverse), :as => :select, :label => 'Type', :required => true 
    %tr 
    %td{:colspan => 2}= advertise.input_field :active, as: :boolean, inline_label: 'Active' 

    =fields_for :targets do |target| 
    %tr 
     %td= select_tag :hs_kind, options_for_select(Advertise::HS.map(&:reverse).insert(0,'')) 
     %td= target.select :hs_id, '' 

hash PARAMS:

[3] pry(#<AdvertisesController>)> params 
=> {"utf8"=>"✓", 
"authenticity_token"=>"fOdn4NYLg/4HXruWURZPf9DYVT4EQzbaTRTKZvX1ugY=", 
"advertise"=> 
    {"url"=>"http://test.com", 
    "description"=>"test", 
    "country_id"=>"17", 
    "kind"=>"2", 
    "active"=>"1"}, 
"hs_kind"=>"2", 
"targets"=>{"hs_id"=>"487"}, 
"commit"=>"Create Advertise", 
"action"=>"create", 
"controller"=>"advertises"} 

哈希似乎確定我的,但它只是Advertise造成的,而不是創建target對相關的advertise

雖然嗎?也許是錯誤的模型關聯。

在此先感謝。

回答

1

嘗試改變

=fields_for :targets do |target| 

= advertise.fields_for :targets do |target| 

,並添加以下到advertise.rb

accepts_nested_attributes_for :targets 
attr_accessible :targets_attributes # just add targets_attributes to attr_accessible 

被警告,當你做到這一點,廣告對象有沒有具體目標,不會顯示目標字段。您必須建立與廣告相關的新目標對象才能顯示字段

# example: on the new action of the controller 
@advertise = Advertise.new 
@advertise.targets.build 
+0

Thanks!你是一個拯救生命的人! – 2013-02-20 16:50:55

相關問題