在Rails

时间:2016-06-09 19:13:49

标签: ruby-on-rails ruby

我正在构建一个应用程序,用户可以从营养列表中进行选择,在其个人食物日记中添加一些营养素。因此,我创建了一个user_nutrients表,其中包含user_id,nutrient_id和用户必须输入的其他信息。

在列表中的每个营养素项旁边,我有一个按钮(“添加此项”),用户将用户重定向到我user_nutrients_controller.rb的新操作。到现在为止还挺好。

我现在研究了几天,但没有找到合适的解决方案来解决我的问题:我需要在代码中进行哪些更改,以便在user_nutrients/new.html.erb中用户无需键入{{ 1}}手动,但它会自动保存用户之前在nutrient_id列表中选择的营养素的ID(通过“添加此”按钮)?

每位用户nutrients/index.html.erbhas_many :user_nutrients。每种营养素has_many :nutrients, through: :user_nutrientshas_many :user_nutrients

非常感谢。

很抱歉,如果这个问题太简单或不完全可以理解。刚开始编码几周前。

营养物/ index.html.erb

has_many :users, through: :user_nutrients

user_nutrients_controller.rb

<% @nutrients.each do |nutrient| %>
    <tr>
      <td><%= nutrient.id %></td>
      <td><%= link_to nutrient.name, nutrient, class: "link" %></td>
      <td class="hidden-xs"><%= nutrient.kcal %></td>
      <td class="hidden-xs"><%= nutrient.fat %></td>
      <td class="hidden-xs"><%= nutrient.carbs %></td>
      <td class="hidden-xs"><%= nutrient.sugar %></td>
      <td class="hidden-xs"><%= nutrient.protein %></td>
      <td class="hidden-xs"><%= nutrient.gram %></td>
      <td class="hidden-xs"><%= nutrient.points %></td>

      <td>
        <div class="dropdown">
          <button class="btn btn-default dropdown-toggle btn-xs" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">
            Actions
            <span class="caret"></span>
          </button>
          <ul class="dropdown-menu dropdown-menu-right" aria-labelledby="dropdownMenu1">
            <li><%= link_to 'Edit', edit_nutrient_path(nutrient) %></li>
            <li role="separator" class="divider"></li>
            <li><%= link_to 'Add this', new_user_nutrient_path %></li>
            <li role="separator" class="divider"></li>
            <li><%= link_to 'Delete', nutrient_path(nutrient), method: :delete, data: {confirm: "Are you sure?"} %></li>

_form.html.erb(针对用户营养素)

class UserNutrientsController < ApplicationController
  before_action :set_user_nutrient, only: [:show, :edit, :update, :destroy]

  # GET /user_nutrients
  # GET /user_nutrients.json
  def index
    @user_nutrients = UserNutrient.where(:user_id => current_user.id).order(day: :desc)
  end

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

  # GET /user_nutrients/new
  def new
    @user_nutrient = UserNutrient.new
  end

  # GET /user_nutrients/1/edit
  def edit
  end

  # POST /user_nutrients
  # POST /user_nutrients.json
  def create
    @user_nutrient = UserNutrient.new(user_nutrient_params)
    @user_nutrient.user = current_user
    respond_to do |format|
      if @user_nutrient.save
        format.html { redirect_to user_nutrients_path, notice: 'User nutrient was successfully created.' }
        format.json { render :show, status: :created, location: @user_nutrient }
      else
        format.html { render :new }
        format.json { render json: @user_nutrient.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /user_nutrients/1
  # PATCH/PUT /user_nutrients/1.json
  def update
    respond_to do |format|
      if @user_nutrient.update(user_nutrient_params)
        format.html { redirect_to @user_nutrient, notice: 'User nutrient was successfully updated.' }
        format.json { render :show, status: :ok, location: @user_nutrient }
      else
        format.html { render :edit }
        format.json { render json: @user_nutrient.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /user_nutrients/1
  # DELETE /user_nutrients/1.json
  def destroy
    @user_nutrient.destroy
    respond_to do |format|
      format.html { redirect_to user_nutrients_url, notice: 'User nutrient was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

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

    # Never trust parameters from the scary internet, only allow the white list through.
    def user_nutrient_params
      params.require(:user_nutrient).permit(:user_id, :nutrient_id, :amount, :day)
    end
end

1 个答案:

答案 0 :(得分:1)

您希望将nutrient_id传递给UserNutrientsController中的新操作。你可以通过这样的querystring传递它在营养素#index:

link_to 'Add this', new_user_nutrient_path(nutrient_id: nutrient.id)

new操作如下所示:

def new
  @nutrient = Nutrient.find(params[:nutrient_id])
  @user_nutrient = UserNutrient.new(nutrient_id: @nutrient.id)
end

表单看起来像这样:

<h2>Add Nutrient <%= @nutrient.name %></h2>

<%= form_for(@user_nutrient) do |f| %>

  # get rid of number_field for nutrient_id and use a hidden field
  <%= f.hidden_field :nutrient_id %>