1

나는 레일스 3.1을 기본으로하고 http://ruby.railstutorial.org/ruby-on-rails-tutorial-book을 기반으로 RailsCast 274 (http://railscasts.com/episodes/274-remember-me-reset-password)에 제시된 권한으로 기본 블로그 도구를 구축했습니다. 나는이 사용자의 두 값을 확인하고 싶다고, 지금은 문제가있어레일스 3.1 도우미 - current_user의 값을 확인하는 방법

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

: 그래서 다음과 같습니다 내 레일 - 응용 프로그램 내 세션 관리를위한 CURRENT_USER 있습니다.

내 사용자 모델은 다음과 같습니다가 true 또는하지 않은 경우, 작가 : 관리자 및 :

# == 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+\-.][email protected][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 

내가 확인 할 값은 이다. 보시다시피 관리자 만이 값을 편집 할 수 있기를 원하기 때문에 액세스 가능으로 표시되지 않습니다. RailsCast 237 (링크를 삽입하고 싶습니다. 그러나 두 개 이상의 링크를 게시하는 새 사용자로 허용되지 않음) 에서처럼 이것을 해결합니다. 컨트롤러에서 이러한 매개 변수를 확인하려면 current_user.admin? 또는 current_user.writer?은 문제 없습니다. 하지만 다음과 같은 오류 메시지 얻을 전망이 시도하는 경우 :

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' 

이 문제를 풀 수있는 방법 인 경우 누군가가 나에게 기쁘게 할 수 있습니까? 나는 그것을 대단히 감사 할 것입니다!

업데이트 1 : 알아 냈습니다. current_user.admin? 또는 current_user.writer? 컨트롤러에서 작동하지 않습니다. 그래서 나는 일반적인 도우미 - 방법이 필요해 보인다.

답변

1

이 오류는 사용자가 로그인하지 않았다는 것을 의미합니다. 'current_user'변수에주의해야합니다.

사용자가 로그인했는지 여부를 다시 한 번 확인해야합니다.

그래서 시도해보십시오. 내 문제 해결이

+0

가 대단히 감사합니다 도움이 될 것입니다

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

희망! –