通过文本字段创建模型

时间:2013-12-20 21:22:58

标签: ruby-on-rails ruby-on-rails-4 rails-models

我在“通过文本字段创建模型”上看了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

为什么这对我不起作用?

2 个答案:

答案 0 :(得分:0)

对于Item,您指定:

belongs_to :factory_project

正在使用create_project

使用belongs_to :project

答案 1 :(得分:-1)

我明白了。它需要在我允许的参数范围内。

相关问题