Home>

I'm making an image posting application like Instagram on Ruby on Rails. So, I want to display the profile image using carrierwave on the home screen after login, but I can't display it because a NoMethod error appears. Users/show.html.erb can display the same code, but static_pages/home.html.erb will cause an error. In the same way, we also implemented image submission, but both pages can be displayed.

Error message
NoMethodError in StaticPages # home
Showing /home/ec2-user/environment/myshoes/app/views/shared/_user_info.html.erb where line # 1 raised:
undefined method `image? 'for nil: NilClass
Applicable source code
<% if @ user.image?%>
">
<% end%>
<% = current_user.name%>
<span><% = link_to "view my profile", current_user%></span>
<span><% = pluralize (current_user.microposts.count, "micropost")%></span>
static_pages_controller
class StaticPagesController</pre>
<strong>users_controller</strong>
<pre><code>class UsersController</pre>
<strong>Home screen</strong>
<pre><code><% if logged_in?%>
  
    
      <section>
        <% = render 'shared/user_info'%>
      </section>
      <section>
        <% = render 'shared/stats'%>
       </section>
      <section>
        <% = render 'shared/micropost_form'%>
      </section>
    </aside>
    
       Micropost Feed 
      <% = render 'shared/feed'%>
    
  
<% else%>
,,,
<% end%>


On the home screen, render is used to call the corresponding source code. In users/show.html.erb, the image is displayed with the same code.
If i look at the questions of people who are suffering from similar errors and try them out, what you see will not work. I would appreciate it if you could tell me. Thank you.

  • Answer # 1

    <% if @ user.image?%>
      ">
    <% end%>


    That means you need to pass@userto the view.
    static_page # homereturns only@micropostand@feed_items, so@user = niland>undefined method 'image?' for nil: NilClass