Reputation: 1923
I can't seem to find a solution to this problem. Basically in my show page, I want users who are not signed up to be able to look at the page, while those who have signed up AND are the owner of the show page to be able to edit and delete the post.
in my Journey Controller
before_action :authenticate_user!, except: [:index, :show]
in my Journey Show page
- if user_signed_in?
- if @journey.user_id == current_user.id
%p= link_to "Add a Trip to this Journey", new_journey_trip_path(@journey)
%p= link_to "Add a Travel Time to this Journey", new_journey_travel_path(@journey)
%p= link_to "Add a Shelter to this Journey", new_journey_shelter_path(@journey)
%p= link_to "Home", journeys_path
The error that I've been getting is undefined method `id' for nil:NilClass, and it seems to only show up when I do not log in as any user, and then proceed to visit the journeys show page.
I am assuming that it's because current_user is nil right now, and therefore no id is available for a nil class.
To counter that, I forced an if statement to see if the user is logged in or not, but that does not appear to be working.
What can I do here?
Upvotes: 0
Views: 923
Reputation: 979
-if current_user
-if @journey.user_id == current_user.id
%p= link_to "Add a Trip to this Journey", new_journey_trip_path(@journey)
%p= link_to "Add a Travel Time to this Journey", new_journey_travel_path(@journey)
%p= link_to "Add a Shelter to this Journey", new_journey_shelter_path(@journey)
%p= link_to "Home", journeys_path
Try this. This also works fine
Upvotes: 0
Reputation: 17834
Use this instead
- unless current_user.blank?
- if @journey.user_id == current_user.id
%p= link_to "Add a Trip to this Journey", new_journey_trip_path(@journey)
%p= link_to "Add a Travel Time to this Journey", new_journey_travel_path(@journey)
%p= link_to "Add a Shelter to this Journey", new_journey_shelter_path(@journey)
%p= link_to "Home", journeys_path
Hope this helps!
Upvotes: 1