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

    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

    Either

    will be supported.