2016-04-14 8 views
1

埋め込み関係の中でhas_one関係に問題があります。関係は、レシピembeds_many ilist、ilist has_one成分です。私はこのために単一のフォームを使用していますが、私が提出する際にはilistに保存されていません。Rails 4とMongoidのネストされたhas_one関係は機能しませんか?

レシピモデル

class Recipe 
include Mongoid::Document 
. 
. 
    embeds_many :ilists 
    accepts_nested_attributes_for :ilists, 
    :allow_destroy => true, 
    :reject_if  => :all_blank, 
    autosave: true 
end 

レシピコントローラ

class RecipesController < ApplicationController 
    before_action :set_recipe, only: [:show, :edit, :update, :destroy] 

    # GET /recipes 
    # GET /recipes.json 
    def index 
    @recipes = Recipe.all 
    end 

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

    # GET /recipes/new 
    def new 
    @recipe = Recipe.new 
    3.times { @recipe.ilists.build } 
    end 

    # GET /recipes/1/edit 
    def edit 
    end 

    # POST /recipes 
    # POST /recipes.json 
    def create 
    @recipe = Recipe.new(recipe_params) 

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

    end 

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

    end 

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

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

    # Never trust parameters from the scary internet, only allow the white list through. 
    def recipe_params 
     params.require(:recipe).permit(:title, :photo, :type, :preptime, :serves, :description, :calories, :protien, :Fat, :cholesterol, :sodium, :potassium, :carbohydrate, :fiber, :sugar, :calcium, :iron, :zinc, :copper, :choline, :fluoride, :folate, :magnesium, :manganese, :phosphorus, :potassium, :selenium, :vitaminA, :vitaminB1, :vitaminB2, :vitaminB3, :vitaminB4, :vitaminB5, :vitaminB6, :vitaminB12, :vitaminC, :vitaminD, :vitaminE, :vitaminK, :vegetarian, :lactovegetarian, :vegan, :halal, :pescetarian, :glutenfree, :alcohol, ilists_attributes: [ :ingrediant, :quantity]) 
    end 
end 

のIListモデル

class Ilist 
    include Mongoid::Document 
    field :quantity, type: Integer 

    has_one :ingrediant 
    accepts_nested_attributes_for :ingrediant, 
    :allow_destroy => true, 
    :reject_if  => :all_blank, 
    autosave: true 
    embedded_in :recipe, inverse_of: :ilists 
end 
**ilist controller params** 
params.require(:ilist).permit(ingrediant_attribute: [ :name, :calories,..], :quantity) 

Ingrediantモデル(私は成分間違った綴り知る)

class Ingrediant 
include Mongoid::Document 
field :name, type: String 
field :calories, type: BigDecimal 
field :protien, type: BigDecimal 
. 
. 

belongs_to :ilist 
end 

形式:コンソールでのHTTPポストで

<%= form_for @recipe, :html => { :multipart => true } do |f| %> 
. 
<%= f.fields_for :ilists do |builder| %> 
    <tr> 

    <td><%= builder.collection_select :ingrediant, Ingrediant.all, :id, :name, {} %></td> 
    <td><%= builder.text_field :quantity %></td> 

    </tr> 
<% end %> 

私は

"..,ilists_attributes"=>{"0"=>{"ingrediant"=>"56ccc8b7de301b1904488361", "quantity"=>"100"},.. 

56ccc8b7de301b1904488361は、成分データベース内の鶏の胸肉のための_idであるとともにレシピの投稿を見ることができる、私はしないでくださいちょうど_id私は全体の成分を照会できるようにする必要があります。

アイデアには成分のすべての情報と量が含まれているので、作成時にコントローラを介してレシピの栄養価を計算することができます(これを行う方法はわかりませんが一度に1つの問題)。

答えて

0

ilistを含むレシピ内にilistを埋め込んでいますが、ilistに材料を埋め込んでいないため、埋め込みilistsで関連するレシピ&という2つのドキュメントコレクションがあります。コンポーネントを取得するには、recipe.ilist.ingrediantを呼び出してドキュメントを返す必要があります。これは別のものですが、関連文書であるmongoidはilist内に_idだけを格納します。

あなたの機会があれば、コレクションのスペルを更新するメンテナンス上の理由からお勧めします。

関連する問題