【问题标题】:Generating a random string to save as an id, but continues to change on every page refresh?生成随机字符串以保存为 id,但在每次页面刷新时继续更改?
【发布时间】:2016-05-14 05:59:26
【问题描述】:

我正在尝试生成一个随机的 4-5 字母数字字符串来替换标准用户 ID,我一切正常,除了每次刷新它生成的页面并替换字符串时。我已经尝试过 before_save 和 before_create 但两者似乎都在这里不起作用。

我的模特:

class Admin < ActiveRecord::Base
  before_create :admin_ident

  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable,
         :validatable, :confirmable, :lockable, :timeoutable

       validates_uniqueness_of :admin_ident

       def admin_ident
         self.admin_ident = SecureRandom.hex(2).upcase
       end
end

我的设计注册控制器:

class Admin::Admins::RegistrationsController < Devise::RegistrationsController

  private

  def sign_up_params
    params.require(:admin_admin).permit(:email, :password, :password_confirm, :admin_ident, :f_name, :m_name, :l_name, :dob, :street_number, :street_name, :unit_apt,
    :quadrant, :city, :province, :postal_code, :home_tel, :moibile_tel, :office_tel, :office_ext, :company_email,
    :position, :start_date, :end_date, :quit, :resigned, :terminated_cause, :terminated_wo_cause, :medical_leave,
    :leave_of_abscense, :dl_number, :dl_class, :expiry, :conditions, :sl_number, :sl_certs, :issued, :expires, :emc_1_name,
    :emc_1_tel, :emc_1_relationship, :emc_2_name, :emc_2_tel, :emc_2_relationship)
  end

  def account_update_params
    params.require(:admin_admin).permit(:email, :password, :password_confirm, :current_password, :admin_ident, :f_name, :m_name, :l_name, :dob, :street_number, :street_name, :unit_apt,
    :quadrant, :city, :province, :postal_code, :home_tel, :moibile_tel, :office_tel, :office_ext, :company_email,
    :position, :start_date, :end_date, :quit, :resigned, :terminated_cause, :terminated_wo_cause, :medical_leave,
    :leave_of_abscense, :dl_number, :dl_class, :expiry, :conditions, :sl_number, :sl_certs, :issued, :expires, :emc_1_name,
    :emc_1_tel, :emc_1_relationship, :emc_2_name, :emc_2_tel, :emc_2_relationship)
  end

  def set_admin
     @admin = Admin.find_by_admin_ident(params[:id])
  end

end

我的 Routes.rb 文件:

  ## Namespace Resources
    namespace :admin do
      devise_for :admins, controllers: {
        :registrations => 'admin/admins/registrations',
        :sessions => 'admin/admins/sessions',
        :passwords => 'admin/admins/passwords',
        :confirmations => 'admin/admins/confirmations',
        :unlocks => 'admin/admins/unlocks'
      }
      resources :admin_static
    end

  ## Devise Scopes
  devise_scope :admin do
    authenticated  do
      root to: 'admin/admin_static#home', as: 'admin_authenticated_root'
    end
  end

不知道我哪里出错了..任何帮助都会很棒!

编辑 1:

我正在尝试编辑 admin/admin_static#home 页面

【问题讨论】:

  • 你在刷新什么页面?
  • 我会编辑但我正在刷新 admin/admin_static#home
  • 它本质上只是一个登录后的静态登陆页面
  • 你为什么要这样做?你想达到什么目标?
  • 这是一个用于安全/巡逻管理/调度他们登陆的页面将提供每日更新但现在它是静态的 b2b 应用程序

标签: ruby-on-rails ruby postgresql ruby-on-rails-4 model-view-controller


【解决方案1】:

大概您在这些页面上显示admin_identsome_admin.admin_ident

这将更新属性,因为您已将 reader 方法替换为设置 admin ident 的方法。我建议您改为调用该方法 set_admin_ident(并更新 before_create 以匹配)

【讨论】:

    【解决方案2】:

    我认为可能是您对字段使用相同的名称以及随机更新该字段的方法。

    before_create :generate_admin_ident
    validates_uniqueness_of :generate_admin_ident
    
    def generate_admin_ident
      begin
        self.admin_ident = SecureRandom.hex(2).upcase
        other_admin = Admin.find_by(admin_ident: self.admin_ident)
      end while other_admin
    end
    

    这应该使admin_ident 的所有使用都指向字段而不是随机生成器。

    请注意,您还使用验证来确保唯一性,但是,这可能会导致对create!save 的任意调用失败。这些应该包含在 begin..rescue..retry 块中,或者随机生成器应该自己验证唯一性。

    begin..end while 循环和other_admin 代码旨在手动验证该方法生成的admin_ident 的唯一性,并不断尝试直到找到唯一值。

    【讨论】:

    • 这就是问题所在,我删除了生成,认为它会更容易全面......我的愚蠢错误!非常感谢
    • 超级!我在答案中添加了一些内容,以防它有助于追踪下一个问题。在验证生成的 ID 方面吸取了深刻的教训。
    • 啊,太好了,我可以把验证语句放在方法中吗?这可能是一个菜鸟问题,但我是菜鸟哈哈..
    • 不幸的是,没有。验证实际上是一个回调,当create 发生时被调用。您实际上需要在方法内部进行查询。我会用一个例子来更新答案。
    • 太棒了!非常感谢这里的额外帮助!现在我看到了,这很有意义!非常感谢。
    猜你喜欢
    • 2014-02-09
    • 1970-01-01
    • 2012-06-06
    • 2021-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多