Cocoon Nested 资源节约同时属于协会

Cocoon Nested Resources Saving Belongs To Association Simultaneously

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

通过的参数:

=> {"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>

尝试将 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