2012-04-04 13 views
1

レール開発を学び、一般的には無駄な時間よりも答えを探すのが好きですが、これは一晩中私の頭の中で行っています。ユーザー中心のルーティングのためのレール3の先進的な制約

基本的に私はここにレイアウトされた指示に従うことをしようとしているなどのGitHubたalaユーザ依存のビューを提示する

をしようとしている: http://collectiveidea.com/blog/archives/2011/05/31/user-centric-routing-in-rails-3/

現時点では私の認証がrailscastからです"スクラッチからの認証 - 改訂版" のセッションを使用して、私のsessions_crontroller.rb:

class SessionsController < ApplicationController 
    def new 
    end 

    def create 
     user = User.find_by_email(params[:email]) 
     if user && user.authenticate(params[:password]) 
     session[:user_id] = user.id 
     redirect_to root_url, notice: "Logged in!" 
     else 
     flash.now.alert = "Email or password is invalid" 
     render "new" 
     end 
    end 

    def destroy 
     session[:user_id] = nil 
     redirect_to root_url, notice: "Logged out!" 
    end 
end 

そして、私のroutes.rbを:

C::Application.routes.draw do 

root :to => "static_pages#home", :constraints => LoggedInConstraint.new(false) 
root :to => "users#show", :constraints => LoggedInConstraint.new(true) 

resources :users 
resources :sessions 

私が理解しているように、私はクッキーを使用していないので、ブログ記事の最後のコメントはrequest.cookies.key?( "user_token")の代わりにrequest.session [:your_key]私はまだstatic_pages#homeに連れて行っていますか?もし誰かがその話題に光を当てることができたら、私はそれを非常に感謝します。

私はまた、任意の書式設定エラーなどをお詫び申し上げます、これは私の最初の質問stackoverflowです。

もう一度おねがいします!

答えて

1

ないあなたの正確な質問について確認が、私はちょうどこれに種の似た何かをしたので、多分私のコードがお手伝いします:

マイルート:

# Except from config/routes.rb 
require File.expand_path("../../lib/role_constraint", __FILE__) 

MyApp::Application.routes.draw do 
    mount Resque::Server, :at => "/resque", :constraints => RoleConstraint.new('admin') 
    ... 
    ... 
    ... 

マイ制約:

# lib/role_constraints.rb 
class RoleConstraint < Struct.new(:value) 
    def matches?(request) 
    request.session[:role] == value 
    end 
end 

マイセッションコントローラ:

# app/controllers/sessions_controller.rb 
class SessionsController < ApplicationController 
    before_filter :require_user, :only => :destroy 
    def new 
    end 

    def create 
    user = User.find_by_username(params[:username]) 
    if user && user.authenticate(params[:password]) 
     session[:user_id] = user.id 

     # Just for /resque 
     # Not secure - if you change a user's role, it will not be updated here 
     # until they log out and log in again. 
     session[:role] = user.role 

     if user.email.nil? 
     redirect_to user, :notice => "Please add your email address to your account" 
     else 
     redirect_to root_url, :notice => "Logged in!" 
     end 
    else 
     flash.now.alert = "Invalid email or password" 
     render "new" 
    end 
    end 

    def destroy 
    session[:user_id] = nil 
    session[:current_project_id] = nil 
    redirect_to root_url, :notice => "Logged out!" 
    end 
end 
関連する問題