Parry Parry - 1 month ago 8
Ruby Question

Passing a parameter received in link_to to different actions in controller

I have an Item model as below.

class Item < ApplicationRecord
belongs_to :item_type, :class_name=>ItemType, :foreign_key=>"item_type_id"
end


and a RecipeIngredient model as below

class RecipeIngredient < ApplicationRecord
belongs_to :item, :class_name=>Item, :foreign_key=>"item_id"
belongs_to :ingredient, :class_name=>Ingredient, :foreign_key=>"ingredient_id"
validates_numericality_of :quantity
end


From the index view of items, I am passing the item_id to the index view of recipe_ingredients as below

<td><%= link_to 'Add Recipe', recipe_ingredients_path(:item_id =>item.id) %></td>


and the index view of recipe_ingredients only displays those ingredients which belong to the item with item_id as received in URL. For that the controller for RecipeIngredient is like this.

def index
@recipe_ingredients = RecipeIngredient.where(:item_id => params[:item_id])
end


now I am trying to pass the same item_id to newrecipe ingredient form from the index page of recipe ingredient like this.

<%= link_to 'New Recipe Ingredient', new_recipe_ingredient_path(:item_id => @item_id) %>


and the whole controller file for recipe ingredients, including new is given below.

class RecipeIngredientsController < ApplicationController
before_action :set_recipe_ingredient, only: [:show, :edit, :update, :destroy]

# GET /recipe_ingredients
# GET /recipe_ingredients.json
def index
@recipe_ingredients = RecipeIngredient.where(:item_id => params[:item_id])
end

# GET /recipe_ingredients/1
# GET /recipe_ingredients/1.json
def show
end

# GET /recipe_ingredients/new
def new
@recipe_ingredient = RecipeIngredient.new(:item_id => params[:item_id])
end

# GET /recipe_ingredients/1/edit
def edit
end

# POST /recipe_ingredients
# POST /recipe_ingredients.json
def create
@recipe_ingredient = RecipeIngredient.new(:item_id => params[:item_id])

respond_to do |format|
if @recipe_ingredient.save
format.html { redirect_to @recipe_ingredient, notice: 'Recipe ingredient was successfully created.' }
format.json { render :show, status: :created, location: @recipe_ingredient }
else
format.html { render :new }
format.json { render json: @recipe_ingredient.errors, status: :unprocessable_entity }
end
end
end

# PATCH/PUT /recipe_ingredients/1
# PATCH/PUT /recipe_ingredients/1.json
def update
respond_to do |format|
if @recipe_ingredient.update(recipe_ingredient_params)
format.html { redirect_to @recipe_ingredient, notice: 'Recipe ingredient was successfully updated.' }
format.json { render :show, status: :ok, location: @recipe_ingredient }
else
format.html { render :edit }
format.json { render json: @recipe_ingredient.errors, status: :unprocessable_entity }
end
end
end

# DELETE /recipe_ingredients/1
# DELETE /recipe_ingredients/1.json
def destroy
@recipe_ingredient.destroy
respond_to do |format|
format.html { redirect_to recipe_ingredients_url, notice: 'Recipe ingredient was successfully destroyed.' }
format.json { head :no_content }
end
end

private
# Use callbacks to share common setup or constraints between actions.
def set_recipe_ingredient
@recipe_ingredient = RecipeIngredient.find(params[:id])
end

# Never trust parameters from the scary internet, only allow the white list through.
def recipe_ingredient_params
params.require(:recipe_ingredient).permit(:item_id, :ingredient_id, :quantity)
end
end


but the URL to new recipe ingredient does not contain the parameter and hence the form as below which has a hidden field (mandatory) of item_id gives error of field being empty.

<%= form_for(recipe_ingredient) do |f| %>
<% if recipe_ingredient.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(recipe_ingredient.errors.count, "error") %> prohibited this recipe_ingredient from being saved:</h2>

<ul>
<% recipe_ingredient.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>

<div class="field">
<%= f.hidden_field :item_id %>
</div>

<div class="field">
<%= f.label :ingredient_id %>
<%= f.collection_select :ingredient_id, Ingredient.all, :id, :ingredient %>
</div>

<div class="field">
<%= f.label :quantity %>
<%= f.text_field :quantity %>
</div>

<div class="actions">
<%= f.submit %>
</div>
<% end %>

Answer

You're not setting the @item_id instance variable that you use here:

<%= link_to 'New Recipe Ingredient', new_recipe_ingredient_path(:item_id => @item_id) %>

In the controller action for that view, you should set the variable:

@item_id = params[:item_id]

Or just use the params directly in the view:

<%= link_to 'New Recipe Ingredient', new_recipe_ingredient_path(item_id: params[:item_id]) %>