2016-06-09 52 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