从另外两个表创建连接表上的记录

时间:2021-08-11 15:22:32

I have a Recipe model and an Ingredient model. They both has_many :recipe_ingredients and Recipe has_many :ingredients, through: recipe_ingredients and Ingredient has_many :recipes, through: recipe_ingredients and RecipeIngredient belongs_to both Recipe and Ingredient. Coming from the recipe#show I have a link_to new_recipe_ingredient_path(@recipe). In that view I have

我有一个食谱模型和一个成分模型。他们都has_many:recipe_ingredients和Recipe has_many:成分,通过:recipe_ingredients和Ingredient has_many:食谱,通过:recipe_ingredients和RecipeIngredient belongs_to Recipe and Ingredient。来自食谱#show我有一个link_to new_recipe_ingredient_path(@recipe)。在那个观点中,我有

<%= form_for @recipe_ingredient do |f| %>
    <%= f.collection_select :ingredient_id, Ingredient.all, :id, :name %>

    <%= f.label :amount %>
    <%= f.number_field :amount %>
    <%+ f.submit %>
<% end %>

My question is, what do I need in my ingredients_controller to create the record in RecipeIngredient table? I have tried shoveling in the params. I have tried straight up creating the RecipeIngredient in the INgredient controller and get a forbidden attributes error. I try other things and get a typemismatch. I'm wondering if I can redirect to the create method in the REcipeINgredient controller and create it there.

我的问题是,在recipe_controller中我需要在RecipeIngredient表中创建记录?我试过铲除了参数。我已经尝试直接在INgredient控制器中创建RecipeIngredient并获得禁用属性错误。我尝试其他的东西,并得到一个typemismatch。我想知道我是否可以重定向到REcipeINgredient控制器中的create方法并在那里创建它。

2 个解决方案

#1


1  

Sorry I tried both of those and didn't work. My new form-for looks very similar except I have

对不起,我尝试了这两个并没有奏效。我的新形式 - 看起来非常相似,除了我有

<%= form_for [@recipe_ingredient] do |f|%>
    <%= f.collection_select :ingredient_id, Ingredient.all, :id, :name %>

    <%= f.label :amount %>
    <%= f.number_field :amount, step: :any %>

    <%= f.submit 'Add Ingredient' %>
<% end %>

And in my Ingredients Controller

在我的成分控制器

def new
  @recipe = Recipe.find(params[:recipe_id])
  @ingredient = Ingredient.new
  @recipe_ingredient = RecipeIngredient.new
end

def create
  @recipe = Recipe.find(params[:recipe_id])
  @ingredient = Ingredient.find(params[:recipe_ingredient][:ingredient_id])
  @recipe_ingredient = RecipeIngredient.create(
    recipe: @recipe,
    ingredient: @ingredient,
    amount: params[:recipe_ingredient][:amount]
  )

  redirect_to recipe_path(@recipe)
end

I know it's not the prettiest but it works, :P

我知道这不是最漂亮的但是有效,:P

#2


0  

Based on your question "what do I need in my ingredients_controller to create the record in RecipeIngredient table?" Your code is trying to create Ingredients from recipes which is the opposite, but I'll assume that you are trying to achieve your question, so here is:

基于您的问题“我在ingredients_controller中需要什么才能在RecipeIngredient表中创建记录?”你的代码试图从食谱中创建相反的成分,但我会假设你正在尝试实现你的问题,所以这里是:

Modify or add this at your controller

在您的控制器上修改或添加它

class IngredientsController < ApplicationController
 def ingredient_params
 params.require(:ingredient).permit(
   :id,
   :your_attributes_here,
   {:recipe_ids => []}
 end
end

at your view :

在你看来:

<%= form_for @ingredient do |f| %>
 <%= f.collection_select :recipe_ids, Recipe.all, :id, :name, {}, { multiple: true } %>
<% end %>

#1


1  

Sorry I tried both of those and didn't work. My new form-for looks very similar except I have

对不起,我尝试了这两个并没有奏效。我的新形式 - 看起来非常相似,除了我有

<%= form_for [@recipe_ingredient] do |f|%>
    <%= f.collection_select :ingredient_id, Ingredient.all, :id, :name %>

    <%= f.label :amount %>
    <%= f.number_field :amount, step: :any %>

    <%= f.submit 'Add Ingredient' %>
<% end %>

And in my Ingredients Controller

在我的成分控制器

def new
  @recipe = Recipe.find(params[:recipe_id])
  @ingredient = Ingredient.new
  @recipe_ingredient = RecipeIngredient.new
end

def create
  @recipe = Recipe.find(params[:recipe_id])
  @ingredient = Ingredient.find(params[:recipe_ingredient][:ingredient_id])
  @recipe_ingredient = RecipeIngredient.create(
    recipe: @recipe,
    ingredient: @ingredient,
    amount: params[:recipe_ingredient][:amount]
  )

  redirect_to recipe_path(@recipe)
end

I know it's not the prettiest but it works, :P

我知道这不是最漂亮的但是有效,:P

#2


0  

Based on your question "what do I need in my ingredients_controller to create the record in RecipeIngredient table?" Your code is trying to create Ingredients from recipes which is the opposite, but I'll assume that you are trying to achieve your question, so here is:

基于您的问题“我在ingredients_controller中需要什么才能在RecipeIngredient表中创建记录?”你的代码试图从食谱中创建相反的成分,但我会假设你正在尝试实现你的问题,所以这里是:

Modify or add this at your controller

在您的控制器上修改或添加它

class IngredientsController < ApplicationController
 def ingredient_params
 params.require(:ingredient).permit(
   :id,
   :your_attributes_here,
   {:recipe_ids => []}
 end
end

at your view :

在你看来:

<%= form_for @ingredient do |f| %>
 <%= f.collection_select :recipe_ids, Recipe.all, :id, :name, {}, { multiple: true } %>
<% end %>