2016-06-09 54 views
1

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

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

我现在研究了几天,但没有找到适合我的问题的解决方案:我必须在我的代码中更改什么,以便在user_nutrients/new.html.erb用户不必手动输入nutrient_id,但它自动保存用户在nutrients/index.html.erb列表中选择的营养素的ID(通过“添加此”按钮)?每个用户has_many :user_nutrientshas_many :nutrients, through: :user_nutrients。每种营养物质has_many :user_nutrientshas_many :users, through: :user_nutrients

非常感谢。

对不起,如果这个问题太简单或不能完全理解。刚刚开始编码几周前。

营养物/ index.html.erb

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

user_nutrients_controller.rb

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 

_form.html.erb(用于用户营养素)

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

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

    <div class="field"> 
    <%= f.label :nutrient_id %><br> 
    <%= f.number_field :nutrient_id %> 
    </div> 
    <div class="field"> 
    <%= f.label :amount %><br> 
    <%= f.number_field :amount %> 
    </div> 
    <div class="field"> 
    <%= f.label :day %><br> 
    <%= f.date_select :day %> 
    </div> 
    <div class="actions"> 
    <%= f.submit %> 
    </div> 
<% end %> 

回答

1

您想要将nutrient_id传递给UserNutrientsController中的新操作。你可以通过查询字符串传递这样的营养物质#指数:

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 %> 
+0

这太棒了!这样的救济。我正在为此研究日子!多谢! – Oliver