保存之前的小写是零是Rails

时间:2016-06-05 15:02:20

标签: ruby-on-rails ruby

Rails 5.0.0.rc1

我希望在保存在rails 5.0.0.rc1之前有usernamedowncase,但有些usernamenil {/ p>}

user.rb:

before_save :downcase_username

def downcase_username
  self.username.downcase!
end

HTML:

<form action="/users" method="post">
    <input type="text" name="user[username]" id="user_username" />
</form>
  

未定义的方法`downcase!&#39;为零:NilClass

我使用React,我可以创建一个用户名没有问题的用户。我可以将username设置为toLowerCase,但我选择不这样做。

为什么用户名是nil?

修改(.jsx):

handleName: function(e){
  this.setState({name: e.target.value});
}

<form action="/users" method="post">
   <input type="text" name="user[username]" id="user_username"
    value={this.state.name} onChange={this.handleName} placeholder="username" />
  <button className="button small radius" type="submit">Register</button>
</form>

设计控制器:

def create
    super do |resource|
      resource.user[username] = params[:username]
      resource.registration_id = params[:registration_id]
      resource.save!
    end
  end

日志:

Processing by Users::RegistrationsController#create as HTML
  Parameters: {"user"=>{"username"=>"Test", "email"=>"test@example.com", "password"=>"[FILTERED]"}, "registration_id"=>"2"}
Unpermitted parameter: username
   (0.2ms)  BEGIN
  User Exists (0.3ms)  SELECT  1 AS one FROM "users" WHERE "users"."email" = $1 LIMIT $2  [["email", "test@example.com"], ["LIMIT", 1]]
   (0.1ms)  ROLLBACK
Completed 500 Internal Server Error in 159ms (ActiveRecord: 0.7ms)

日志2:#数据现在可以保存但仍然可以保持不变

Started POST "/users" for ::1 at 2016-06-05 17:18:10 +0100
Processing by Users::RegistrationsController#create as HTML
  Parameters: {"user"=>{"username"=>"Foo", "email"=>"foo@example.edu", "password"=>"[FILTERED]"}, "registration_id"=>"2"}
   (0.1ms)  BEGIN
  User Exists (0.4ms)  SELECT  1 AS one FROM "users" WHERE "users"."email" = $1 LIMIT $2  [["email", "foo@example.edu"], ["LIMIT", 1]]
  SQL (1.1ms)  INSERT INTO "users" ("email", "encrypted_password", "created_at", "updated_at", "username") VALUES ($1, $2, $3, $4, $5) RETURNING "id"  [["email", "foo@example.edu"], ["encrypted_password", "$2a$11$TrrnP4391MeEMvn8e2JwMesU5JS0vGELx0.8eUVO.B4sDPZVdUFMy"], ["created_at", 2016-06-05 16:18:10 UTC], ["updated_at", 2016-06-05 16:18:10 UTC], ["username", "foo"]]
   (0.9ms)  COMMIT
   (0.1ms)  BEGIN
   (0.1ms)  ROLLBACK
Completed 500 Internal Server Error in 168ms (ActiveRecord: 5.5ms)

2 个答案:

答案 0 :(得分:1)

更新方法如下: -

def downcase_username

self.usernname = self.username.try(:downcase)

答案 1 :(得分:0)

<强> application_controller.rb

class ApplicationController < ActionController::Base

protected
      def configure_permitted_parameters
        # from new devise -v 4.1.1 gem
        # devise_parameter_sanitizer.permit(:sign_up, keys: [:username])
        # devise_parameter_sanitizer.permit(:account_update, keys: [:username])

         #older version
        devise_parameter_sanitizer.for(:sign_up) << [:username]
        devise_parameter_sanitizer.for(:account_update) << [:username]
      end
end