将link_to中收到的参数传递给控制器​​中的不同操作

时间:2016-10-19 14:26:41

标签: ruby-on-rails ruby

我有一个项目模型如下。

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

和RecipeIngredient模型如下

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

从项目的索引视图中,我将item_id传递给recipe_ingredients的索引视图,如下所示

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

并且recipe_ingredients的索引视图仅显示属于具有在URL中接收的item_id的项目的那些成分。为此,RecipeIngredient的控制器是这样的。

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

现在我正在尝试将相同的item_id从食谱成分的索引页面传递给新配方成分表格。

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

以下是配方成分的整个控制器文件,包括新的。

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

但是新配方成分的URL不包含参数,因此下面的表格有一个隐藏字段(必填)item_id会给出字段为空的错误。

<%= 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 %>

1 个答案:

答案 0 :(得分:1)

您没有设置此处使用的@item_id实例变量:

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

在该视图的控制器操作中,您应该设置变量:

@item_id = params[:item_id]

或者直接在视图中使用参数:

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