2012-07-24 2 views
0

두 가지 문제가있어 그 이유를 알 수 없습니다! 나는 튜토리얼을 따라 갔다. http://ruby.railstutorial.org/chapters/Ruby on Rails - 로그인 세션 편집

Problem #1: My login and logout don't seem to be working 
Problem #2: My private function doesn't see sign_in? -- I believe however is because sign_in? is declared in the sessionHelpers and I declared this function in the customerController which would explain why I can't see but don't know how to make it see. 

여기 코드가있다! 모델/customer.rb 컨트롤러/세션

class SessionsController < ApplicationController 
    def new 
    end 

    def create 
     customer = Customer.find_by_email(params[:session][:email]) 
     if customer && customer.authenticate(params[:session][:password]) 
       # Sign the user in and redirect to the user's show page. 
      sign_in customer 
      redirect_back_or customer 
     else 
      flash.now[:error] = 'Invalid email/password combination' 
      render 'new' 
     end 

    end 

    def destroy 
     sign_out 
     redirect_to root_path 
    end 
end 

컨트롤러/고객

class CustomersController < ApplicationController 
    before_filter :signed_in_customer, only: [:edit, :update] 
    before_filter :correct_customer, only: [:edit, :update] 

    def new 
     @customer = Customer.new 
    end 

    def create 
     @customer = Customer.new(params[:customer]) 
     if @customer.save 
      sign_in @customer 
      flash[:success] = "Welcome to Where you Where" 
      redirect_to @customer 
     else 
      render 'new' 
     end 
    end 

    def show 
     @customer = Customer.find(params[:id]) 
    end 

    def edit 
     @customer = Customer.find(params[:id]) 
    end 

    def update 
     @customer = Customer.find(params[:id]) 
     if @customer.update_attributes(params[:customer]) 
      flash[:success] = "Profile updated" 
      sign_in @customer 
      redirect_to @customer 
     else 
      render 'edit' 
     end 
    end 

    def index 
     @customers = Customer.all 
    end 

    private 
     def signed_in_customer 
      unless signed_in? 
       store_location 
       redirect_to signin_path, notice: "Please sign in." 
      end 
     end 

      def correct_customer 
       @customer = Customer.find(params[:id]) 
       redirect_to(root_path) unless current_customer?(@customer) 
      end 
end 

도우미 다시/세션

module SessionsHelper 
    def sign_in(customer) 
     cookies.permanent[:remember_token] = customer.remember_token 
     self.current_customer = customer 
    end 

    def sign_in? 
     !current_customer.nil? 
    end 

    def current_customer=(customer) 
     @current_customer = customer 
    end 

    def current_customer?(customer) 
     customer = current_customer 
    end 

    def current_customer 
     @current_customer ||= Customer.find_by_remember_token(cookies[:remember_token]) 
    end 

    def sign_out 
     self.current_customer = nil 
     cookies.delete(:remember_me) 
    end 

    def redirect_back_or(default) 
      redirect_to(session[:return_to] || default) 
      session.delete(:return_to) 
    end 

    def store_location 
      session[:return_to] = request.fullpath 
    end 
end 

감사의 철자를 잘못

업데이트 죄송합니다 :

여기에 응용 프로그램 컨트롤러

class ApplicationController < ActionController::Base 
    protect_from_forgery 
    before_filter :pages 

    def pages 
     @pages = Page.all 
    end 
    include SessionsHelper 
end 

고객에게

undefined method `signed_in?' for #<CustomersController:0xb56089ec> 
app/controllers/customers_controller.rb:45:in `signed_in_customer' 

주를 편집하려고 : 난 그냥 DB를했던 오류 : 재설정하고 새로운 계정을 생성합니다. 나는 성공했지만 헤더의 링크는 로그 아웃했다. 여기

헤더 파일

<header class="header"> 
    <div class="menu"> 
    <div class="center"> 
    <div class="logo"> 
      <nav> 
     <ul> 
     <% if sign_in? %> 
      <div>Welcome <%= current_customer.full_name %> </div> 
      <li><%= link_to "Home", root_path %></li> 
      <li><%= link_to "Customers", customers_path %></li> 
       <li><%= link_to "Profile", current_customer %></li> 
       <li><%= link_to "Settings", edit_customer_path(current_customer) %></li> 
       <li><%= link_to "Sign out", signout_path, method: "delete" %></li> 
     <% else %> 
      <li><%= link_to "Home", '#' %></li> 
       <li><%= link_to "Sign in", '#' %></li> 
     <% end %> 
     </ul> 
      </nav> 
    </div> 
    </div> 
    </div> 
</header> 

답변

0

당신이와 ApplicationController에서 도우미를 포함 있는지 확인은

class ApplicationController < ActionController::Base 
    protect_from_forgery 
    include SessionsHelper 
end 

이것은 당신의 예에서와 ApplicationController에서 (예 : CustomersController로) 상속하는 모든 컨트롤러 수를 의미 도우미의 메서드에 액세스하십시오.

나는 모든 코드를 살펴 보지 않았지만이 코드가 모두 해결되면 알려주십시오. ms, 나에게 알려주지 않으면 나는 더 긴 표정을 가질 것이다.

+0

Nah 그 안에 이미있다. 필자는 그것을 보여주기 위해 내 게시물을 업데이트했습니다. 제안 해 주셔서 감사합니다. – Jseb