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
Applicable source code
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
<% if @ user.image?%> "> <% end%> <% = current_user.name%> <span><% = link_to "view my profile", current_user%></span> <span><% = pluralize (current_user.microposts.count, "micropost")%></span>
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
@user = niland
>undefined method 'image?' for nil: NilClass
- ruby on rails - error when vagrant up
- error in building environment of ruby on rails
- ruby on rails - when i run rails, i get an error related to webpacker
- ruby on rails 6 - i want to solve the error that occurred while implementing the product listing function
- ruby - an error occurs when building a rails 6 environment with docker
- ruby on rails - i don't understand the content of the error
- ruby on rails - error when implementing rails6 follow function
- ruby on rails - unknown action error when trying to post a comment
- ruby on rails - syntax error cannot be resolved
- ruby on rails 6 - [error] uninitialized constant user (call'userconnection' to establish a connection) :: image
- ruby on rails 6 - when i press new user registration, i get an error of no route matches [get] "/ users"
- ruby on rails - i get an error without being created
- ruby on rails - rails routing error
- ruby on rails - nomethoderror in itemscontroller # new error
- ruby on rails 5 - i want to get an error message when i enter the user id in full-width in url
- ruby on rails 6 - [error] nomethoderror in communitiescontroller # create undefined method `published?'for # <community: 0x0
- ruby on rails - rspec validation error
- solution for ruby on rails routes error
- ruby on rails - i get a template is missing error in render
- ruby on rails 6 - about rails error "wrong number of arguments (given 1, expected 0)"