ruby - Rails: Set up favorite recipe for user but keep getting same error -


as user want able add recipe favorites. unfortunately when try add recipe favorites following error: recipe(#69883866963220) expected, got nilclass(#46922250887180).

i followed 'tutorial' guideline

somehow not able add user's favorites. when use rails c , type in user.find(1).favorites, returns me empty array.

who can me solve issue? thank in advance!

my models:

class favoriterecipe < activerecord::base   belongs_to :recipe   belongs_to :user end 

class user < activerecord::base   has_many :recipes    # favorite recipes of user   has_many :favorite_recipes # 'relationships'   has_many :favorites, through: :favorite_recipes, source: :recipe # actual recipes user favorites end  class recipe < activerecord::base   belongs_to :user    # favorited users   has_many :favorite_recipes # 'relationships'   has_many :favorited_by, through: :favorite_recipes, source: :user # actual users favoriting recipe end 

my recipecontroller.rb:

def show     @review = review.new     @recipe = recipe.find(params[:id])     @user = user.find(@recipe.user_id)     @full_name = @recipe.user.first_name + " " + @recipe.user.last_name     # @reviews = @recipe.reviews.page(params[:page]).order('created_at desc')   end    # add , remove favorite recipes   # current_user   def favorite     type = params[:type]     if type == "favorite"       current_user.favorites << @recipe       redirect_to :back, notice: 'you favorited #{@recipe.name}'      elsif type == "unfavorite"       current_user.favorites.delete(@recipe)       redirect_to :back, notice: 'unfavorited #{@recipe.name}'      else       # type missing, nothing happens       redirect_to :back, notice: 'nothing happened.'     end   end 

routes:

resources :recipes, only: [:index, :show]     put :favorite, on: :member end 

my view: app/views/recipes/show.html.erb

  <% if current_user.favorites.exists?(id: @recipe.id) %>     <%= link_to favorite_recipe_path(@recipe, type: "unfavorite"), method: :put %>       <ul class="list-inline product-controls">         <li><i class="fa fa-heart"></i></li>       </ul>     <% end %>   <% else %>     <%= link_to favorite_recipe_path(@recipe, type: "favorite"), method: :put %>       <ul class="list-inline product-controls">         <li><i class="fa fa-heart"></i></li>       </ul>     <% end %>  <% end %> 

according relationship need recipe object assign user. need first find object , assign user.

def favorite     @recipe = recipe.find(params[:id])     type = params[:type]     if type == "favorite"       current_user.favorites << @recipe       redirect_to :back, notice: 'you favorited #{@recipe.name}'      elsif type == "unfavorite"       current_user.favorites.delete(@recipe)       redirect_to :back, notice: 'unfavorited #{@recipe.name}'      else       # type missing, nothing happens       redirect_to :back, notice: 'nothing happened.'     end end 

Comments

Popular posts from this blog

PySide and Qt Properties: Connecting signals from Python to QML -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

scala - 'wrong top statement declaration' when using slick in IntelliJ -