gpt4 book ai didi

ruby-on-rails - 为什么Devise一直问我名字不能为空?

转载 作者:数据小太阳 更新时间:2023-10-29 08:04:42 24 4
gpt4 key购买 nike

我是 Rails 新手,正在使用 Devise。我面临的问题是我的表单在注册时没有更新用户的名称列。我在数据表中有名称属性,但我似乎无法在设计注册中使用表单来更改它。

<h2>Sign up</h2>

<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>

<div><%= f.label :name %><br />
<%= f.text_field :name, :autofocus => true %></div>

<div><%= f.label :email %><br />
<%= f.email_field :email %></div>

<div><%= f.label :password %><br />
<%= f.password_field :password %></div>

<div><%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation %></div>

<div><%= f.submit "Sign up" %></div>
<% end %>

<%= render "devise/shared/links" %>

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
has_many :posts

end

方案.rb

ActiveRecord::Schema.define(version: 20130622203624) do

create_table "posts", force: true do |t|
t.integer "user_id"
t.text "description"
t.integer "comments_id"
t.datetime "created_at"
t.datetime "updated_at"
t.string "title"
end

create_table "users", force: true do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
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.datetime "created_at"
t.datetime "updated_at"
t.string "name", null: false
end

add_index "users", ["email"], name: "index_users_on_email", unique: true, using: :btree
add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true, using: :btree

create_table "views", force: true do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", limit: 128, default: "", null: false
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.datetime "created_at"
t.datetime "updated_at"
end

add_index "views", ["email"], name: "index_views_on_email", unique: true, using: :btree
add_index "views", ["reset_password_token"], name: "index_views_on_reset_password_token", unique: true, using: :btree

end

最佳答案

如果您使用的是 rails4,则需要强参数。如果您需要实现额外的参数来设计,您可以为用户创建一个称为配置文件的额外 1-1 关系并将它们存储在那里(从应用程序的长期运行或如果您有更多用户数据更好)我个人觉得将您的用户数据合并到设计用户表中要容易得多。

或者,如果您使用一个或两个属性,您可以执行以下操作。

您需要允许名称参数进行设计。在您的 ApplicationController 代码中执行此操作。

class ApplicationController < ActionController::Base
before_filter :configure_permitted_parameters, if: :devise_controller?

protected

def configure_permitted_parameters
devise_parameter_sanitizer.for(:sign_up) << :name
end
end

devise wiki 上引用更多信息.

关于ruby-on-rails - 为什么Devise一直问我名字不能为空?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17256496/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com