Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails rails 3中用于以用户为中心的路由的高级约束_Ruby On Rails_Ruby On Rails 3_Routes_Constraints - Fatal编程技术网

Ruby on rails rails 3中用于以用户为中心的路由的高级约束

Ruby on rails rails 3中用于以用户为中心的路由的高级约束,ruby-on-rails,ruby-on-rails-3,routes,constraints,Ruby On Rails,Ruby On Rails 3,Routes,Constraints,学习rails开发,通常宁愿寻找答案也不愿浪费时间,但这整晚都在困扰着我 本质上,我试图呈现依赖于用户的视图,如github等 我试着按照这里的说明来做: 目前我的身份验证来自railscast“从头开始的身份验证-修订版”,它使用会话My sessions\u crontroller.rb: class SessionsController < ApplicationController def new end def create user =

学习rails开发,通常宁愿寻找答案也不愿浪费时间,但这整晚都在困扰着我

本质上,我试图呈现依赖于用户的视图,如github等

我试着按照这里的说明来做:

目前我的身份验证来自railscast“从头开始的身份验证-修订版”,它使用会话My sessions\u 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
根据我的理解,因为我没有使用cookies,所以博客帖子下的最后一条评论建议使用request.session[:your_key]来代替request.cookies.key?(“用户_令牌”),但是当我登录时,我仍然会被带到静态_页面#主页?如果有人能解释一下这个话题,我将不胜感激

我也为任何格式错误等道歉,这是我关于stackoverflow的第一个问题


再次感谢

不确定您的确切问题,但我只是做了类似的事情,所以我的代码可能会帮助您:

我的路线:

# 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
#lib/role_constraints.rb
类RoleConstraint
我的会话控制器:

# 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
#app/controllers/sessions_controller.rb
类sessioncontroller:destroy
def新
结束
def创建
user=user.find\u by_username(参数[:username])
如果用户&&user.authenticate(参数[:密码])
会话[:user\u id]=user.id
#只是为了/重新确认
#不安全-如果您更改了用户的角色,它将不会在此处更新
#直到他们注销并再次登录。
会话[:角色]=user.role
如果user.email.nil?
将_重定向到用户:notice=>“请将您的电子邮件地址添加到您的帐户”
其他的
将\u重定向到root\u url,:notice=>“已登录!”
结束
其他的
flash.now.alert=“无效的电子邮件或密码”
呈现“新”
结束
结束
def销毁
会话[:用户\u id]=无
会话[:当前项目\u id]=无
将\u重定向到root\u url,:notice=>“已注销!”
结束
结束

不确定您的确切问题,但我只是做了类似的事情,所以我的代码可能会帮助您:

我的路线:

# 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
#lib/role_constraints.rb
类RoleConstraint
我的会话控制器:

# 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
#app/controllers/sessions_controller.rb
类sessioncontroller:destroy
def新
结束
def创建
user=user.find\u by_username(参数[:username])
如果用户&&user.authenticate(参数[:密码])
会话[:user\u id]=user.id
#只是为了/重新确认
#不安全-如果您更改了用户的角色,它将不会在此处更新
#直到他们注销并再次登录。
会话[:角色]=user.role
如果user.email.nil?
将_重定向到用户:notice=>“请将您的电子邮件地址添加到您的帐户”
其他的
将\u重定向到root\u url,:notice=>“已登录!”
结束
其他的
flash.now.alert=“无效的电子邮件或密码”
呈现“新”
结束
结束
def销毁
会话[:用户\u id]=无
会话[:当前项目\u id]=无
将\u重定向到root\u url,:notice=>“已注销!”
结束
结束