某些字段未保存在sql表中

时间:2014-07-29 11:47:48

标签: sql ruby-on-rails ruby

Ruby 1.9.3p545 Rails 4.1.4

我正在尝试将此表单中的字段保存到表格配置文件

   <%= form_for @profile do |f| %>
       <% if @profile.errors.any? %>
          <div class="error_messages">
          <h2>Form is invalid</h2>
           <ul>
           <% for message in @profile.errors.full_messages %>
           <li><%= message %></li>
            <% end %>
           </ul>
            </div>
       <% end %>
  <p>
  <%= f.label :name %><br />
  <%= f.text_field :name %>
  </p>
  <p>
  <%= f.label :email %><br />
  <%= f.text_field :email %>
  </p>
  <p>
   <%= f.label :password %><br />
   <%= f.password_field :password %>
  </p>
  <p>
  <%= f.label :password_confirmation %><br />
  <%= f.password_field :password_confirmation %>
  </p>
  <p>
   <%= f.label :interests %><br />
   <%= autocomplete_field_tag 'interests', '', profile_autocomplete_interest_name_path %>  </p>
   <p>
   <%= f.label :zip %><br />
   <%= f.text_field :zip %>
   </p>
   <p>
   <%= f.label :country %><br />
   <%= f.country_select :country, ["United Kingdom"] %>  </p>
   <p class="button"><%= f.submit %></p>

未保存密码和兴趣字段。 控制器是

  autocomplete :profile, :interest
  def new
     @profile = Profile.new 
  end
  def create
  @profile = Profile.new(profile_params)
  if @profile.save

Profile模型中的方法是

  attr_accessor :password,  :encrypted_password, :password_confirmation
  validates :name, :presence => true, :uniqueness => true, :length => { :in => 3..20 }
  validates :password, :confirmation => true 
  validates_length_of :password, :in => 6..20, :on => :create
  validates :email, :presence => true, :uniqueness => true, confirmation: true
  validates_email_realness_of :email
  before_save :encrypt_password
  logger.info "post save"
  after_save :clear_password
  def encrypt_password
     if password.present?
         password_salt = BCrypt::Engine.generate_salt
         password= BCrypt::Engine.hash_secret(password, password_salt)
         logger.info "debug passwords"
         logger.debug password_salt
         logger.debug password
     end
  end
  def clear_password
      self.password = nil
  end

表定义是

 def change
    create_table :profiles do |t|
     t.string :name
     t.string :email
    t.string :interests
    t.string :zip
     t.string :country
     t.string :password
     t.boolean :author
     t.date :dob
     t.string :gender
     t.boolean :optinemail
     t.boolean :optinstories
     t.timestamps
   end

从日志中

所有参数都成功传递给配置文件模型,并且由于

而在encrypt_password中构建了密码
   before_save :encrypt_password

但是SQL语句缺少两个字段,之后没有记录任何内容,因此after_save:clear_password未被操作

在2014-07-29 12:05:40 +0100开始为127.0.0.1发布POST / / profiles 由ProfilesController处理#create as HTML   参数:{“utf8”=&gt;“”“”,“authenticity_token”=&gt;“D8HVrGVeFudwz5O0rvf7orkW / TFJS3kXL6uv9lZOxEU =”,“profile”=&gt; {“name”=&gt;“admin”,“email”=&gt; “info@good2use.com”,“password”=&gt;“[FILTERED]”,“password_confirmation”=&gt;“[FILTERED]”,“zip”=&gt;“b72 1af”,“country”=&gt;“英国“},”兴趣“=&gt;”高尔夫“,”提交“=&gt;”创建个人资料“}

[1m [35m(0.0ms)[0m开始交易    [1m [36mProfile Exists(1.0ms)[0m [1mSELECT 1 AS one FROM“profiles”WHERE“profiles”。“name”='admin'LIMIT 1 [0m    [1m [35mProfile Exists(0.0ms)[0m SELECT 1 AS one FROM“profiles”WHERE“profiles”。“email”='info@good2use.com'LIMIT 1

调试密码

$ 2A $ 10 $ GJDyoPHEd2 / 2zhEPRxaMvu

$ 2A $ 10 $ GJDyoPHEd2 / 2zhEPRxaMvu6v9j5mrOXt5klPgO0WNYvwGGqwrHH3m

[1m [36mSQL(1.0ms)[0m [1mINSERT INTO“profiles”(“country”,“created_at”,“email”,“name”,“updated_at”,“zip”)VALUES(?,?, ?,?,?,?)[0m [[“country”,“United Kingdom”],[“created_at”,“2014-07-29 11:05:53.912000”],[“email”,“info @ good2use .com“],[”name“,”admin“],[”updated_at“,”2014-07-29 11:05:53.912000“],[”zip“,”b72 1af“]]    [1m [35m(73.0ms)[0m提交事务   在布局/应用程序(277.0ms)内呈现布局/ application.html.erb

如果我尝试从rails控制台中操作表格配置文件中的任何字段,则数据会显示在架构中但是已保存的SQL会将事务回滚 这正在以某种方式驱动Profile模型,因为日志条目被删除

保存后

[1m [36m(1.0ms)[0m [1mbegin transaction [0m   [1m [35mProfile Exists(0.0ms)[0m SELECT 1 AS one FROM“profiles”WHERE“profiles”。“name”='admin1'LIMIT 1   [1m [36mProfile Exists(0.0ms)[0m [1mSELECT 1 AS one FROM“profiles”WHERE“profiles”。“email”IS NULL LIMIT 1 [0m]   [1m [35m(0.0ms)[0m回滚交易

这似乎意味着路由,所以这里是提及个人资料

的路线的唯一部分
    get "sign_up" => "profiles#new", :as => "sign_up"
    get 'profile/autocomplete_interest_name'

希望soemone可以提供帮助。这似乎是一个SQL问题,它让我疯了。 谢谢 艾迪读者

完整型号

class Profile&lt;的ActiveRecord ::基

attr_accessor:password,:encrypted_pa​​ssword,:password_confirmation

验证:name,:presence =&gt; true,:uniqueness =&gt; true,:length =&gt; {:in =&gt; 3..20}

验证:密码,:confirm =&gt;真的

validates_length_of:password,:in =&gt; 6..20,:on =&gt; :创建

验证:email,:presence =&gt; true,:uniqueness =&gt;真

before_save:encrypt_password

logger.info“post save”

after_create:clear_password

def encrypt_password

if password.present?

  password_salt = BCrypt::Engine.generate_salt

  self.password= BCrypt::Engine.hash_secret(password, password_salt)

  logger.info "debug passwords"

  logger.debug password_salt

  logger.debug password

end

def clear_password

 self.password = nil

def self.authenticate(电子邮件,密码)

user = find_by_email(:email)

password_salt = BCrypt::Engine.generate_salt

if user && user.password_hash == BCrypt::Engine.hash_secret(password, password_salt)

  user

else

  nil

end

def self.authenticate_by_email(电子邮件,密码)

user = find_by_email(email)

password_salt = BCrypt::Engine.generate_salt

logger.info "debug salt variable"

logger.debug password_salt

logger.debug password

logger.debug BCrypt::Engine.hash_secret(password,password_salt)

logger.debug user

logger.debug user.password 

logger.debug user.zip

if user && user.password == BCrypt::Engine.hash_secret(password,password_salt)

   user

else

   nil

end

def self.authenticate_by_username(用户名,密码)

 user = find_by_username(username)

 password_salt = BCrypt::Engine.generate_salt

 if user && user.password_hash == BCrypt::Engine.hash_secret(password, user.password_salt)

    user

 else

    nil

 end

0 个答案:

没有答案