2014-02-07 47 views
2

我有3個表稱爲引腳(職位),流派(類別)和genre_pin(引腳和流派之間多對多,因爲引腳可以有很多流派)。Rails 4 - 多對多類別不會保存到數據庫

我正在嘗試構建創建表單,這將允許用戶選擇儘可能多的流派,因爲他們想要的針和保存/創建。創建工作正常,沒有錯誤,但它沒有將任何關係記錄保存到genre_pin表。我很確定我可能在這裏錯過了一些非常明顯的東西。

我使用的是'別針'和'類型'這兩個術語,但你可以把它們看作'帖子'和'類別'。

插針型

class Pin < ActiveRecord::Base 

    belongs_to :user 
    belongs_to :type 
    has_many :replies 
    has_many :genre_pins 
    has_many :genres, :through => :genre_pins 

    accepts_nested_attributes_for :genres, allow_destroy: true, reject_if: proc { |genre| genre[:id].blank? } 

    validates :user_id, presence: true 
    validates :type_id, presence: true 
    validates :title, presence: true 
    validates :description, presence: true 

end 

類型型號

class Genre < ActiveRecord::Base 

    has_many :genre_pins 
    has_many :pins, :through => :genre_pins 

    validates :title, presence: true 

end 

GenrePin型號

class GenrePin < ActiveRecord::Base 

    belongs_to :pin 
    belongs_to :genre 

    validates_uniqueness_of :pin_id, :scope => :genre_id 

end 

接下來,我針#新的觀點,我有以下形式:

引腳新視角

<%= form_for :pin, url: pins_path do |f| %> 

    <div class="small-12 columns"> 
     <%= f.label :title %> 
     <%= f.text_field :title %> 
    </div> 

    <div class="small-12 columns"> 
     <%= f.label :description %> 
     <%= f.text_area :description, :rows => 10 %> 
    </div> 

    <div class="small-12 columns"> 
     <%= f.label :type %> 
     <%= f.collection_select :type_id, Type.order(:title), :id, :title, include_blank: false %> 
    </div> 

    <div class="small-12 columns"> 
     <%= f.label :genres %> 
     <%= f.collection_select :genre_ids, Genre.order(:title), :id, :title, {}, { :multiple => true } %> 
    </div> 

    <div class="small-12 columns"> 
     <%= f.submit "Create Pin", :class => "button" %> 
    </div> 

<% end %> 

視圖工作正常,並讓我從流派表可用的類型。但是,當我去保存表格時,會創建記錄(pin),但與流派的關係不是。我在這裏錯過了什麼?沒有錯誤拋出,只是沒有任何反應。我假設Rails應該通過與流派的關聯來創建這些PinGenre記錄。

引腳控制器(創建)

def create 

    @pin = Pin.new(pin_params) 
    @pin.user_id = current_user.id 
    @pin.save 

    if @pin.save 
     flash[:success] = "Pin successfully created" 
     redirect_to @pin 
    else 
     flash[:warning] = @pin.errors.full_messages.to_sentence 
     redirect_to :action => "new" 
    end 

end 

private 

     def pin_params 

      params.require(:pin).permit(:title, :description, :type_id, :genre_ids) 

     end 

是否有進一步的措施我需要在這裏做什麼呢?記錄是否需要由我自己明確創建?

感謝您的幫助和耐心。我希望我已經清楚地說明了這一點。 Michael。

Schema.rb

ActiveRecord::Schema.define(version: 0) do 

    create_table "genre_pins", force: true do |t| 
    t.integer "pin_id" 
    t.integer "genre_id" 
    end 

    create_table "genres", force: true do |t| 
    t.string "title", null: false 
    end 

    create_table "pins", force: true do |t| 
    t.integer "user_id",           null: false 
    t.string "title",  limit: 160,       null: false 
    t.text  "description",          null: false 
    t.decimal "latitude",    precision: 10, scale: 8 
    t.decimal "longitude",    precision: 11, scale: 8 
    t.datetime "created_at" 
    t.datetime "updated_at" 
    t.datetime "deleted_at" 
    t.integer "type_id",           null: false 
    end 

    create_table "profiles", force: true do |t| 
    t.integer "user_id",           null: false 
    t.string "first_name",          null: false 
    t.string "last_name",          null: false 
    t.string "gender" 
    t.string "email" 
    t.text  "bio" 
    t.decimal "latitude",    precision: 10, scale: 8 
    t.decimal "longitude",    precision: 11, scale: 8 
    t.string "image",  limit: 2000 
    t.datetime "updated_at" 
    t.datetime "created_at" 
    t.datetime "deleted_at" 
    end 

    create_table "replies", force: true do |t| 
    t.integer "pin_id",  null: false 
    t.integer "user_id", null: false 
    t.text  "reply",  null: false 
    t.datetime "updated_at" 
    t.datetime "created_at" 
    t.datetime "deleted_at" 
    end 

    create_table "saves", force: true do |t| 
    t.integer "pin_id",  null: false 
    t.integer "user_id", null: false 
    t.datetime "updated_at" 
    t.datetime "created_at" 
    t.datetime "deleted_at" 
    end 

    create_table "settings", force: true do |t| 
    t.integer "user_id",  null: false 
    t.boolean "show_email",  null: false 
    t.boolean "show_telephone", null: false 
    t.integer "timezone",  null: false 
    t.datetime "updated_at" 
    t.datetime "created_at" 
    t.datetime "deleted_at" 
    end 

    create_table "types", force: true do |t| 
    t.string "title", null: false 
    end 

    create_table "users", force: true do |t| 
    t.string "provider",   limit: 45, null: false 
    t.string "provider_id",  limit: 45, null: false 
    t.string "oauth_token",  limit: 1000 
    t.datetime "oauth_expires_at" 
    t.datetime "updated_at" 
    t.datetime "created_at" 
    t.datetime "deleted_at" 
    end 

end 

生成查看源

<form accept-charset="UTF-8" action="/pins" method="post"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="&#x2713;" /><input name="authenticity_token" type="hidden" value="3VO3kVR/62JFf6dJs+yyPTPvYJb3fsUSLRFljSE0Jdk=" /></div> 

     <div class="small-12 columns"> 
      <label for="pin_title">Title</label> 
      <input id="pin_title" name="pin[title]" type="text" /> 
     </div> 

     <div class="small-12 columns"> 
      <label for="pin_description">Description</label> 
      <textarea id="pin_description" name="pin[description]" rows="10"> 
</textarea> 
     </div> 

     <div class="small-12 columns"> 
      <label for="pin_type">Type</label> 
      <select id="pin_type_id" name="pin[type_id]"><option value="2">Available</option> 
<option value="1">Wanted</option></select> 
     </div> 

     <div class="small-12 columns"> 
      <label for="pin_genres">Genres</label> 
      <input name="pin[genre_ids][]" type="hidden" value="" /><select id="pin_genre_ids" multiple="multiple" name="pin[genre_ids][]"><option value="5">Ambient</option> 
<option value="4">Electronic</option> 
<option value="2">Indie</option> 
<option value="3">Jazz</option> 
<option value="6">Metal</option> 
<option value="1">Rock</option></select> 
     </div> 

     <div class="small-12 columns"> 
      <input class="button" name="commit" type="submit" value="Create Pin" /> 
     </div> 

</form> 
+0

你可以發佈你的引腳控制器的'新'方法嗎? – Pavan

+0

引腳控制器中的'新'動作只是空白,因爲它所做的只是加載視圖。 –

+0

你可以發佈你的'genre_pin'Active Record Migration文件嗎? – Pavan

回答

3

你需要告訴你的控制器,這是genre_ids`預計將數組:

params.require(:pin).permit(:title, :description, :type_id, genre_ids: []) 

何wever我希望你會得到另一個錯誤(未知屬性gener_ids)。讓我知道這是否會發生。

+1

我現在收到錯誤「字段ID沒有默認值」。請求參數顯示:{「utf8」=>「✓」,「authenticity_token」=>「3VO3kVR/62JFf6dJs + yyPTPvYJb3fsUSLRFljSE0Jdk =」,「pin」=> {「title」=>「aaaa」,「description」=> 「aaa」,「type_id」=>「2」,「genre_ids」=> [「」,「4」,「3」]},「commit」=>「Create Pin」,「action」=> ,「控制器」=>「管腳」}。似乎有一個空白的價值......可能會絆倒它?不知道爲什麼會發生。感覺我們正在接近。謝謝。 –

+0

對,genre_pins表的數據庫中的ID字段未設置爲Auto Increment。現在,我在數據庫中改變了這一點,它一切正常,如預期的那樣!非常感謝。我會接受這個答案。 –

+0

出於好奇 - 你在你的Pin模型中定義了'genre_ids ='方法嗎?沒有它,我無法得到它的工作方式。 :) – BroiSatse