I have a rails app whereby you can edit your account information. When I click the edit my information button i receive the following error when I click the link.
NoMethodError in Users#edit – undefined method `name’ for
nil:NilClass
Which is pointing to the following line of my users/_form.html.erb
<% else %> <td><%= @user.admin_level.name %>
My full trace follows: Pastie
It appears something is wrong with user.admin_level.name What I cannot seem to understand is why this error is occurring also the attribute name exist in the admin_level entity.
My users_controller has the following def edit:
# GET /users/:id/edit
def edit
if @user
respond_to do |format|
format.html # edit.html.erb
end
else
redirect_to routing_error_path
end
end
Its seems that I am missing something, do I need to define the name in my users_controller?
It simply means that
<%= @user.admin_level %>isnil. This is maybe because the user has no admin_level assigned. Therefore, calling.nameon nil which results in a noMethodError.Correct would be
<%= @user.admin_level.name if @user.admin_level %>, or maybe<%= @user.admin_level.try(:name) %>(which both only display something ifadmin_levelis not nil)