2013-12-20 80 views
0

我看到「通過文本字段創建模型」this Railscast,因爲我希望用戶可以選擇現有項目或在表單中創建新項目。我一直跟着,但它仍然不適合我。我有我的代碼設置完全一樣的視頻顯示:通過文本字段創建模型

形式:

<%= f.label :project_id %><br> 
<%= f.collection_select :project_id, Project.order(:name), :id, :name, :prompt => "Select a project" %> 
or create one: 
<%= f.text_field :new_project_name %> 

模型的形式是:

class Item < ActiveRecord::Base 
    belongs_to :project 
    attr_accessor :new_project_name 
    before_save :create_project_from_name 

    def create_project_from_name 
     create_project(:name => new_project_name) unless new_project_name.blank? 
    end 
end 

項目模型

class Project < ActiveRecord::Base 
    has_many :items 
end 

這是爲什麼不爲我工作?

+0

你得到了什麼錯誤?你可以顯示你的「項目」模型嗎? – AbM

+0

我添加了項目模型。我沒有看到錯誤。 – user2270029

+0

如果你沒有看到錯誤,那麼當你說它不起作用時,你的意思是什麼? – aelfric5578

回答

0

對於Item,您指定:

belongs_to :factory_project

,並使用create_project。使用belongs_to :project

+0

哎呀,這是一個不屬於我的代碼的錯字。如果沒有它,仍然不行。 – user2270029

-1

我想通了。它需要符合我允許的參數。

相關問題