2010-11-09 90 views
4

原問題

我在Ruby 1.9.2上運行Rails 3.0.1。這裏是相關的模型,控制器和視圖。爲什麼update_attributes會破壞我的Rails應用程序?

代碼

user.rb:

class User < ActiveRecord::Base 
    belongs_to :directory 

    attr_accessor :new_password, :new_password_confirmation 

    validates_confirmation_of :new_password, :if => :password_changed? 

    before_save :hash_password, :if => :password_changed? 

    def self.authenticate(login, password) 

    # Check to see if the user exists 
    if user = find_by_login(login) 

     # If this is an directory user, authenticate them against their directory 
     if user.directory 
     return directory_auth user, password 

     # Otherwise, authenticate them against the local database 
     elsif user.hash == Digest::SHA2.hexdigest(user.salt + password) 
     return user 
     end 
    end 
    return nil 
    end 

    def password_changed? 
    [email protected]_password.blank? 
    end 

    private 

    def hash_password 
    self.salt = ActiveSupport::SecureRandom.base64 8 
    self.hash = Digest::SHA2.hexdigest(self.salt + @new_password) 
    end 

    def self.directory_auth(user, password) 

    directory = user.directory 
    directory.bind['%s'] = user.login 

    ldap = Net::LDAP.new 
    if directory.use_simple_tls? 
     ldap.encryption :simple_tls 
    end 
    ldap.host = directory.host 
    ldap.port = directory.port 
    ldap.auth directory.bind, password 

    return user if ldap.bind 
    return nil 
    end 
end 

users_controller.rb:

class UsersController < ApplicationController 

    def index 
    @users = User.all 
    end 

    def show 
    @user = User.find params[:id] 
    end 

    def new 
    @user = User.new 
    end 

    def create 
    @user = User.new params[:user] 

    if @user.save 
     flash[:notice] = "#{@user.login} was created" 
     redirect_to @user 
    else 
     flash[:notice] = 'The user could not be created' 
     render :action => 'new' 
    end 
    end 

    def edit 
    @user = User.find params[:id] 
    end 

    def update 
    @user = User.find params[:id] 

    @user.attributes = params[:user] # this works 
    # @user.update_attributes params[:user] # this does NOT work 

    if @user.save # I realize this is redundant if update_attributes is working 
     flash[:notice] = "#{@user.login} was updated" 
     redirect_to @user 
    else 
     flash[:notice] = 'The user could not be updated' 
     render :action => 'edit' 
    end 
    end 

    def destroy 
    @user = User.find params[:id] 
    @user.destroy 

    flash[:notice] = "#{@user.login} was deleted" 
    redirect_to users_url 
    end 
end 

users.html.erb:

<%= form_for @user do |f| %> 
    <%= f.label :login %>: 
    <%= f.text_field :login %> 
    <br> 
    <%= f.label :new_password %>: 
    <%= f.password_field :new_password %> 
    <br> 
    <%= f.label :new_password_confirmation %>: 
    <%= f.password_field :new_password_confirmation %> 
    <br> 
    <% if directories = Directory.all.empty? %> 
    No directories defined. You can <%= link_to 'add a directory', new_directory_path %>. 
    <% else %> 
    <%= f.label :directory_id %>: 
    <%= f.collection_select :directory_id, Directory.all, :id, :name, { :include_blank => 'None' } %> 
    <% end %> 
    <br> 
    <%= f.label :admin, 'Administrator?' %>: 
    <%= f.check_box :admin %> 
    <br> 
    <%= f.submit %> 
<% end %> 

schema.rb:

ActiveRecord::Schema.define(:version => 20101107005603) do 

    create_table "directories", :force => true do |t| 
    t.string "host" 
    t.string "bind" 
    t.boolean "use_simple_tls" 
    t.integer "port" 
    t.string "name" 
    t.datetime "created_at" 
    t.datetime "updated_at" 
    end 

    create_table "users", :force => true do |t| 
    t.string "login" 
    t.string "hash" 
    t.string "salt" 
    t.boolean "admin" 
    t.integer "directory_id" 
    t.datetime "created_at" 
    t.datetime "updated_at" 
    end 

end 

我有一個目錄,這是非常相似的另一種模式/控制器/視圖,但沒有虛擬存取或其他型號ID,並在update_attributes正常工作。我做了一個快速測試應用程序rails g scaffold users name:string password:string和所有CRUD操作正常。

這讓我瘋狂!我找到了一個解決方法,但我真的很想理解爲什麼update_attributes在這裏不起作用。當我運行更新的動作,我得到這個:

類型錯誤在UsersController#更新

不能轉換成零整數

Rails.root:/家庭/力量/凸出

應用/controllers/users_controller.rb:34:in'更新」

完整的堆棧跟蹤

 
activerecord (3.0.1) lib/active_record/connection_adapters/abstract/database_statements.rb:318:in `uniq' 
activerecord (3.0.1) lib/active_record/connection_adapters/abstract/database_statements.rb:318:in `commit_transaction_records' 
activerecord (3.0.1) lib/active_record/connection_adapters/abstract/database_statements.rb:165:in `transaction' 
activerecord (3.0.1) lib/active_record/transactions.rb:204:in `transaction' 
activerecord (3.0.1) lib/active_record/transactions.rb:287:in `with_transaction_returning_status' 
activerecord (3.0.1) lib/active_record/persistence.rb:126:in `update_attributes' 
app/controllers/users_controller.rb:34:in `update' 
actionpack (3.0.1) lib/action_controller/metal/implicit_render.rb:4:in `send_action' 
actionpack (3.0.1) lib/abstract_controller/base.rb:150:in `process_action' 
actionpack (3.0.1) lib/action_controller/metal/rendering.rb:11:in `process_action' 
actionpack (3.0.1) lib/abstract_controller/callbacks.rb:18:in `block in process_action' 
activesupport (3.0.1) lib/active_support/callbacks.rb:435:in `_run__805567340__process_action__482539529__callbacks' 
activesupport (3.0.1) lib/active_support/callbacks.rb:409:in `_run_process_action_callbacks' 
activesupport (3.0.1) lib/active_support/callbacks.rb:93:in `run_callbacks' 
actionpack (3.0.1) lib/abstract_controller/callbacks.rb:17:in `process_action' 
actionpack (3.0.1) lib/action_controller/metal/instrumentation.rb:30:in `block in process_action' 
activesupport (3.0.1) lib/active_support/notifications.rb:52:in `block in instrument' 
activesupport (3.0.1) lib/active_support/notifications/instrumenter.rb:21:in `instrument' 
activesupport (3.0.1) lib/active_support/notifications.rb:52:in `instrument' 
actionpack (3.0.1) lib/action_controller/metal/instrumentation.rb:29:in `process_action' 
actionpack (3.0.1) lib/action_controller/metal/rescue.rb:17:in `process_action' 
actionpack (3.0.1) lib/abstract_controller/base.rb:119:in `process' 
actionpack (3.0.1) lib/abstract_controller/rendering.rb:40:in `process' 
actionpack (3.0.1) lib/action_controller/metal.rb:133:in `dispatch' 
actionpack (3.0.1) lib/action_controller/metal/rack_delegation.rb:14:in `dispatch' 
actionpack (3.0.1) lib/action_controller/metal.rb:173:in `block in action' 
actionpack (3.0.1) lib/action_dispatch/routing/route_set.rb:62:in `call' 
actionpack (3.0.1) lib/action_dispatch/routing/route_set.rb:62:in `dispatch' 
actionpack (3.0.1) lib/action_dispatch/routing/route_set.rb:27:in `call' 
rack-mount (0.6.13) lib/rack/mount/route_set.rb:148:in `block in call' 
rack-mount (0.6.13) lib/rack/mount/code_generation.rb:93:in `block in recognize' 
rack-mount (0.6.13) lib/rack/mount/code_generation.rb:103:in `optimized_each' 
rack-mount (0.6.13) lib/rack/mount/code_generation.rb:92:in `recognize' 
rack-mount (0.6.13) lib/rack/mount/route_set.rb:139:in `call' 
actionpack (3.0.1) lib/action_dispatch/routing/route_set.rb:492:in `call' 
actionpack (3.0.1) lib/action_dispatch/middleware/best_standards_support.rb:17:in `call' 
actionpack (3.0.1) lib/action_dispatch/middleware/head.rb:14:in `call' 
rack (1.2.1) lib/rack/methodoverride.rb:24:in `call' 
actionpack (3.0.1) lib/action_dispatch/middleware/params_parser.rb:21:in `call' 
actionpack (3.0.1) lib/action_dispatch/middleware/flash.rb:182:in `call' 
actionpack (3.0.1) lib/action_dispatch/middleware/session/abstract_store.rb:149:in `call' 
actionpack (3.0.1) lib/action_dispatch/middleware/cookies.rb:287:in `call' 
activerecord (3.0.1) lib/active_record/query_cache.rb:32:in `block in call' 
activerecord (3.0.1) lib/active_record/connection_adapters/abstract/query_cache.rb:28:in `cache' 
activerecord (3.0.1) lib/active_record/query_cache.rb:12:in `cache' 
activerecord (3.0.1) lib/active_record/query_cache.rb:31:in `call' 
activerecord (3.0.1) lib/active_record/connection_adapters/abstract/connection_pool.rb:355:in `call' 
actionpack (3.0.1) lib/action_dispatch/middleware/callbacks.rb:46:in `block in call' 
activesupport (3.0.1) lib/active_support/callbacks.rb:415:in `_run_call_callbacks' 
actionpack (3.0.1) lib/action_dispatch/middleware/callbacks.rb:44:in `call' 
rack (1.2.1) lib/rack/sendfile.rb:107:in `call' 
actionpack (3.0.1) lib/action_dispatch/middleware/remote_ip.rb:48:in `call' 
actionpack (3.0.1) lib/action_dispatch/middleware/show_exceptions.rb:46:in `call' 
railties (3.0.1) lib/rails/rack/logger.rb:13:in `call' 
rack (1.2.1) lib/rack/runtime.rb:17:in `call' 
activesupport (3.0.1) lib/active_support/cache/strategy/local_cache.rb:72:in `call' 
rack (1.2.1) lib/rack/lock.rb:11:in `block in call' 
:10:in `synchronize' 
rack (1.2.1) lib/rack/lock.rb:11:in `call' 
actionpack (3.0.1) lib/action_dispatch/middleware/static.rb:30:in `call' 
railties (3.0.1) lib/rails/application.rb:168:in `call' 
railties (3.0.1) lib/rails/application.rb:77:in `method_missing' 
railties (3.0.1) lib/rails/rack/log_tailer.rb:14:in `call' 
rack (1.2.1) lib/rack/content_length.rb:13:in `call' 
rack (1.2.1) lib/rack/handler/webrick.rb:52:in `service' 
/home/force/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/1.9.1/webrick/httpserver.rb:111:in `service' 
/home/force/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/1.9.1/webrick/httpserver.rb:70:in `run' 
/home/force/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/1.9.1/webrick/server.rb:183:in `block in start_thread' 

請求參數

 
{"utf8"=>"✓", 
"_method"=>"put", 
"authenticity_token"=>"OvMUeM9hqfPASC0NS+Th07GELu6B+dQCCTtm3gWdJE4=", 
"user"=>{"login"=>"local", 
"new_password"=>"[FILTERED]", 
"new_password_confirmation"=>"[FILTERED]", 
"directory_id"=>"", 
"admin"=>"0"}, 
"commit"=>"Update User", 
"id"=>"13"} 

完整的源代碼

如果您想嘗試一下,你可以在http://github.com/sidewaysmilk/deezy下載完整的源代碼和編輯app/controllers/users_controller.rbupdate行動使用@user.update_attributes params[:user]

+0

您的模式對於您的用戶模型是什麼樣的? – raidfive 2010-11-09 00:44:36

+0

添加到問題。謝謝。 – 2010-11-09 01:11:37

+0

您可以添加什麼時候提交表單的參數嗎? (來自服務器日誌) – monocle 2010-11-09 02:37:04

回答

5

好的。我感覺真的很愚蠢。我很驚訝沒有人抓到這個。

在我的模型中,我命名了我的一個屬性。 hash,所以要訪問它,我會說@user.hash

ActiveRecord :: Base#hash是already defined

所以我搞砸了。當ActiveRecord的被試圖執行該交易,它試圖設置一個值等

@user.hash = password_hash 

ActiveRecord::Base#hash=期望的整數,並且password_hash輸出如果密碼被改變字符串,和否則無。

所以永遠不要命名列哈希!當您選擇列名以避免衝突時請檢查文檔。

+1

保存我的一天! '哈希'現在在我的心理黑名單上的任何類型的變量或字段:) – 2010-12-16 16:14:36

+1

同樣問題在這裏,投票了! – 2012-02-10 09:53:15

1

如果沒有完整的堆棧跟蹤,很難說,但錯誤消息「無法將nil轉換爲整數」是追蹤的關鍵。同樣,我們希望查看失敗請求的日誌。我認爲日誌的參數:行可能會說明問題。

更新下面:

你能順利通過相同PARAMS爲@ user.update_attributes在軌控制檯?

rails console 

user = User.find(42) # whatever a good test user's id is 
user_params = {"login"=>"local", "new_password"=>"supersecret", "new_password_confirmation"=>"supersecret", "directory_id"=>"", "admin"=>"0"} 
user.update_attributes!(user_params) 

這種行爲有什麼不同嗎?

+0

當然。對不起。當我發佈這個問題時,我非常疲憊。我應該包含堆棧跟蹤。我現在就更新它。 – 2010-11-09 17:06:42

+0

我應該首先提到使用堆棧跟蹤,我能夠跟蹤update_attributes的問題,但除此之外我無法得到任何進一步的信息。我不知道所謂的'nil'是什麼。我想這可能與我的'attr_accessor'有關,因爲如果我設置了密碼,它開始說它不能將** String **(而不是nil)轉換爲Integer。 – 2010-11-09 17:18:10

+1

我克隆了github的源代碼,並將用戶控制器的更新操作更改爲使用update_attributes,並且對我來說工作正常。這是與Ruby 1.8.7。 – jwarchol 2010-11-09 21:16:58

1

它看起來像什麼可能是問題是,用戶ID是一個主鍵,並以某種方式試圖更新。如果您使用'bang version of update_attributes : update_attributes!`,您會得到什麼錯誤消息?

+0

如果我使用'update_attributes!',我會得到完全相同的行爲。我會看看我可以找出有關正在更新的@ user.id。這是一個很好的結果。我不知道爲什麼我的表單將@ user.id作爲參數提交。有任何想法嗎? – 2010-11-09 17:54:05

+0

看起來像提交id是正常行爲。我在使用腳手架的全新Rails應用程序中看到了相同的行爲。 – 2010-11-09 18:01:10

相關問題