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):
21<% = link_to edit_user_path (@ user.user_id), class: "btn btn-default" do%>
23<% end%>
24<p>New book</p>

The following code is likely to be needed.


<% = 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'%>
<Th><% = current_user.name%></th>
<Th><% = current_user.introduction%></th>
      <% = link_to edit_user_path (@ user.user_id), class: "btn btn-default" do%>
      <% end%>
      <p>New book</p>
      <% = form_for (@book, url: '/ users') do | f |%>
        <% = f.text_field: title%>
        <% = f.text_area: body%>
        <% = f.submit 'Create Book'%>
      <% end%>
            <th>Profile image</th>
        <% @ books.each do | book |%>
            <td><% = book.title%></td>
            <td><% = book.body%></td>
        <% end%>

<% = render 'shared/footer'%>


class BooksController


class UsersController


class CreateBooks

Please let us know if there are any other missing points.

  • Answer # 1

    link_to edit_user_path (@user)

    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.

  • Answer # 2


    I understand that there is no user_id

    No, this is an error because@userisnil. I don't know how@useris given,

    Set exactly so that@useris notnil

    Move even if@userbecomesnil


    will be supported.