为什么设计一直在问我这个名字不能为空? [英] Why does Devise keep asking me that the name can't be blank?

查看:143
本文介绍了为什么设计一直在问我这个名字不能为空?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的Rails,以及我与制定工作。我现在面临的问题是,我的表没有更新的用户的名称列,当他们签约。我在数据表中的name属性,但我似乎无法在色器件登记使用的形式来改变它。

 < H2>注册< / H><%=的form_for(资源:如=> RESOURCE_NAME,:URL => registration_path(RESOURCE_NAME))做| F | %GT;
  &所述;%= devise_error_messages! %GT;  < D​​IV><%= f.label:名称%>< BR />
  <%= f.text_field:姓名,:自动对焦=>真正%GT;< / DIV>  < D​​IV><%= f.label:电子邮件%GT;< BR />
  <%= f.email_field:电子邮件%GT;< / DIV>  < D​​IV><%= f.label:密码%GT;< BR />
  <%= f.password_field:密码%GT;< / DIV>  < D​​IV><%= f.label:password_confirmation%GT;< BR />
  <%= f.password_field:password_confirmation%GT;< / DIV>  < D​​IV><%= f.submit注册%GT;< / DIV>
<%结束%GT;<%=呈现设计/共享/链接%GT;类User< ActiveRecord的::基地
  #包括默认色器件模块。可用其他是:
  #:token_authenticatable,:可加密,:可证实,:可锁定,:timeoutable和:omniauthable
  设计:database_authenticatable,:可注册,
         :可恢复的,:rememberable,:可追踪,:可验证
   的has_many:帖子  结束

Scheme.rb

 的ActiveRecord :: Schema.define(版本:20130622203624)做  CREATE_TABLE上岗,力:真正做| T |
    t.integerUSER_ID
    t.text说明
    t.integercomments_id
    t.datetimecreated_at
    t.datetime的updated_at
    t.string称号
  结束  CREATE_TABLE用户,力:真正做| T |
    t.string电子邮件,默认:空:假的
    t.stringencrypted_pa​​ssword,默认:空:假的
    t.stringreset_password_token
    t.datetimereset_password_sent_at
    t.datetimeremember_created_at
    t.integersign_in_count,默认值:0
    t.datetimecurrent_sign_in_at
    t.datetimelast_sign_in_at
    t.stringcurrent_sign_in_ip
    t.stringlast_sign_in_ip
    t.datetimecreated_at
    t.datetime的updated_at
    t.string名,空:假的
  结束  add_index用户,[电子邮件],名称:index_users_on_email,独一无二的:true,则使用:B树
  add_index用户,[reset_password_token],名称:index_users_on_reset_password_token,独一无二的:true,则使用:B树  CREATE_TABLE意见,力:真正做| T |
    t.string电子邮件,默认:空:假的
    t.stringencrypted_pa​​ssword,限制:128,默认:空:假的
    t.stringreset_password_token
    t.datetimereset_password_sent_at
    t.datetimeremember_created_at
    t.integersign_in_count,默认值:0
    t.datetimecurrent_sign_in_at
    t.datetimelast_sign_in_at
    t.stringcurrent_sign_in_ip
    t.stringlast_sign_in_ip
    t.datetimecreated_at
    t.datetime的updated_at
  结束  add_index意见,[电子邮件],名称:index_views_on_email,独一无二的:true,则使用:B树
  add_index意见,[reset_password_token],名称:index_views_on_reset_password_token,独一无二的:true,则使用:B树结束


解决方案

如果您使用的是rails4你需要强大的参数。如果需要实现额外的参数制定,你既可以为用户创建一个额外的1-1的关系,即情景模式,并将其存储在那里(在应用程序的长期运行更好,或者如果你有更多的用户数据),我个人感觉来更容易在色器件用户表将您的用户数据。

另外,你可以,如果你正在使用一个或两个属性请执行下列操作。

您需要允许对设计出的名称参数。在你的ApplicationController code,做到这一点。

 类的ApplicationController< ActionController的基地::
  的before_filter:configure_permitted_pa​​rameters,如果:devise_controller?  保护  高清configure_permitted_pa​​rameters
    devise_parameter_sanitizer.for(:sign_up)LT;< :名称
   结束
结束

请参阅更多关于这个在制定维基

I am new to Rails, and I am working with Devise. The problem I am facing is that my form is not updating the name column of the user when they are signing up. I have the name attribute in the data table, but I can't seem to alter it using a form in devise registration.

<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

Scheme.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

解决方案

If you are using rails4 you need strong parameters. If you need to implement extra parameters to devise, you could either create an additional 1-1 relation called profiles for users and store them there (better in the long run of app or if you have more user data) and I personally feel it to be much easier the incorporating your user data in the devise user table.

Alternatively you could do the following if you are using one or two attributes.

You need to permit the name parameter for devise. in your ApplicationController code, do this.

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

Refer more on this on devise wiki.

这篇关于为什么设计一直在问我这个名字不能为空?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆