Homeland homeland 二次开发,添加手机号字段

aldrich · 2017年12月11日 · 最后由 aldrich 回复于 2017年12月13日 · 2868 次阅读

如题:基于 homeland 做二次开发的时候,需要添加用户手机号字段。在 accoutn_controller 里修改如下

def create
  build_resource(sign_up_params)
  resource.login = params[resource_name][:login]
  resource.email = params[resource_name][:email]
  resource.phone_number = params[resource_name][:phone_number]
  if verify_rucaptcha?(resource) && resource.save
    sign_in(resource_name, resource)
  end
end

视图部分修改如下:

<div class="form-group">
            <%= f.text_field :phone_number, type: :text, class: "form-control input-lg", placeholder: t("activerecord.attributes.user.phone_number") %>
</div>

User model 里修改如下


ACCESSABLE_ATTRS = [:name, :email_public, :location, :company, :bio, :website, :github, :twitter,
                    :tagline, :avatar, :by, :current_password, :password, :password_confirmation,
                    :_rucaptcha, :phone_number,:phone_auth_code]

那么现在我在注册的时候,始终无法写如 phone_number 的值,打断点显示的值如下

resource

#<User id: nil, login: "flk8", name: nil, email: "flk8@admin.com", email_md5: "a4df18915fa08b9e51e8dfa9e7787cb3", email_public: true, location: nil, location_id: nil, bio: nil, website: nil, company: nil, github: nil, twitter: nil, avatar: nil, verified: false, state: "normal", tagline: nil, created_at: nil, updated_at: nil, persistence_token: "", single_access_token: "", perishable_token: "", topics_count: 0, replies_count: 0, follower_ids: nil, team_users_count: nil, followers_count: 0, following_count: 0, phone_number: "">

sign_up_params

{"email_public"=>"1",
"password"=>"flk123",
"password_confirmation"=>"flk123",
"phone_number"=>"15627215695",
"phone_auth_code"=>"15627215695"}

params

<ActionController::Parameters {"utf8"=>"✓", "user"=>{"login"=>"flk8", "email"=>"flk8@admin.com", "email_public"=>"1", "password"=>"flk123", "password_confirmation"=>"flk123", "phone_number"=>"15627215695", "phone_auth_code"=>"15627215695"}, "_rucaptcha"=>"wpsks", "commit"=>"注册", "controller"=>"account", "action"=>"create"} permitted: false>

求助精通 homeland 的大牛们,感激不尽!~

更新: 原因是下面这个设置覆盖了 user model 的 phone_number 属性...

attr_accessor :password_confirmation, :auth_code, :phone_number

提供一下思路

  1. 你试一下,直接保存用户是否可以 User.create!(login: 'xxx', email: 'www@qq.com', password: '12345678', phone_number: '15627215695')
  2. 如果能保存成功,说明 model 没有问题,则
def create
  build_resource(sign_up_params)
  resource.login = params[resource_name][:login]
  resource.email = params[resource_name][:email]
  resource.phone_number = params[resource_name][:phone_number]
  # 打一下断点,查看一下resource内容,并执行一下 save操作
  if verify_rucaptcha?(resource) && resource.save
    sign_in(resource_name, resource)
  end
end

控制台 create 是这个样子:

irb(main):001:0> User.create!(login: 'flk', email: 'www@qq.com', password: '12345678', phone_number: '15627215695')
   (1.1ms)  SET NAMES utf8,  @@SESSION.sql_mode = CONCAT(CONCAT(@@sql_mode, ',STRICT_ALL_TABLES'), ',NO_AUTO_VALUE_ON_ZERO'),  @@SESSION.sql_auto_is_null = 0, @@SESSION.wait_timeout = 2147483
   (0.7ms)  BEGIN
  User Exists (0.9ms)  SELECT  1 AS one FROM `users` WHERE `users`.`email` = BINARY 'www@qq.com' LIMIT 1
  User Exists (0.8ms)  SELECT  1 AS one FROM `users` WHERE `users`.`login` = 'flk' LIMIT 1
  SQL (0.9ms)  INSERT INTO `users` (`login`, `email`, `email_md5`, `created_at`, `updated_at`, `encrypted_password`) VALUES ('flk', 'www@qq.com', 'dcd3a0511f1f4076d370a38115b80e9d', '2017-12-11 09:57:46', '2017-12-11 09:57:46', '$2a$10$soBsMJi/AFhywSOj1JW2b.o4ce2yU.NB31fHwF8vV9csKk3dd219e')
   (2.4ms)  COMMIT
Dalli::Server#connect 127.0.0.1:11211
Enqueued ActionMailer::DeliveryJob (Job ID: aa8b6f14-31fa-450e-91e1-c6b5c4b8433f) to Sidekiq(mailers) with arguments: "UserMailer", "welcome", "deliver_now", 1
Enqueued SearchIndexer (Job ID: 152719d7-1e2e-4d40-a389-733857bf96b6) to Sidekiq(search_indexer) with arguments: "index", "User", 1
=> #<User id: 1, login: "flk", name: nil, email: "www@qq.com", email_md5: "dcd3a0511f1f4076d370a38115b80e9d", email_public: false, location: nil, location_id: nil, bio: nil, website: nil, company: nil, github: nil, twitter: nil, avatar: nil, verified: false, state: "normal", tagline: nil, created_at: "2017-12-11 09:57:46", updated_at: "2017-12-11 09:57:46", persistence_token: "", single_access_token: "", perishable_token: "", topics_count: 0, replies_count: 0, follower_ids: nil, team_users_count: nil, followers_count: 0, following_count: 0, phone_number: "">

awking 回复

insert 的语句里没有 phone_number 字段

  1. migration 做了吗?贴出来看看。
  2. 有没有自己在 model 层重写了 create 方法

===

我本地测试,可以提交 phone_number 字段

群内姜军给出了答案。 帮楼主更新。。 是ACCESSABLE_ATTRSphone_number覆盖了activerecord中的phone_number导致没有存入数据库。

hging 回复

不对,是 attr_accessor

aldrich 回复

啊。 手误。 嗯。

small_fish__ 回复

是我 attr_accessor 里的设置的:phone_number 覆盖了 user model 的 phone_number 字段

small_fish__ 回复

已经解决了😃

需要 登录 后方可回复, 如果你还没有账号请点击这里 注册