Topic: Undefined method admin?

I am working on an ecommerce website for a college project and I am working on the admin user section of the site. I was using the micheal hartl tutorial (http://ruby.railstutorial.org/chapters/ … tive_users) but I've run into a problem, - undefined method `admin?' for #<#<Class:0x38d9540>:0x38d5e20>. In the tutuorial he doesn't define a method call admin?

Can anybody shed some light on where I'm going wrong, or if there is a better way to make admin users?

Re: Undefined method admin?

This is what is got when I google part of the above error message. This might help:
http://getsatisfaction.com/railstutoria … l_nilclass
or this:
http://stackoverflow.com/questions/5919 … -nilnilcla
or this:
http://stackoverflow.com/questions/2763 … thod-error
or this:
https://github.com/plataformatec/devise … Admin-role

Re: Undefined method admin?

Thanks for the help, I've already went through all them though.

Re: Undefined method admin?

From that tutorial:
--snip--
As expected, Rails figures out the boolean nature of the admin attribute and automatically adds the question-mark method admin?
--snip--

So did you run the migration that added the boolean 'admin' attribute?

Look at the schema file created by the last migration, and make sure the users table has an attribute named 'admin' and it is of type boolean.

Joe got a job, on the day shift, at the Utility Muffin Research Kitchen, arrogantly twisting the sterile canvas snout of a fully charged icing anointment utensil.

Re: Undefined method admin?

hi Brad

i defininetly ran the migration and it shows in the development.sqlite3 file when i open it in sqlite3 browser and in the schema.rb file. This is the error i get whenever i logout. Again any help would be great. Thanks

NoMethodError in Products#index

Showing c:/Street_Heart_Project/app/views/products/index.html.erb where line #20 raised:

undefined method `admin?' for nil:NilClass

Extracted source (around line #20):

17:         </dl>
18:        </td>
19:       
20:         <% if current_user.admin? %>
21:           <td class = "list_actions">
22:             <%= link_to 'Show', product %><br/>
23:             <%= link_to 'Edit', edit_product_path(product) %><br/>

Rails.root: c:/Street_Heart_Project
Application Trace | Framework Trace | Full Trace

app/views/products/index.html.erb:20:in `block in _app_views_products_index_html_erb__941812207_36067080__209394639'
app/views/products/index.html.erb:5:in `each'
app/views/products/index.html.erb:5:in `_app_views_products_index_html_erb__941812207_36067080__209394639'
app/controllers/products_controller.rb:10:in `index'

Re: Undefined method admin?

What authentication system are you using?

Did you roll your own, or are you using devise, or restful_athenrication

current_user is nil,  it's not returning a currently logged on user

either you haven't logged in yet,  or your login code is messed up.

to be safe you should have something like

if logged_in?

preceding the check for admin status

post your entire products_controller.rb

Last edited by BradHodges (2011-12-02 12:11:27)

Joe got a job, on the day shift, at the Utility Muffin Research Kitchen, arrogantly twisting the sterile canvas snout of a fully charged icing anointment utensil.