Home>
I want to link to the edit page from the edit button in the profile field that is always displayed, but No Method Error occurs.
I know that there is no user_id, but I don't know how to fix it.
NoMethodError in Books # index
Showing /home/ec2-user/environment/bookers-level2.herokuapp/app/views/books/index.html.erb where line # 21 raised:
undefined method `user_id 'for nil: NilClass
Extracted source (around line # 21):
19</table>
20
21<% = link_to edit_user_path (@ user.user_id), class: "btn btn-default" do%>
22<i></i>
23<% end%>
24<p>New book</p>
The following code is likely to be needed.
books/index.html.erb
<% = render 'shared/header'%>
<p>User info</p>
<% = attachment_image_tag @user,: profile_image_id,: fill, 250, 250, format: 'jpeg', fallback: "no_image.jpg", size: '250x250'%>
<table>
<tr>
<Th>name</th>
<Th><% = current_user.name%></th>
</Tr>
<Tr>
<Th>introduction</th>
<Th><% = current_user.introduction%></th>
</Tr>
</table>
<% = link_to edit_user_path (@ user.user_id), class: "btn btn-default" do%>
<i></i>
<% end%>
<p>New book</p>
<% = form_for (@book, url: '/ users') do | f |%>
<p>Title</p>
<% = f.text_field: title%>
<p>Opinion</p>
<% = f.text_area: body%>
<% = f.submit 'Create Book'%>
<% end%>
<p>Books</p>
<table>
<thead>
<tr>
<th>Profile image</th>
<th>Title</th>
<th>Opinion</th>
</tr>
</thead>
<% @ books.each do | book |%>
<tbody>
<tr>
<td></td>
<td><% = book.title%></td>
<td><% = book.body%></td>
</tr>
</tbody>
<% end%>
</table>
<% = render 'shared/footer'%>
books_controller.rb
class BooksController
users_controller.rb
class UsersController
20191003161330_create_books.rb
class CreateBooks
Please let us know if there are any other missing points.
-
Answer # 1
-
Answer # 2
I understand that there is no user_id
No, this is an error because
@user
isnil
. I don't know how@user
is given,Set exactly so that
@user
is notnil
Move even if
Either@user
becomesnil
will be supported.
Related articles
- ruby on rails - i want to eliminate nomethoderror
- ruby on rails - nomethoderror in mypage # profile undefined method `username’ for nil: nilclass
- i want to solve an error on the ruby on rails ec site nomethoderror in cartscontroller # add_item
- ruby - nomethoderror occurs in create action
- ruby - an error occurs when building a rails 6 environment with docker
- ruby - i'm having trouble with rails nomethoderror (i want to get data with each method!)
- ruby on rails 6 - [error] nomethoderror in communities # index undefined method `following?'for nil: nilclass
- ruby on rails 6 - [error] nomethoderror in followscontroller # create undefined method `follows' for nil: nilclass
- ruby on rails - rails6 rspec model run-time error nomethoderror: undefined method `valid?'for nil: nilclass
- ruby on rails - [rails] routing error occurs in devise_token_auth and the registration screen cannot be displayed
- ruby on rails: about nomethoderror that occurred when adding a good function
- ruby on rails 6 - [error] nomethoderror in communitiescontroller # create undefined method `published?'for # <community: 0x0
- ruby on rails - nomethoderror in itemscontroller # new error
- ruby on rails - about nomethoderror in skilscontroller # create
- ruby on rails 6 - [error] nomethoderror in followscontroller # destroy undefined method `destroy'for nil: nilclass
- ruby - rails:loaderror occurs after rails s
- ruby on rails - nomethoderror has occurred in the permit part of the strong parameter, and the cause and solution are unknown
- ruby on rails: nomethoderror that occurred when adding a good function
- ruby on rails - nomethoderror in skils # new
- ruby on rails - rails rspec argumenterror occurs when the mock under test has arguments
Trends
It was possible to solve as.
If edit_user_path had params (id), it seemed to be necessary to pass a variable with id to the argument.