New to rails. Following a tutorial on polymorphic associations, I bump into this to set @client in create and destroy.
@client = Client.find(params[:client_id] || params[:id])
I'm normally only used to that you can only find @client = Client.find(params[:id])
so how does this work with there being two params? How does the || work?
FavoriteClientsController.rb:
class FavoriteClientsController < ApplicationController
def create
@client = Client.find(params[:client_id] || params[:id])
if Favorite.create(favorited: @client, user: current_user)
redirect_to @client, notice: 'Leverandøren er tilføjet til favoritter'
else
redirect_to @client, alert: 'Noget gik galt...*sad panda*'
end
end
def destroy
@client = Client.find(params[:client_id] || params[:id])
Favorite.where(favorited_id: @client.id, user_id: current_user.id).first.destroy
redirect_to @client, notice: 'Leverandøren er nu fjernet fra favoritter'
end
end
Full code for controller, models can be seen here
Using rails 5