I thought I had defined the method, but I keep getting an error. Should
I just be able to call user.favorite_places? Would appreciate any
help.
favorite_places_controller.rb
class FavoritePlacesController < ApplicationController
before_action :set_place, except: [:index]
before_action :authenticate_user!
def index
@places = User.favorite_places
end
def create
if Favorite.create(favorited: @place, user: current_user)
redirect_to @place, notice: 'Place has been favorited'
else
redirect_to @place, alert: 'Something went wrong. *womp womp*'
end
end
def destroy
Favorite.where(favorited_id: @place.id, user_id:
current_user.id).first.destroy
redirect_to @place, notice: 'Place is no longer a favorite'
end
private
def set_place
@place = Place.find(params[:place_id] || params[:id])
end
end
Favorite.rb
class Favorite < ActiveRecord::Base
belongs_to :user
belongs_to :favorited, polymorphic: true
belongs_to :place
end
user.rb
class User < ActiveRecord::Base
#Relationships
#Favorites
has_many :favorites
has_many :favorite_places, through: :favorites, source: :favorited,
source_type: 'Place'
end
--
Posted via http://www.ruby-forum.com/.
--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/ee0aa10ff885cf51d98b956b1cf47297%40ruby-forum.com.
For more options, visit https://groups.google.com/d/optout.
No comments:
Post a Comment