Implement "add to favorites" in Rails 5

1

I am new to Rails and I need your help to implement a functionality in my web app. It is a web app of recipes and the functionality in question is "Add recipe to Favorites". I call the "User" "Chef". I want a chef to be able to save recipes from other chefs.

In the models I have the following associations:

chef.rb:

has_many :favorites
has_many :recipes, through: :favorites

recipe.rb:

has_many :favorites
has_many :chefs, through: :favorites

favorite.rb:

class Favorite < ApplicationRecord
  belongs_to :chef
  belongs_to :recipe
end

In the migration file I created a favorite table:

t.belongs_to :chef
t.belongs_to :recipe
t.timestamps

In routes.rb:

get 'my_favorites', to: 'chefs#my_favorites'
resources :favorites
post 'add_recipe', to: 'recipes#add_recipe'

In RecipesController I have defined:

def my_favorites
 @favorites = current_chef.recipes
end

def add_recipe
  @recipe = Recipe.find(params[:id]) 
  current_chef.favorites.build(recipe_id: @recipe.id)

  if @recipe.save
     redirect_to my_favorites_path, notice: "Favorite recipe was 
                                           successfully added"
  else
     redirect_to my_favorites_path, flash[:error] = "There was an error 
                                     with adding recipe as favorite"
  end
end

In views:

H created an "add as my favorite" button link_to in the views / recipes / show.html.erb file. But when I click on it from the browser it gives me this error: 'Could not find Recipe without an ID'

<% if logged_in? %>
  <% if current_chef.not_favorites_with?(@recipe) %>
    <%= link_to "Add as my favorite", add_recipe_path(chef: 
                                  current_chef, recipe: @chef), 
                                  class: "btn btn-xs btn-success", 
                                  method: :post %>
  <% else %>
    <span class="label label-primary">
      It's already your favorite recipe
    </span>
  <% end %>
<% end %>

This is what happens on the server:

Processing by RecipesController#add_recipe as HTML
  Parameters: {"authenticity_token"=>"Pz5C/yK0mP5QtONHJs83fhxcrQ6Alvbp2qpPrVOiKdBKyIUyspww/7L8S66lcOmFGWZr8Lq1ka1rt2D4FbY8NQ==", "chef"=>"9"}
Chef Load (0.3ms)  SELECT  "chefs".* FROM "chefs" WHERE "chefs"."id" = ? ORDER BY "chefs"."created_at" DESC LIMIT ?  [["id", 9], ["LIMIT", 1]]
Completed 404 Not Found in 3ms (ActiveRecord: 0.3ms)  

ActiveRecord::RecordNotFound (Couldn't find Recipe without an ID):

I do not know why you can not find the "recipe's ID". Please, help.

link

    
asked by ordinaryman 20.04.2017 в 21:49
source

1 answer

1
  

link_to "Add as my favorite", add_recipe_path (chef:                                     current_chef, recipe: @chef),                                     class: "btn btn-xs btn-success",                                     method:: post

The action recipes # add_recipe is waiting for a parameter with key: id (the recipe id) and you are not sending it. You send a key: recipe with the chef instead of the recipe. Then in the controller, when doing Recipe.find (params [: id]), params [: id] returns nil and the exception is thrown.

The solution is to always send the object of the controller first (in this case recipe) and then the additional parameters:

Try modifying the link_to to

link_to "Add as my favorite", add_recipe_path(@recipe, chef: current_chef), class: "btn btn-xs btn-success", method: :post
    
answered by 21.04.2017 в 17:17