Home>
I want to display the edit button and delete button in index.html

When the user login is completed, all the posted contents will be displayed on the top page.
Although it is a function, I want to display the edit button and the delete button together.

NameError in Homes # index
Showing /Users/taigasoma/projects/meiwa/app/views/homes/index.html.erb where line # 6 raised:
undefined local variable or method `home'for #<#<Class: 0x00007f931c445c08>: 0x00007f931c4442b8>
Did you mean? @homes
Corresponding source code
<% = render "shared/header"%>
<% if user_signed_in?%>
    
    <ul>
      <li>
        <% = link_to'Edit product', edit_home_path (home.id), method:: get, class: "home-red-btn"%>
      </li>
      <li>
        <% = link_to'Delete', home_path (home.id), method:: delete, class:'home-destroy'%>
      </li>
    </ul>
    
Omitted below
Rails.application.routes.draw do
  devise_for: users
  root to:'homes # index'
  resources: homes do
  end
end
class HomesController</pre>
<strong>What I tried</strong>
<p>This time, it is not a transition to show action, so add index to before_action<br />
Error occurred</p>
<pre><code data-language = "error">couldn't find home without an id

It seems that home.id is not entered properly.

If i have any advice, thank you.

Postscript

home.id


To

@ home.id


After rewriting with

NoMethodError in Homes # index
Showing /Users/taigasoma/projects/meiwa/app/views/homes/index.html.erb where line # 6 raised:
undefined method `id'for nil: NilClass


This error has occurred.

  • Answer # 1

    If the controller method refers to the same view, if one is executed, the other will not be executed, so the variable that is not executed is not passed to the view = nil.
    I think that will be the case.
    Depending on the requirements, such as sending the initial value for some reason or changing the view.

  • Answer # 2

    Edit indexHTML

    <% = render "shared/header"%>
    <% if user_signed_in?%>
          
          <p>
            <% = link_to'Edit product', edit_home_path (home.id), method:: get, class: "home-red-btn"%>
          </p>
          <p>
            <% = link_to'Delete', home_path (home.id), method:: delete, class:'home-destroy'%>
          </p>
          
        
        
      <% @ homes.each do | home |%>
          
          <% = home.buildingname%>
          
          
          <% = image_tag home.images [0], class: "home-box-img"%>
          
        
      <table>
        <tbody>
    Omitted below

    In the omitted part, I wrote the code in each and sent the home information.
    So, put in each.

    Omitted above
    </table>
        
          
          <p>
            <% = link_to'Edit product', edit_home_path (home.id), method:: get, class: "home-red-btn"%>
          </p>
          <p>
            <% = link_to'Delete', home_path (home.id), method:: delete, class:'home-destroy'%>
          </p>
          
        
    <% end%>
    <% end%>
    <% = render "shared/footer"%>

    I was able to display it here.
    Thanks to the advice of those who commented.
    Thank you very much.

Related articles