2016-06-13 19 views
1

In der Woche 3 von Mackenziechild-Rezept-Box, habe ich einige Probleme mit Cocoon. Ich habe Gerät auch installiert, und meine ingredients und directions Attribute werden nicht gespeichert, wenn ich ein neues Recipe erstellen. Aber wenn ich ein existierendes Recipe aktualisiere, ist alles in Ordnung. Die Fehlermeldung lautet:Cocoon Juwel Attribute nicht speichern, wenn neue Rezept erstellen

Rezept Zutaten müssen vorhanden sein, Rezept Richtungen müssen

existieren

Was mache ich falsch? Ich bin mit Schienen 5

app/models/recipe.rb

class Recipe < ApplicationRecord 
    validates :title, :description, :image, presence: true 
    has_attached_file :image, styles: { medium: "400x400#" } 
    validates_attachment_content_type :image, content_type: /\Aimage\/.*\Z/ 
    belongs_to :user 
    has_many :ingredients 
    has_many :directions 
    accepts_nested_attributes_for :ingredients, :reject_if => :all_blank, :allow_destroy => true 
    accepts_nested_attributes_for :directions, :reject_if => :all_blank, :allow_destroy => true 
end 

app/controllers/recipes_controller.rb

def new 
    @recipe = Recipe.new 
    @recipe = current_user.recipes.build 
end 

def create 
    @recipe = Recipe.new(recipe_params) 
    @recipe = current_user.recipes.build(recipe_params) 
    if @recipe.save 
    # show a success flash message and redirect to the recipe show page 
    flash[:success] = 'New recipe created successfully' 
    redirect_to @recipe 
    else 
    # show fail flash message and render to new page for another shot at creating a recipe 
    flash[:danger] = 'New recipe failed to save, try again' 
    render 'new' 
    end 
end 

def update 
    if @recipe.update(recipe_params) 
    # display a success flash and redirect to recipe show page 
    flash[:success] = 'Recipe updated successfully' 
    redirect_to @recipe 
    else 
    # display an alert flash and remain on edit page 
    flash[:danger] = 'Recipe failed to update, try again' 
    render 'edit' 
    end 
end 

private 

def recipe_params 
    params.require(:recipe).permit(:title, :description, :image, 
    directions_attributes: [:id, :step, :_destroy], 
    ingredients_attributes: [:id, :name, :_destroy]) 
end 

def recipe_link 
    @recipe = Recipe.find(params[:id]) 
end 

app/views/Rezepte/_ingredient_fields. html.haml teilweise

.form-inline.clearfix 
.nested-fields 
    = f.input :name, input_html: { class: 'form-input form-control' } 
    = link_to_remove_association 'Remove', f, class: 'form-button btn btn-default' 

app/views/Rezepte/_direction_fields.html.haml Teil

.form-inline.clearfix 
.nested-fields 
    = f.input :step, input_html: { class: 'form-input form-control' } 
    = link_to_remove_association 'Remove', f, class: 'form-button btn btn-default' 
+0

Auf welcher Linie erhalten Sie diesen Fehler? Hat das Hinzufügen der "inverse_of" tatsächlich etwas gelöst (weil ich dazu noch nie etwas getan habe). In deinem create erstellst du das @rezept zweimal -> hast du das bemerkt? – nathanvda

Antwort

5

Aber wenn ich eine vorhandene Recipe aktualisieren, alles ist in Ordnung.

Das ist Ihre Antwort. Wenn Sie ein neues Rezept erstellen, haben Sie das Rezeptobjekt nicht, da es sich im Serverspeicher befindet. Beim Aktualisieren wird das Rezeptobjekt jedoch beibehalten.

Deshalb erhalten Sie die Ingredients recipe must exist und directions recipe must exist Fehler.

Um das zu beheben, müssen Sie die inverse_of in den Verknüpfungen hinzufügen.

class Recipe 
    has_many :ingredients, inverse_of: :recipe 
    has_many :directions, inverse_of: :recipe 

class Ingredient 
    belongs_to :recipe, inverse_of: :ingredients 

class Direction 
    belongs_to :recipe, inverse_of: :directions 

Wenn Sie die inverse_of umfassen, Rails weiß jetzt über die Verbände und wird „Spiel“ im Speicher ab.

Mehr über inverse_of:

+2

Diese Arbeit, danke. Ich muss in die inverse_of tauchen – Brioproject