I am new to Rails and Ruby and actually it's my first web language. This isn't my first project but it's the bigest so far and I am having trouble with a certain page (the "home" page).
The error is:
NoMethodError in Home#index undefined method `avatar' for nil:NilClass
Extracted source (around line #1):
1: <%= image_tag current_user.avatar.url(:thumb) %>
2: <ul>
3: <li><b>Name:</b><%= current_user.name %></li>
4: <li><b>Username:</b><%= current_user.username %></li>
So it doesn't like my attributes :( What should I do in this situation? Where should I look first? Is this a controller problem?
The problem is that current_user is returning nil.
Take a look at the error
NoMethodError in Home#index undefined method `avatar' for nil:NilClass
Ruby is saying that you are trying to execute an instance method avatar on the nil object. After looking at your code, you should be able to tell that you are calling avatar on the return value of current_user so you can deduce that current_user is returning nil instead of the user.
Per the comments:
To solve this you will need to first determine if there is a current_user. Then and only then do you try to display a users avatar or the users other attributes (name, username ...)
Some people do not like logic in the views, I tend to follow this practice.
To avoid this, you could: create a view helper that contains the logic. This helper may or may not display/return data about a user.
Something along the lines of if current_user ...
You are trying to get the avatar for the 'current_user'. The system is complaining because it doesn't know who or what current_user is.
Your current_user object is nil. The code causing the problem (probably form your application or sessions helper) where you are setting the current_user...is most likely where you'll find the root of the problem.
Related
I tried to practice #163 Self-Referential Association using Rails 3.2.21 and work fine almost, but I can only display
http://localhost:3000/users/1
rather than
http://localhost:3000/users/current
in URL address bar. I read and search almost all code, I only find one current:
<%= link_to "View Profile", user_path(:current) %>.
What should I do for this? It is relative to Rails 3? Thanks a lot!
The Railscast is a little unconventional, but the user_path() method, will take in a value (object, string, int, symbol) and call to_param on it. A symbol (in this case :current) will turn into "current" and the url built will be "/users/current". If you pass it a #user (User instance), the to_param method will return a "1" (the id of the object) giving you "/users/1".
I say this code is unconventional because the users_controller#show method doesn't use the ID to find the #user. It just sets it to current_user for convenience. Ryan was not trying to teach this necessarily, more about the restful friendship controller and data-modeling friendships.
Try this
<%= link_to "View Profile", users_path(:current) %>.
I get the following error after putting it on certain pages in Ruby on Rails:
undefined method `name' for nil:NilClass
It also says there is no method error in the page I am on. I know this may be a beginner question but it has been stumping me big time in my project. If you can give me some advice I'd be very happy! Thank you in advanced!
Fist of all: It's a common trap. We all fall into it from time to time.
When ever you read undefined method foobar for nil:NilClass you know that something is nil what shouldn't be nil. So you have to check why #user is nil. Maybe a bug in the controller.
If you want to handle both situations (nil AND User) than you can use this code:
<%= #user.name if #user %>
As I was reading "Ruby on Rails 3 Tutorial", I was trying to figure out what I did wrong in chapter 11 as I couldn't have the test in listing 11.15 to pass.
So here I am, stuck in section 11.2.1. Since I couldn't get anywhere with it, I decided to ignore this and proceed further: to fill up 50 microposts for the first six users and then try to access a user's show page. I got the following error page:
NoMethodError in Users#show
Showing <my_path>/sample_app/app/views/users/show.html.erb where line #10 raised:
undefined method `model_name' for NilClass:Class
Extracted source (around line #10):
7: </h1>
8: <% unless #user.microposts.empty? %>
9: <table class="microposts" summary="User microposts">
10: <%= render #microposts %>
11: </table>
12: <%= will_paginate #microposts %>
13: <% end %>
With trial and errors, it looks like the instance variable #microposts is nil for some reason. It looked as if the method "UsersController.show" didn't do its job of creating that instance variable. However, I could verify that this method is executed since I could manage to acces a user's show page successfully provided that he doesn't have any micropost.
At this step, it's hard to give more hints. But I can tell that all the other tests are passing.
try replacing all your occurrences of #microposts with #user.microposts.
Is #microposts defined in your controller action? Or is #user defined in your controller action? Because it seems like #user.microposts is probably the same thing as #microposts (from this context) and unless you've declared a variable for an associated user.microposts object, you might be trying to access a nonexistent variable.
could it be #user.microposts? The thing that on line 8 it checks for the existence of?
Well, with these types of errors, there's a stupid mistake creeping somewhere.
Following the advice of the ruby.railstutorial.org site. I did compare my code against the one of the author. Guess what, in my version of UsersController, there was two definitions of the 'show' method! And that second instance, at the bottom of the file, didn't have any assignment to the #microposts instance variable.
Would I had to compile the thing, the bug would have been spotted. I guess that there must be a way to scan a rails site against a ruby syntax checker.
Anyway, thanks for the other people suggestions.
I'm trying to create a form on my website but I get this error:
undefined method 'model_name' for NilClass:Class
says the error is on line #33.
On line 33 I've got <%= form_for #try do |f| -%>
A view does not exist in isolation. You need to have your controller set up all the variables that you might need in the view.
So, assuming this is an edit view, you'll need to have code in your controller's edit action, something like this:
def edit
#try = SomeModel.find params[:id]
end
This will set up the #try variable and provide it to the view.
The #try variable must be nil. How is it set? Are you certain it will always contain a valid object?
You need to link your form to a specific model. In your controller, what code is defining #try? Whatever it is seems to be failing to specify a new or current model instance.
If you are using the code in new.html.haml or new.html.erb view file
def new
#try = ModelName.new
end
The same question has been asked many times, but I am unable to find a solution that works for me.
I have three models, posts, users, and picture (paperclip).
I use restful_authentication and set it up so that each user sees only his/her post in posts/index. I've also given him the ability to upload a profile picture.
My question is how do I take the picture/index and show it in post/index. I want to show a thumbnail on top of the post/index page, followed by the posts.
I've used partials, but I get the error
NoMethodError in Posts#index
Showing app/views/us/_picture.html.erb where line #1 raised:
You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.each
Extracted source (around line #1):
1: <% for u in #us %>
2: <tr>
3: <td><%= image_tag u.picture.url(:thumb) %></td>
4: <td><%=h u.name %></td>
I see what it's trying to do, how do I solve it so that it just shows the uploaded picture.
Thanks for your help.
The error is saying that you havent assigned the #us instance variable to the template, which you normally do in the controller.
The template compilation step is having issues at the first iteration step (iterating over the #us array), it hasnt even gotten to the point of trying to run the picture accessing/rendering step, which might or might not have problems of its own.
In your controller grab a set of users (with pictures) and assign it to #us, some basic pseudo-code:
class PostsController < ApplicationController
def index
#us = User.find(:limit => 10, :include => [:picture])
end
end