Cocoon嵌套资源同时保存属于协会

时间:2015-08-07 16:11:49

标签: ruby-on-rails nested-attributes cocoon-gem

我正在尝试在我的应用中创建食谱,并使用cocoon动态添加成分。食谱和配料都属于餐厅。就像这些例子一样,我的参数正确传递。但是,我正在保存食谱,因此与餐厅的关联会自动设定。但是,成分和餐厅之间的关联不是。在rails控制台中,我可以看到正在创建的所有新成分但未填充restaurant_id列。我如何强制rails也设置该列?

Params Passed:

=> {"utf8"=>"✓",
 "authenticity_token"=>"...",
 "recipe"=>
  {"name"=>"Random Dish",
   "summary"=>"A random summary",
   "description"=>"A random description",
   "recipe_ingredients_attributes"=>
    {"1438959353582"=>{"ingredient_id"=>"2", "_destroy"=>"false"},
     "1438959355828"=>{"ingredient"=>{"name"=>"Chicken", "unit_of_measurement"=>"kg"}, "ingredient_id"=>"", "_destroy"=>"false"}}},
 "commit"=>"Create Recipe",
 "controller"=>"recipes",
 "action"=>"create",
 "restaurant_id"=>"4"}

控制器:

def create
    # require pry; binding.pry;
    @recipe = @restaurant.recipes.new(recipe_params)
    if @recipe.save
        flash[:success] = "Your recipe was created tastefully!"
        redirect_to restaurant_recipes_path
    else
        render :new
    end
end

def recipe_params
    params.require(:recipe).permit(
        :name, :summary, :description,
        recipe_ingredients_attributes: [:id, :_destroy, :ingredient_id,
        ingredient_attributes: [:id, :_destroy, :name, :quantity]]
    )
end

查看:

_form.html.erb

<%= simple_form_for([@restaurant, @recipe]) do |f| %>
  <%= f.input :name %>
  <%= f.input :summary %>
  <%= f.input :description, rows: 10%>
  <strong>Ingredients: </strong>
    <div id="recipe_ingredients">
      <%= f.simple_fields_for :recipe_ingredients do |recipe_ingredient| %>
        <%= render 'recipe_ingredient_fields', :f => recipe_ingredient %>
      <% end %>
      <div class="links">
        <%= link_to_add_association 'Add Ingredient', f, :recipe_ingredients %>
      </div>
    </div>

_recipe_ingredient_fields.html.erb

<div class="nested-fields">  
    <div class="form-inline">
    <div class="recipe_from_list" style="display: inline;">
      <%= f.association :ingredient, collection: @restaurant.ingredients, prompt: 'Choose an existing ingredient', label: false %>
        <%= link_to_add_association 'or create a new ingredient', f, :ingredient, class: 'add-ingredient' %>
        <%= link_to_remove_association f, class: 'remove-tag btn btn-default btn-xs' do %>
        <div class="glyphicon glyphicon-remove"></div>
      <% end %>
    </div>
    </div>
</div>

_ingredient_fields.html.erb

<div class="nested-fields form-inline">
  <%= f.input :name, :placeholder => 'ingredient name', :label => false %>
  <%= f.input :unit_of_measurement, collection: ['kg','grams','lb','oz', 'litres', 'ml'], :selected => '1' %>
</div>

1 个答案:

答案 0 :(得分:1)

尝试将restaurant_id添加到_ingredient_fields.html.erb并在recipe_params

中允许

_ingredient_fields.html.erb

= f.input :restaurant_id, :as => :hidden, :input_html => { :value => @restaurant.id }

鉴于Ingredient模型具有restaurant_id字段

Controller

def recipe_params
    params.require(:recipe).permit(
        :name, :summary, :description,
        recipe_ingredients_attributes: [:id, :_destroy, :ingredient_id,
        ingredient_attributes: [:id, :_destroy, :name, :quantity, :restaurant_id]]
    )
end