I have the following code in my events show controller:
def show
@event = Event.find_by_name(params[:id])
if request.path != event_path(@event)
redirect_to @event, status: :moved_permanently
end
if @event.videos.present?
@video = @event.videos.find(params[:video]) || @event.videos.first
if :video_id.present? && current_user && @video.premium?
@order = Order.new(user_id: current_user.id, video_id: @video.id, price: @video.price)
elsif :event_id.present? && current_user && @event.premium?
@order = Order.new(user_id: current_user.id, event_id: @event.id, price: @event.price)
end
end
@user = User.new
end
This line:
@video = @event.videos.find(params[:video]) || @event.videos.first
Should find the video if it has been passed on ID into the params, such as by this link:
event_path(video.event.name, video: video)
When a video is passed into the params, the app works fine and the correct video is shown in the correct event.
However when a video ID isn't passed into the params, I get the following error:
Couldn't find Video without an ID
I thought that the || operator would skip past the @event.videos.find(params[:video] part and just pick the first videos associated with the event to display, but clearly this is no longer happening, and I think the problem has been introduced since adding friendly_id to videos, although I can't say for sure.
Videos belong to Events, and an Event has many Videos.
Can anyone help show me how I can have @video show the clicked video when the params are passed into it, and the first video belonging to the event if there are no params passed?