我正在使用Rails 4并设计,当我尝试从我的管理控制台更新用户时遇到问题(我正在使用Rails管理员).每次我尝试更新时,都会给我一个错误:
ActiveRecord::RecordNotUnique (PG::UniqueViolation: ERROR: duplicate key value violates unique constraint "index_users_on_unlock_token"
Run Code Online (Sandbox Code Playgroud)
这可能是因为大多数用户的unlock_token字段为空.但是肯定可以为null的字段仍可以编入索引吗?只有当用户被锁定在他们的帐户之外时才设置unlock_token?
我可以通过在确认令牌字段中输入随机字符串来轻松避免此问题,但这似乎是处理它的一种不好的方法.有更好的想法吗?
我的用户表定义(以及它的相关部分):
create_table "users", force: true do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "screen_name"
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", default: 0
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.string "confirmation_token"
t.datetime "confirmed_at"
t.datetime "confirmation_sent_at"
t.integer "failed_attempts", default: 0
t.string "unlock_token"
t.datetime "locked_at"
end
Run Code Online (Sandbox Code Playgroud)
我的日志错误:
SQL (10.5ms) UPDATE "users" SET "unlock_token" = $1, "updated_at" = $2 …Run Code Online (Sandbox Code Playgroud) 我正在使用设计宝石,我在让我的应用程序在heroku上工作时遇到了问题.我不确定这是数据库的问题还是设计自己.在安装设备之前它工作正常.我在下面放了一个heroku日志.
2014-03-03T05:01:41.875781+00:00 app[web.1]: => Ctrl-C to shutdown server
2014-03-03T05:01:41.859124+00:00 app[web.1]: from /app/vendor/bundle/ruby/2.0.0/gems/railties-4.0.2/lib/rails/commands/server.rb:48:in `app'
2014-03-03T05:01:41.875781+00:00 app[web.1]: => Booting WEBrick
2014-03-03T05:01:41.875781+00:00 app[web.1]: => Rails 4.0.2 application starting in production on http://0.0.0.0:24518
2014-03-03T05:01:41.859124+00:00 app[web.1]: from /app/vendor/bundle/ruby/2.0.0/gems/rack-1.5.2/lib/rack/builder.rb:49:in `eval'
2014-03-03T05:01:41.875781+00:00 app[web.1]: => Run `rails server -h` for more startup options
2014-03-03T05:01:41.875781+00:00 app[web.1]: Exiting
2014-03-03T05:01:41.859124+00:00 app[web.1]: from /app/vendor/bundle/ruby/2.0.0/gems/rack-1.5.2/lib/rack/server.rb:314:in `wrapped_app'
2014-03-03T05:01:41.859124+00:00 app[web.1]: from /app/vendor/bundle/ruby/2.0.0/gems/rack-1.5.2/lib/rack/server.rb:199:in `app'
2014-03-03T05:01:41.859299+00:00 app[web.1]: from bin/rails:4:in `require'
2014-03-03T05:01:43.043928+00:00 heroku[web.1]: Process exited with status 1
2014-03-03T05:01:43.048168+00:00 heroku[web.1]: State changed from starting to crashed
2014-03-03T05:01:47.110760+00:00 heroku[router]: …Run Code Online (Sandbox Code Playgroud) 如果用户不是管理员,我将RailsAdmin设置为重定向到主页.但是,重定向失败.这是我收到的错误消息:
ActionController::UrlGenerationError in RailsAdmin::MainController#dashboard
No route matches {:controller=>"home", :action=>"index"} missing required keys: []
Run Code Online (Sandbox Code Playgroud)
rails_admin.rb中的代码如下所示.在配置文件的顶部:
ADMIN_EMAILS= ['my@email.com']
Run Code Online (Sandbox Code Playgroud)
以下是限制访问管理员的其余代码:
config.authorize_with do
is_admin = ADMIN_EMAILS.include?(current_user.email)
if current_user
redirect_to root_path unless is_admin
end
end
Run Code Online (Sandbox Code Playgroud)
这是完整的堆栈跟踪:
actionpack (4.0.0) lib/action_dispatch/journey/formatter.rb:35:in `generate'
actionpack (4.0.0) lib/action_dispatch/routing/route_set.rb:576:in `generate'
actionpack (4.0.0) lib/action_dispatch/routing/route_set.rb:606:in `generate'
actionpack (4.0.0) lib/action_dispatch/routing/route_set.rb:642:in `url_for'
actionpack (4.0.0) lib/action_dispatch/routing/url_for.rb:155:in `url_for'
actionpack (4.0.0) lib/action_dispatch/routing/route_set.rb:209:in `call'
actionpack (4.0.0) lib/action_dispatch/routing/route_set.rb:178:in `call'
actionpack (4.0.0) lib/action_dispatch/routing/route_set.rb:249:in `block (2 levels) in define_url_helper'
config/initializers/rails_admin.rb:17:in `block (2 levels) in <top (required)>'
rails_admin (0.6.1) app/controllers/rails_admin/application_controller.rb:47:in `instance_eval'
rails_admin (0.6.1) …Run Code Online (Sandbox Code Playgroud) 我在这个问题上找不到其他答案对我有帮助.
我有一个条款和条件框,并想验证它是否在创建时进行了检查.
在我的模型中:
validates :terms_and_conditions, :on => :create, acceptance: true, :allow_nil => false
Run Code Online (Sandbox Code Playgroud)
在我看来:
<%= f.check_box(:terms_and_conditions )%>
Run Code Online (Sandbox Code Playgroud)
无论是否check_box检查,它总是失败.我认为它实际上从未设置过该字段,问题在我看来,但我找不到它.
提交表单时记录.
Processing by Users::RegistrationsController#create as HTML
Parameters: {"utf8"=>"?", "authenticity_token"=>"xNNmGTWSV4hkj0rrlZeOq+AiTlJ9sc9+cAT1Sy0bjhc=", "user"=>{"email"=>"sdasdr@gmail.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "name"=>"", "surname"=>"", "country_id"=>"", "language"=>"", "description"=>"", "register_as_company"=>"1", "terms_and_conditions"=>"1"}, "commit"=>"Sign up"}
Unpermitted parameters: name, surname, country_id, language, description, register_as_company, terms_and_conditions
[1m[35m (0.1ms)[0m begin transaction
[1m[36mUser Exists (0.3ms)[0m [1mSELECT 1 AS one FROM "users" WHERE "users"."email" = 'sdasdr@gmail.com' LIMIT 1[0m
[1m[35m (0.1ms)[0m rollback transaction
[1m[36mCountry Load (0.2ms)[0m [1mSELECT "countries".* FROM "countries"[0m …Run Code Online (Sandbox Code Playgroud) 嗨,我有一个模型Coach,我正在尝试添加特定的字段.我创建了一个迁移并将字段添加到注册中,但在Devise :: Registrations #new&undefined method`first_name'中收到错误
NoMethodError
<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<div><%= f.label :first_name %><br />
<%= f.text_field :first_name, autofocus: true %></div>
<div><%= f.label :email %><br />
<%= f.email_field :email, autofocus: true %></div>
<div><%= f.label :password %><br />
<%= f.password_field :password, autocomplete: "off" %></div>
<div><%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation, autocomplete: "off" %></div>
<div><%= f.submit "Sign up" %></div>
<% end %>
<%= render …Run Code Online (Sandbox Code Playgroud) 我有以下代码,以显示用户的一些记录:
= @kid.educations.each do |education|
= education.studies_centre
%br
= education.city_and_country
%br
%small
= education.academic_qualification
%hr
%br
= @kid.certificates.each do |certificate|
= certificate.certificate
%br
= certificate.city_and_country
%br
%small
= certificate.description
%hr
%br
= @kid.works.each do |work|
= work.company
%br
= work.city_and_country
%br
%small
= work.description
%hr
%br
Run Code Online (Sandbox Code Playgroud)
但是在每一条记录之后,我都会得到真实的字样,如下图所示:

我该怎么做才能解决这个问题.在此先感谢您的帮助
我正在制作一项网络服务,信用卡信息将作为用户个人资料的一部分存储,并将用于处理付款.
但是,我不喜欢将卡信息保存为数据库中的原始文本的想法.相反,我想以某种方式对卡号进行散列,以便在恶意人员访问数据库时,该站点的用户将尽可能保持安全.
我想它可以类似于密码的散列方式,但一个重要的区别是我需要能够通过第三方api解散并发送信用卡信息.
如何在rails中为Devise用户添加哈希信用卡?
谢谢你的帮助
我正在尝试测试用户创建的用户模型规范
工厂/ users.rb的
FactoryGirl.define do
factory :user do
first_name {Faker::Name.first_name}
last_name {Faker::Name.last_name}
email {Faker::Internet.email}
username {Faker::Internet.user_name}
password {Faker::Internet.password}
end
end
Run Code Online (Sandbox Code Playgroud)
规格/型号/ user_spec.rb
require 'rails_helper'
describe User, :type => :model do
context "valid Factory" do
it "has a valid factory" do
expect(build(:user)).to be_valid
end
end
context "validations" do
before { create(:user) }
context "presence" do
it { should validate_presence_of :first_name }
it { should validate_presence_of :last_name }
it { should validate_presence_of :email }
it { should validate_presence_of :encrypted_password }
end
context …Run Code Online (Sandbox Code Playgroud) 我正在构建一个rails应用程序,我需要通过以下方式支持多个租户:
example.com/:tenant_1
example.com/:tenant_2
Run Code Online (Sandbox Code Playgroud)
对于这些租户中的每一个,我想要单独登录:
example.com/:tenant_1/sign_in
example.com/:tenant_2/sign_in
Run Code Online (Sandbox Code Playgroud)
对于每个租户,我希望他们的登录会话的范围限定在适用的租户子目录中.这是设计支持的东西,如果是这样的话,如何实现这个?
问题:我想为行动制作password&password_confirmation字段validates presence:true,create而不是对update行动进行验证
guest.rb:
class Guest < ActiveRecord::Base
devise :database_authenticatable, :recoverable, :rememberable, :trackable
validates :email, presence: true
end
Run Code Online (Sandbox Code Playgroud)
我的guests_controller.rb:
class GuestsController < ApplicationController
before_action :set_guest, only: [:show, :edit, :update]
def index
@guests = Guest.all
end
def show
@guest = Guest.find(params[:id])
end
def new
@guest = Guest.new
end
def edit
@guest = Guest.find(params[:id])
end
def create
respond_to do |format|
format.html do
@guest = Guest.new(guest_params)
if @guest.save
redirect_to guests_path, notice: 'Client was successfully …Run Code Online (Sandbox Code Playgroud)