【问题标题】:Rails: has_many through association, and the form for creating new instancesRails:has_many 通过关联,以及创建新实例的形式
【发布时间】:2011-01-03 23:19:39
【问题描述】:

我对 Rails 还是很陌生,只是想通过建立关联来获得我的第一个 has_many。

食谱有很多成分,每种成分都有食谱所需的量。 Ingredient_amount 表有一个 recipe_id、一个 ingredients_id 和一个数量。

创建新食谱时,我希望能够在同一个地方创建这些食谱/配料关联。最后,我将为成分构建一个 AJAX 自动完成器。现在,作为一个小步骤,我想假设该成分存在,并在我完成这部分后进行检查。

那么,我怎样才能为食谱制作 new.html.erb 呢?我怎样才能为不止一种成分扩展表格?

就目前而言,经过http://weblog.rubyonrails.org/2009/1/26/nested-model-forms 我仍然无法获得任何字段来添加成分。当前代码如下。

class Recipe < ActiveRecord::Base
    has_many :ingredient_amounts
    has_many :ingredients, :through => :ingredient_amounts
    accepts_nested_attributes_for :ingredient_amounts, :allow_destroy => true
end

class IngredientAmount < ActiveRecord::Base
  belongs_to :ingredient
  belongs_to :recipe
end

class Ingredient < ActiveRecord::Base
  has_many :ingredient_amounts
  has_many :recipes :through => :ingredient_amounts
end

这是我目前拥有的 new.html.erb:

   <h1>New recipe</h1>

<% form_for @recipe do |f| %>
  <%= f.error_messages %>

  <p>
    <%= f.label :name %><br />
    <%= f.text_field :name %>
  </p>
  <p>
    <%= f.label :instructions %><br />
    <%= f.text_area :instructions %>
  </p>
  <p>
    <%= f.label :numberOfServings %><br />
    <%= f.text_field :numberOfServings %>
  </p>
  <p>
    <%= f.label :prepTime %><br />
    <%= f.text_field :prepTime %>
  </p>

  <p>
    <% f.fields_for :ingredient_amounts do |ingredient_form| %>
    <%= ingredient_form.label :ingredient_formedient_id, 'Ingredient' %>
      <%= ingredient_form.collection_select :ingredient_id, Ingredient.all, :id, :name, :prompt => "Select an Ingredient"%>
      <%= ingredient_form.text_field :amount %>
    <% unless ingredient_form.object.new_record? %>
        <%= ingredient_form.label :_delete, 'Remove:' %>
        <%= ingredient_form.check_box :_delete %>

    <% end %>
  </p>
  <% end %>
  <p>
    <%= f.submit 'Create' %>
  </p>
<% end %>

<%= link_to 'Back', recipes_path %>

配方控制器的重要部分:

def new
    @recipe = Recipe.new

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @recipe }
    end
  end
  def create
    @recipe = Recipe.new(params[:recipe])

    respond_to do |format|
      if @recipe.save
        flash[:notice] = 'Recipe was successfully created.'
        format.html { redirect_to(@recipe) }
        format.xml  { render :xml => @recipe, :status => :created, :location => @recipe }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @recipe.errors, :status => :unprocessable_entity }
      end
    end
  end

而且...我不知道在成分数量控制器中从哪里开始。 这是我的第一次刺伤,我很确定它不是那么接近:)

def new
    @recipe = Recipe.find(params[:recipe_id])
    @ingredient = Ingredient.find(params[:ingredient_id])
    @ingredient_amount = Recipe.ingredient_amounts.build
  end

感谢您的帮助!

【问题讨论】:

  • 你有没有想过这个问题?我正在尝试做几乎相同的事情。

标签: ruby-on-rails


【解决方案1】:

我相信您正在寻找的是“嵌套模型表单”。

试试这个链接:http://weblog.rubyonrails.org/2009/1/26/nested-model-forms

当您不真正了解术语时,很难知道要搜索什么:)

【讨论】:

  • 链接有帮助!但我仍然无法让它工作。我正在编辑上面的帖子以获得新的 new.html.erb 和配方模型。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-23
  • 1970-01-01
  • 2016-04-02
  • 1970-01-01
  • 2014-01-26
  • 1970-01-01
相关资源
最近更新 更多