NoMethodError occurred when adding the good function by the following procedure.
First of all, create a table.
$rails g model like article: references user: references $rails db: migrate
Next, add to app/models/user.rb.
class User</pre> <p><br /> Added to app/models/article.rb.</p> <pre><code>class Article</pre> <p><br /> Then add variations to app/model/like.rb.</p> <pre><code>class Like</pre> <p><br /> Next, define def already_liked ?.</p> <pre><code>class User</pre><strong>-Step2 Controller creation-</strong> <p>First, create a controller.</p> <pre><code data-language = "Cmd">$rails g controller likes
Next, add the routing.
Rails.application.routes.draw do #Change before resources: articles # #After change resources: articles do resources: likes, only: [: create,: destroy] end #
Change the contents of the Like controller.
-Step3 Change view-
class LikesController</pre> <p><br /> We will add content to the show action in articles_controller.rb.</p> <pre><code>def show @article = Article.find (params [: id]) @like = Like.new end
Rewrite show.html.erb as follows.
Shikaku Channel Qualified frequent roads <p><% = @ article.user.email%></p><% = @ article.body%><p><% = @ article.created_at%></p> Number of likes:<% = @ article.likes.count%><% if current_user.already_liked? (@Article)%><% = link_to article_like_path (@article), method:: delete do%> <i></i><% End%><% else%><% = link_to article_likes_path (@article), method:: post do%> <i></i><% End%><% end%>Liked users<% @ article.liked_users.each do | user |%><li><% = user.email%></li><% end%><% = link_to "edit", edit_article_path (@ article.id)%><% = link_to "Return to list", articles_path%>
Add the following to index.html.erb.
(NoMethodError occurs on line 13 according to the error message)
Shikaku Channel Qualified frequent roads Article list<% = link_to "What is a qualified frequency?", Articles_top_path%><% = link_to "to new post", new_article_path%><% @ articles.each do | t |%> <% = t.title%><br> <% = t.content%><br> <% = t.category%><br> <% = t.user.email%><br> <% if current_user.already_liked? (T)%> <% = link_to article_like_path (id: t.id, article_id: t.id), method :: delete do%> <i></i><% = t.likes.count%> <% end%> <% else%> <% = link_to article_likes_path (id: t.id, article_id: t.id), method :: post do%> <i></i><% = t.likes.count%> <% end%> <% end%> <% = t.created_at%><br> <br> <% end%>
Answer # 1
It is an error because current_user is nil.
<% = if current_user.present?&¤t_user.already_liked? (T)%>
If you are using devise, you can use user_signed_in ?.
The fact that there is no data in current_user means
Perhaps current_user contains the logged-in user information, so
I think you're getting an error if you're not logged in.
If you are not logged in, I think that you are not like it, so I made the above answer.
- ruby on rails - about nomethoderror in skilscontroller # create
- ruby on rails - nomethoderror has occurred in the permit part of the strong parameter, and the cause and solution are unknown
- ruby on rails - [rails] about the problem that screen transition is not possible (error does not occur)
- ruby on rails - about switching screen display on multiple models
- ruby - about how to save db with rails devise
- ruby - [rails] about the error that occurs when deleting likes (routing error)
- ruby on rails 6 - i want to solve the error that occurred while implementing the product listing function
- ruby on rails 5 - about lint/syntax error of rubocop
- ruby - about accelerating heroku's rails app
- ruby - about routing using rails collection
- ruby on rails - about the matter that does not transition to the detail page
- ruby on rails - about matters that cannot be migrated
- ruby on rails 6 - rails tutorial chapter 14 about follow, unfollow, following? methods
- about description of manifest file of ruby on rails
- ruby on rails - nomethoderror in skils # new
- ruby on rails - about image distribution on heroku and s3
- ruby - [rails] about nameerror
- ruby - about rails controller new action
- ruby - about embedding rails videos
- ruby on rails - i want to eliminate nomethoderror
- python - you may need to restart the kernel to use updated packages error
- php - coincheck api authentication doesn't work
- php - i would like to introduce the coincheck api so that i can make payments with bitcoin on my ec site
- dart - flutter: the instance member'stars' can't be accessed in an initializer error
- [php] i want to get account information using coincheck api
- the emulator process for avd pixel_2_api_29 was killed occurred when the android studio emulator was started, so i would like to
- sh - 'apt-get' is not recognized as an internal or external command, operable program or batch file
- i want to call a child component method from a parent in vuejs
- i want to check the type of a shell script variable