1

目前我正在开发一个短信应用程序。我用于devise gem身份验证和carrier wave上传。我的问题是成功登录后,用户必须重定向到他自己的页面,并且不同的用户将重定向到不同的页面。

用户模型

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and    :omniauthable
   devise :database_authenticatable, :registerable,:recoverable, :rememberable, :trackable, :validatable
   # Setup accessible (or protected) attributes for your model
   attr_accessible :email, :password, :password_confirmation, :remember_me
   # attr_accessible :title, :body
   has_many :sms
 end

型号

class Sm < ActiveRecord::Base
  attr_accessible :Messages, :Mobile_no, :Nickname, :Templates
  validates_presence_of :Mobile_no
  validates_length_of :Mobile_no, :minimum => 10, :maximum => 10, :allow_blank => true
  validates :Mobile_no, :numericality => {:only_integer => true}
  attr_accessible :sm_id, :name, :image
  belongs_to :sm
  mount_uploader :image, ImageUploader
  validate :image_size_validation, :if => "image?"  

  def image_size_validation
    errors[:image] << "should be less than 1MB" if image.size > 1.megabytes
  end

  validates :image, allow_blank: true, format: {
    with: %r{\.(xls|xlsx|csv|txt)\z}i,
    message: 'must be a TXT, CSV, XLS, or XLSX'
  }, if: :filename_has_extension?

  def filename_has_extension?
    !(image.to_s =~ /\.[a-z]{1,4}\z/).nil?
  end
  belongs_to :user
end
4

1 回答 1

0

您应该在 ApplicationController 中定义 after_sign_in_path_for 方法

  def after_sign_in_path_for(resource)
    # resource is commonly a User class object
    if resource.admin?
      # redirect somewhere
    else
      # redirect somewhere else
    end
  end
于 2012-11-20T07:51:15.563 回答