Home>

I want to solve NoMethodError to get&reflect the demo data described in db!
Write the source code of db and in the corresponding directory (blog-app-hirochan) in the terminal

$rails db: seed


Has been executed.

▼ Problem occurring (details of the phenomenon occurring/error message/screenshot)
I have a screenshot.

▼ Source code, log
https://github.com/hirooutdoor/blog-app-hirochan/tree/show-articles

▼ What I tried to solve the problem
Since it was'each'for nil: Nilclass, I suspected that @articles could not be defined properly.

▼ I tried the following and thought about the problem (debugging result)
(1) Confirm that the description of app/views/index.html.erb with the relevant part seems to be correct.


    
        
        List of new articles
        
        
        Timeline
        
    
<% @ articles.each do | article |%>
    
      
      <% = image_tag'eyecatch1.png'%>
      
      
          
        <% = article.title%>
          
          
          <% = image_tag'heart.svg'%>
          <span>23</span>
          
          
          <% = image_tag'default-avatar.png'%>
            
            <p>cohki0305</p>
            <p>2020/4/16</p>
            
          
      
    
<% end%>


(2) Confirm that it can be defined in app/controllers/articles_controller.rb

class ArticlesController</pre>
<p><br />
③ Confirm that the description is as shown in the demo in app/db/seeds.rb</p>
<pre><code>Article.create ({title:'Visioning Power 1', content:' Almost all successful people are doing visioning. The brain can't tell what actually happened from what it imagined. Use this power to get an image of your success anyway.'})
Article.create ({title:'Visioning Power 2', content:'Put a photo of a great man all over the wall. You can check every day if i am approaching that person.'})

④ Confirm that the file of app/model/article.rb is correct.

class Article

Since it was'each'for nil: Nilclass, I suspected that @articles could not be defined properly, but it seems that it can be defined and what is the problem is clear. I would appreciate it if you could teach me>

-Addition-
I confirmed that the seed data can be generated by model using rails console.

Usage environment
Rails 6.0.3.4
VScode 1.49.2

  • Answer # 1

    Clone the questioner's repositoryshow-articlesI tried to run the branch of.
    I was able to display it without any problems.
    Mayberails db: seedMay not be working.
    Please check the contents of the database.

    $sqlite3

    sqlite>.open db/development.sqlite3
    sqlite>select * from articles;
    1 | The Power of Visioning 1 | Almost all successful people are doing visioning. The brain cannot tell what actually happened and what it imagined. Use this power to get an image of your success anyway. 2020-10-31 00: 12: 52.379845 | 2020-10-31 00: 12: 52.379845
    2 | The power of visioning 2 | Put a photo of a great man on the wall. You can check daily to see if you are approaching the person. 2020-10-31 00: 12: 52.384367 | 2020-10-31 00: 12: 52.384367

    If all goes well, you should see the contents of db: seed as above

Related articles