Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6556929
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:00:31+00:00 2026-05-25T13:00:31+00:00

I’ve build my own basic blog-tool with Rails 3.1 based upon http://ruby.railstutorial.org/ruby-on-rails-tutorial-book and with

  • 0

I’ve build my own basic blog-tool with Rails 3.1 based upon http://ruby.railstutorial.org/ruby-on-rails-tutorial-book and with the authorization presented in RailsCast 274 ( http://railscasts.com/episodes/274-remember-me-reset-password ). So I have a current_user for my session-management in my Rails-app that looks like this:

def current_user
  @current_user ||= User.find_by_auth_token!(cookies[:auth_token]) if cookies[:auth_token]
end

Now I’ve got the problem, that I would like to check two values of this user.

My user-model looks like this:

# == Schema Information
#
# Table name: users
#
#  id              :integer         not null, primary key
#  name            :string(255)
#  email           :string(255)
#  password_digest :string(255)
#  admin           :boolean         default(FALSE)
#  created_at      :datetime
#  updated_at      :datetime
#  auth_token      :string(255)
#  writer          :boolean         default(FALSE)
#  website         :string(255)
#

class User < ActiveRecord::Base
  attr_accessible :name, :email, :website, :password, :password_confirmation
  has_secure_password

  has_many :articles, :dependent => :destroy

  email_regex = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
  website_regex = /(^$)|(^(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(([0-9]{1,5})?\/.*)?$)/ix

  validates :name, :presence => true,
                   :length   => { :maximum => 50 }
  validates :email, :presence => true,
                    :format   => { :with => email_regex },
                    :uniqueness => { :case_sensitive => false }
  # Automatically create the virtual attribute 'password_confirmation'.
  validates :password, :presence     => true,
                       :confirmation => true,
                       :length       => { :within => 6..40 }
  validates :website, :format => { :with => website_regex }

  before_create { generate_token(:auth_token) }

  private              
    def generate_token(column)
      begin
        self[column] = SecureRandom.urlsafe_base64
      end while User.exists?(column => self[column])
    end
end

The values I want to check are :admin and :writer, if they are true or not. As you can see, they are not marked as accessible because I want that only administrators can edit these values. I solve this like in RailsCast 237 (I would like to insert the link, but I’m not allowed as a new user to post more than two links) shown. Checking these parameters in controllers with current_user.admin? or current_user.writer? seems to be no problem. But if try this in a view I get the following error-message:

ActionView::Template::Error (undefined method `writer' for nil:NilClass):
    15:       <li><%= link_to "Log in", signin_path %></li>
    16:       <% end %>
    17:     </ul>
    18:     <% if current_user.writer == true %>
    19:     <ul>
    20:       <li><%= link_to "new article", newarticle_path %></li>
    21:     </ul>
  app/views/layouts/_header.html.erb:18:in `_app_views_layouts__header_html_erb___3376819447841576130_36475600'
  app/views/layouts/application.html.erb:12:in `_app_views_layouts_application_html_erb__676755681270806253_35068900'

Could please someone tell me if this problem is solvable and how? I would appreciate that very much!

Update 1: I figured out, that current_user.admin? or current_user.writer? don’t work in controllers either. So it seems I need a general helper-method for that.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T13:00:32+00:00Added an answer on May 25, 2026 at 1:00 pm

    this error means that you are not logged in. There is noting in ‘current_user’ variable.

    you need to add another check whether the user logged-in or not.

    so try this.

    <% if current_user %>
      <% if current_user.writer %> 
      <% end %>
      #this will return true no need for doing current_user.writer == true
    <% end %>
    

    hope this will help

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We're building an app, our first using Rails 3, and we're having to build
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.