2014-10-16 66 views
0

我有一個用於創建新的事物的表單,其中有一個collection_select字段用於輸入new:thing關聯的事物:thing。每一件事:has_many:事物,通過一箇中間模型:related_things,它有一個thing_a_id和thing_b_id。因此,當我填寫字段並單擊submit時,應該使用分別等於兩個thing_id的thing_a_id和thing_b_id創建a:related_thing。但是沒有這樣的:related_thing被創建;該表單不會做任何事情。其他文本域仍然可以工作。我的代碼有什麼問題?Rails:表單不會將數據添加到模型中

我正在使用Rails 4.0.10。

事情/新景觀:

<h1>Add Something!</h1> 
<p> 
    <%= form_for @thing, :url => things_path, :html => { :multipart => true } do |f| %> 

    <%= f.text_field :name, :placeholder => "Name of the thing" %> 
    <br> 
    <%= f.label :related_things %> 
    <%= f.collection_select :related_things, Thing.all, :id, :name %> 
    <br> 
    <%= f.label :display_picture %> 
    <%= f.file_field :avatar %> 
    <br> 
    <%= f.submit "Submit", class: "btn btn-primary" %> 
    <% end %> 
</p> 

事情型號:

class Thing < ActiveRecord::Base 
    has_many :related_things 
    has_many :things, :through => :related_things 
    has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "30x30!" }, :default_url => "/images/:style/missing.png" 
    validates_attachment_content_type :avatar, :content_type => /\Aimage\/.*\Z/ 

    def related_things 
    related_thing_ids = RelatedThing. 
     where("thing_a_id = ? OR thing_b_id = ?", self.id, self.id). 
     map { |r| [r.thing_a_id, r.thing_b_id] }. 
     flatten - [self.id] 
    Thing.where(id: related_thing_ids) 
    end 

    def related_thing_ids=(ids) 
    ids.each do |id| 
     record = RelatedThing.where(thing_a_id: self.id, thing_b_id: id).first 
     record ||= RelatedThing.where(thing_a_id: id, thing_b_id: self.id).first 
     record ||= RelatedThing.create!(thing_a_id: self.id, thing_b_id: id) 
    end 
    end 

end 

RelatedThing型號:

class RelatedThing < ActiveRecord::Base 
    has_many :things 
end 

事情控制器:

class ThingsController < ApplicationController 

    def show 
    @thing = Thing.find(params[:id]) 
    @related_thing = RelatedThing.all 
    @thing.things.build 
    end 

    def new 
    @thing = Thing.new 
    @things = Thing.all 
    end 

    def create 
    @thing = Thing.new(thing_params) 
    if @thing.save 
     redirect_to @thing 
    else 
     render 'new' 
    end 
    end 

    private 

    def thing_params 
     params.require(:thing).permit(:name, :image_path, :avatar) 
    end 

end 

RelatedThings控制器:

class RelatedThingsController < ApplicationController 
    def new 
    @things = Thing.all.by_name 
    end 

    def create 
    @things = Thing.all.by_name 
    end 

    def edit 
    @things = Thing.all.by_name 
    end 
end 

回答

1

有導致這兩個問題:

  1. 由於Jamesuriah指出的那樣,你collection_select應該使用related_things_ids場來代替。

  2. 儘管發生了這種變化,但由於Rails的Strong Parameters,該字段實際上被濾除了參數映射。

具體來說,在你的控制器,該thing_params方法應該是這樣的:

def thing_params 
    params.require(:thing).permit(:name, :image_path, :avatar, :related_things_ids) 
end 

在上面的鏈接獲取更多信息閱讀上強大的參數。希望有所幫助!

1

收集選擇應該爲模型的工作被命名爲related_thing_ids,我相信。

+0

不,我得到相同的結果。新的東西被創建,但沒有相關的東西。 – 2014-10-16 18:54:03

+0

@ user3739453仔細檢查你是否允許參數?同時仔細檢查並調試您的related_thing_ids方法 – 2014-10-16 19:07:11

+0

啊,我不允許這個參數。謝謝! – 2014-10-16 19:15:53

相關問題