0

I am having an issue with my update method in my recipes controller where I cannot create a new category. Category is a nested resource within recipes. This is my update method

def update    
    if current_user.id != @recipe.category.user_id
      flash.now[:notice] = "You cannot update a recipe that you did not author."
      redirect_to recipes_path
    else
      @recipe.update(recipe_params)
      flash.now[:notice] = "#{@recipe.title} has been updated."
      redirect_to recipe_path
    end
    if @category
      recipe.build_category
    end
  end

I just recently added the if @category recipe.build_category but that didn’t do anything.

This is my recipe strong params:

def recipe_params
    params.require(:recipe).permit(
    :title, 
    :description,
    :category_id,
    category_attributes: [:name],
    instructions_attributes: [:id,
      :step, :_destroy
      ]    
  )
  end

And my recipes controller before actions:

before_action :redirect_if_not_logged_in
  before_action :find_recipe, only: [:show, :edit, :update, :destroy]
  before_action :find_category, only: [:index, :new, :create]

I also have this language in my recipes _form:

    <%= f.fields_for :category, recipe.build_category do |cb| %>
      <div style="margin-left:30px">
        <%= cb.label :name %>
        <%= cb.text_field :name %>
      </div>
    <% end %>

and then this in my recipes edit.html.erb

<%= render partial: "form", locals: {recipe: @recipe, category: @category, button_name: "Update Recipe"}%>

These are my recipes & categories routes:

    resources :recipes, only: [:index, :show, :new]
  end

  resources :recipes do
    resources :categories, only: [:index, :new, :create]
  end  


  resources :users, only: [:show, :new] do
    resources :recipes, only: [:index]
  end

This is my recipes model:


  belongs_to :category
  accepts_nested_attributes_for :category, reject_if: :all_blank

  has_many :instructions, dependent: :destroy
  accepts_nested_attributes_for :instructions, reject_if: :all_blank, allow_destroy: true 


  validates :title, presence: true
  validates :instructions, presence: true
  validates :description, presence: true
end

Does anyone know why I wouldn’t be able to create the category within an existing recipe? **Side note - my flash errors are also not working. Thanks in advance for any help.

  • 2
    `Cannot create`, `didn't do anything`, `are also not working` are not clear. You have to be more specific. What are the exact errors you have? what does `not working` mean in your case? what's the expected and actual result? when you say `cannot create`, do you have an error? it's not even trying to create the object? you can try to create/edit a recipe using the console with a similar parameters hash to have more control and check the errors – arieljuod Feb 13 '21 at 05:17
  • As mentioned by @arieljuod, your question is not clear enough. But a question `if @category; recipe.build_category end` what is this supposed to do? Don't we need to build or initiate any child in edit method, rather than on update. Confused.. – ajay dhakal Feb 13 '21 at 10:29

0 Answers0