2010-10-06 31 views
1

美好的一天......我遇到了很大的麻煩,它讓我瘋狂。新腳手架沒有外鍵

我的用戶創作應該有一些額外的數據,這樣的:

<div class="field"><%= label_tag 'species', %> 
    <%= f.collection_select :species_id, Species.all, :id, :name %></div> 

它正確地顯示物種的名單,是的,但是當我做一個提交,它被完全忽略。數據庫的表中甚至沒有出現該數字,它只是消失。我已經把正確的關聯:

class Species < ActiveRecord::Base 
    has_many :users 
end 

class User < ActiveRecord::Base 
    # ... Other stuff 
    belongs_to :species 
    # ... Other stuff 
end 

我也試圖操縱控制器:

class UsersController < ApplicationController 
    def create 
    logout_keeping_session! 
    @user = User.new(params[:user]) 
    @user.species = Species.find(params[:species_id]) 
    # Other stuff 
    end 
end 

但是,這只是給了我'不進行身份找不到物種,即使參數數組包含一個元素「具有值的species_id'。 我在我的智慧的結尾。相當新的,但這是RESTful?不知道如何讓事情變得容易?我喜歡Rails並希望繼續。

謝謝您聽

+0

您可以在控制器嘗試創建用戶時從日誌中發佈相關信息嗎? – zetetic 2010-10-07 01:50:37

回答

0

您發現失敗,因爲params爲可能:params[:user][:species_id]但如果有喜歡它應該,應該已經設置了。