I am creating a posting application with Ruby on Rails.
I want to be able to display the posting screen list even when logging out.
When i am logged in after implementing the Like function, the post list screen is displayed, but if you log out and press the link on the post list screen, an error will occur.
Applicable source code
NoMethodError in Tweets#index undefined method `already_favorited?' for nil:NilClass
#Posting list screen code .TweetMain .TweetMain__form [email protected] do |tweet| .TweetMain__menu .TweetMain__body = link_to user_path(tweet.user) do = attachment_image_tag tweet.user, :profile_image, fallback: "no-image.png", class: "Tweetshow__image" .TweetMain__body__name = link_to user_path(tweet.user), class: "TweetMain__body__Name" do = tweet.user.username .TweetMain__body__date = tweet.updated_at.strftime("%Y-%m-%d %H:%M") .TweetMain__title = link_to tweet_path(tweet), class: "TweetMain__Title" do = tweet.title .TweetMain__favorite -if current_user.already_favorited?(tweet) ← error is displayed here = link_to tweet_favorites_path(tweet), method: :delete, class: "Favorite" do = icon('fas','heart') -else = link_to tweet_favorites_path(tweet), method: :post, class: "Favorite" do = icon('far','heart') = tweet.favorites.count
What I tried
I thought that the cause was already_favorited?(tweet), so I made the same description in tweet.rb.
Didn't solve it.
Answer # 1
-if current_user.already_favorited?(tweet) ← error is displayed here
current_user is nil.
before_action :authenticate_user!, except: [:index]
Probably because authenticate excludes authenticate.
- ruby on rails - nomethoderror has occurred in the permit part of the strong parameter, and the cause and solution are unknown
- ruby - an error occurred when implementing the micropost search function using ransack
- ruby on rails: about nomethoderror that occurred when adding a good function
- ruby on rails: nomethoderror that occurred when adding a good function
- ruby - i want to implement the "like" function, but i get "nomethoderror in messages # index" in the view fi
- ruby on rails 6 - after updating, it becomes no route matches [post] "/ posts/2/edit"
- error when implementing search function using ransack in ruby on rails
- ruby - nomethoderror: undefined method `new'for: uglifier: symbol error on heroku deployment
- ruby on rails - rails not updated after editing update
- ruby - about errors after deploying heroku
- ruby - rails:loaderror occurs after rails s
- ruby - i've separated the methods, but it doesn't work nomethoderror (undefined method `+'for nil: nilclass)
- i want to solve an error on the ruby on rails ec site nomethoderror in cartscontroller # add_item
- ruby - i want to establish the pagination function after separating the rails method
- ruby - i got a: id => nil error while implementing the user removal feature, so i'd like to ask for a solution
- ruby on rails - an error has occurred, all later migrations canceled: mysql2 :: error: table'likes' already exists
- ruby - improvement of nomethoderror undefined method `paginate'
- ruby on rails 5 - after overwriting the image under assets, nothing is displayed
- ruby - when saving the value after submitting
- ruby - i get an error saying undefined method `resize_to_limit'for
- html - i want to set the conditions to be displayed in collection_check_boxes
- ruby - i get an error in the view while implementing carriewave
- ruby - passing variables using render partial
- ruby - [rails] i want to deploy a rails application using elastic beanstalk
- [ruby on rails] i want to display a parent model that matches the information of the associated child model
- ruby - i want to solve the n + 1 problem when using rails where method in loop processing
- rails application css is not reflected
- ruby - [rails] form_with doesn't have proper name attribute
- ruby - rails cannot be started in the production environment when building an ec2 environment