2013-05-27 90 views
1

所以我有一個接受json請求的rails服務器。我打算將它用作我的iPhone應用程序的服務器。無論如何,我都會遇到註冊失敗。rails服務器註冊失敗

我的用戶控制器是

class UsersController < ApplicationController 
    respond_to :json 

    def create 
    @user = User.new(params[:user]) 
    if @user.save 
     render :json => {:status => "200", :message => "Signed up successfully"} 
    else 
     render :json => {:status => "400", :message => "Failed"} 
    end 
    end 
end 

我派中的URL

http://localhost:3000/sign_up.json?username=chalami&password=chalami&name=chalami&[email protected] 

我在url中通過我的瀏覽器,而不是通過應用程序發送。我想先通過瀏覽器測試它。

隨着貼出網址,我得到一個

{"status":"400","message":"Failed"} 

這不應該發生。 這裏是我的user.rb

class User < ActiveRecord::Base 
    attr_accessible :email, :name, :username 
    attr_accessor :password 
    before_save :encrypt_password 

    validates_uniqueness_of :username, :email 
    validates_presence_of :password, :on => :create 
    validates_presence_of :email, :username 

    def encrypt_password 
    if password.present? 
     self.password_salt = BCrypt::Engine.generate_salt 
     self.password_hash = BCrypt::Engine.hash_secret(password, password_salt) 
    end 
    end 
end 

我的數據庫是空的,所以它不能是電子郵件或用戶名已被

編輯1:從耙路線 輸出:

sign_up GET /sign_up(.:format)  users#create 
index GET /index(.:format)   users#index 
root  /      users#index 
users GET /users(.:format)   users#index 
     POST /users(.:format)   users#create 
new_user GET /users/new(.:format)  users#new 
edit_user GET /users/:id/edit(.:format) users#edit 
user GET /users/:id(.:format)  users#show 
     PUT /users/:id(.:format)  users#update 
     DELETE /users/:id(.:format)  users#destroy 

這裏的日誌

Started GET "/sign_up.json?username=chalami&password= [FILTERED]&name=chalami&[email protected]" for 127.0.0.1 at 2013-05-28 00:40:44 +0700 
Processing by UsersController#create as JSON 
Parameters: {"username"=>"chalami", "password"=>"[FILTERED]", "name"=>"chalami", "email"=>"[email protected]"} 
    (0.0ms) begin transaction 
    User Exists (0.1ms) SELECT 1 AS one FROM "users" WHERE "users"."username" IS NULL LIMIT 1 
    User Exists (0.0ms) SELECT 1 AS one FROM "users" WHERE "users"."email" IS NULL LIMIT 1 
(0.0ms) rollback transaction 
Completed 200 OK in 11ms (Views: 0.2ms | ActiveRecord: 0.6ms) 
[2013-05-28 00:40:44] WARN Could not determine content-length of response body. Set  content-length of the response or set Response#chunked = true 

編輯2 :: 正如評論中的建議,我應該使用POST而不是GET,並且應該使用curl。我在我的rake路由中看到有一個POST方法,其URL爲/users。 所以我對

curl -v -H "Accept: application/json" -H "Content-type: application/json" -X POST -d ' {"user":{"name":"name","username":"username","email":"[email protected]","password":"app123"}}' http://localhost:3000/users 

捲曲,我得到的輸出的整個負載,但不能創建一個用戶。下面是輸出:

' http://localhost:3000/users 
* About to connect() to localhost port 3000 (#0) 
* Trying ::1... 
* Connection refused 
* Trying 127.0.0.1... 
* connected 
* Connected to localhost (127.0.0.1) port 3000 (#0) 
> POST /users HTTP/1.1 
> User-Agent: curl/7.24.0 (x86_64-apple-darwin12.0) libcurl/7.24.0 OpenSSL/0.9.8r zlib/1.2.5 
> Host: localhost:3000 
> Accept: application/json 
> Content-type: application/json 
> Content-Length: 93 
> 
* upload completely sent off: 93 out of 93 bytes 
< HTTP/1.1 500 Internal Server Error 
< Content-Type: text/html; charset=utf-8 
< Content-Length: 15954 
< X-Request-Id: 002af4460c3128e7a78bd10362bcb30f 
< X-Runtime: 0.108395 
< Server: WEBrick/1.3.1 (Ruby/1.9.3/2012-11-10) 
< Date: Mon, 27 May 2013 17:56:09 GMT 
< Connection: Keep-Alive 
< 
<!DOCTYPE html> 
<html lang="en"> 
<head> 
<meta charset="utf-8" /> 
<title>Action Controller: Exception caught</title> 
<style> 
body { background-color: #fff; color: #333; } 

body, p, ol, ul, td { 
    font-family: helvetica, verdana, arial, sans-serif; 
    font-size: 13px; 
    line-height: 18px; 
} 

pre { 
    background-color: #eee; 
    padding: 10px; 
    font-size: 11px; 
    white-space: pre-wrap; 
} 

a { color: #000; } 
a:visited { color: #666; } 
a:hover { color: #fff; background-color:#000; } 
</style> 
</head> 
<body> 

<h1> 
    ActiveModel::MassAssignmentSecurity::Error 
    in UsersController#create 
</h1> 
<pre>Can&#x27;t mass-assign protected attributes: password</pre> 


<p><code>Rails.root: /Users/chalamphongpandey/rails_projects/comboo</code></p> 

<div id="traces"> 
    <a href="#" onclick="document.getElementById(&#x27;Framework-Trace&#x27;).style.display=&#x27;none&#x27;;document.getElementById(&#x27;Full-Trace&#x27;).style.display=&#x27;none&#x27;;document.getElementById(&#x27;Application-Trace&#x27;).style.display=&#x27;block&#x27;;; return false;">Application Trace</a> | 
    <a href="#" onclick="document.getElementById(&#x27;Application-Trace&#x27;).style.display=&#x27;none&#x27;;document.getElementById(&#x27;Full-Trace&#x27;).style.display=&#x27;none&#x27;;document.getElementById(&#x27;Framework-Trace&#x27;).style.display=&#x27;block&#x27;;; return false;">Framework Trace</a> | 
    <a href="#" onclick="document.getElementById(&#x27;Application-Trace&#x27;).style.display=&#x27;none&#x27;;document.getElementById(&#x27;Framework-Trace&#x27;).style.display=&#x27;none&#x27;;document.getElementById(&#x27;Full-Trace&#x27;).style.display=&#x27;block&#x27;;; return false;">Full Trace</a> 

<div id="Application-Trace" style="display: block;"> 
    <pre><code>app/controllers/users_controller.rb:8:in `new&#x27; 
app/controllers/users_controller.rb:8:in `create&#x27;</code></pre> 
</div> 
<div id="Framework-Trace" style="display: none;"> 
    <pre><code>activemodel (3.2.13) lib/active_model/mass_assignment_security/sanitizer.rb:48:in `process_removed_attributes&#x27; 
activemodel (3.2.13) lib/active_model/mass_assignment_security/sanitizer.rb:20:in `debug_protected_attribute_removal&#x27; 
activemodel (3.2.13) lib/active_model/mass_assignment_security/sanitizer.rb:12:in `sanitize&#x27; 
activemodel (3.2.13) lib/active_model/mass_assignment_security.rb:230:in `sanitize_for_mass_assignment&#x27; 
activerecord (3.2.13) lib/active_record/attribute_assignment.rb:75:in `assign_attributes&#x27; 
activerecord (3.2.13) lib/active_record/base.rb:498:in `initialize&#x27; 
actionpack (3.2.13) lib/action_controller/metal/implicit_render.rb:4:in `send_action&#x27; 
actionpack (3.2.13) lib/abstract_controller/base.rb:167:in `process_action&#x27; 
actionpack (3.2.13) lib/action_controller/metal/rendering.rb:10:in `process_action&#x27; 
actionpack (3.2.13) lib/abstract_controller/callbacks.rb:18:in `block in process_action&#x27; 
activesupport (3.2.13) lib/active_support/callbacks.rb:414:in `_run__1456629456087302299__process_action__2823567861297586124__callbacks&#x27; 
activesupport (3.2.13) lib/active_support/callbacks.rb:405:in `__run_callback&#x27; 
activesupport (3.2.13) lib/active_support/callbacks.rb:385:in `_run_process_action_callbacks&#x27; 
activesupport (3.2.13) lib/active_support/callbacks.rb:81:in `run_callbacks&#x27; 
actionpack (3.2.13) lib/abstract_controller/callbacks.rb:17:in `process_action&#x27; 
actionpack (3.2.13) lib/action_controller/metal/rescue.rb:29:in `process_action&#x27; 
actionpack (3.2.13) lib/action_controller/metal/instrumentation.rb:30:in `block in process_action&#x27; 
activesupport (3.2.13) lib/active_support/notifications.rb:123:in `block in instrument&#x27; 
activesupport (3.2.13) lib/active_support/notifications/instrumenter.rb:20:in `instrument&#x27; 
activesupport (3.2.13) lib/active_support/notifications.rb:123:in `instrument&#x27; 
actionpack (3.2.13) lib/action_controller/metal/instrumentation.rb:29:in `process_action&#x27; 
actionpack (3.2.13) lib/action_controller/metal/params_wrapper.rb:207:in `process_action&#x27; 
activerecord (3.2.13) lib/active_record/railties/controller_runtime.rb:18:in `process_action&#x27; 
actionpack (3.2.13) lib/abstract_controller/base.rb:121:in `process&#x27; 
actionpack (3.2.13) lib/abstract_controller/rendering.rb:45:in `process&#x27; 
actionpack (3.2.13) lib/action_controller/metal.rb:203:in `dispatch&#x27; 
actionpack (3.2.13) lib/action_controller/metal/rack_delegation.rb:14:in `dispatch&#x27; 
actionpack (3.2.13) lib/action_controller/metal.rb:246:in `block in action&#x27; 
actionpack (3.2.13) lib/action_dispatch/routing/route_set.rb:73:in `call&#x27; 
actionpack (3.2.13) lib/action_dispatch/routing/route_set.rb:73:in `dispatch&#x27; 
actionpack (3.2.13) lib/action_dispatch/routing/route_set.rb:36:in `call&#x27; 
journey (1.0.4) lib/journey/router.rb:68:in `block in call&#x27; 
journey (1.0.4) lib/journey/router.rb:56:in `each&#x27; 
journey (1.0.4) lib/journey/router.rb:56:in `call&#x27; 
actionpack (3.2.13) lib/action_dispatch/routing/route_set.rb:612:in `call&#x27; 
warden (1.2.1) lib/warden/manager.rb:35:in `block in call&#x27; 
warden (1.2.1) lib/warden/manager.rb:34:in `catch&#x27; 
warden (1.2.1) lib/warden/manager.rb:34:in `call&#x27; 
actionpack (3.2.13) lib/action_dispatch/middleware/best_standards_support.rb:17:in `call&#x27; 
rack (1.4.5) lib/rack/etag.rb:23:in `call&#x27; 
rack (1.4.5) lib/rack/conditionalget.rb:35:in `call&#x27; 
actionpack (3.2.13) lib/action_dispatch/middleware/head.rb:14:in `call&#x27; 
actionpack (3.2.13) lib/action_dispatch/middleware/params_parser.rb:21:in `call&#x27; 
actionpack (3.2.13) lib/action_dispatch/middleware/flash.rb:242:in `call&#x27; 
rack (1.4.5) lib/rack/session/abstract/id.rb:210:in `context&#x27; 
rack (1.4.5) lib/rack/session/abstract/id.rb:205:in `call&#x27; 
actionpack (3.2.13) lib/action_dispatch/middleware/cookies.rb:341:in `call&#x27; 
activerecord (3.2.13) lib/active_record/query_cache.rb:64:in `call&#x27; 
activerecord (3.2.13) lib/active_record/connection_adapters/abstract/connection_pool.rb:479:in `call&#x27; 
actionpack (3.2.13) lib/action_dispatch/middleware/callbacks.rb:28:in `block in call&#x27; 
activesupport (3.2.13) lib/active_support/callbacks.rb:405:in `_run__806197691580144289__call__628697914765895210__callbacks&#x27; 
activesupport (3.2.13) lib/active_support/callbacks.rb:405:in `__run_callback&#x27; 
activesupport (3.2.13) lib/active_support/callbacks.rb:385:in `_run_call_callbacks&#x27; 
activesupport (3.2.13) lib/active_support/callbacks.rb:81:in `run_callbacks&#x27; 
actionpack (3.2.13) lib/action_dispatch/middleware/callbacks.rb:27:in `call&#x27; 
actionpack (3.2.13) lib/action_dispatch/middleware/reloader.rb:65:in `call&#x27; 
actionpack (3.2.13) lib/action_dispatch/middleware/remote_ip.rb:31:in `call&#x27; 
actionpack (3.2.13) lib/action_dispatch/middleware/debug_exceptions.rb:16:in `call&#x27; 
actionpack (3.2.13) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call&#x27; 
railties (3.2.13) lib/rails/rack/logger.rb:32:in `call_app&#x27; 
railties (3.2.13) lib/rails/rack/logger.rb:16:in `block in call&#x27; 
activesupport (3.2.13) lib/active_support/tagged_logging.rb:22:in `tagged&#x27; 
railties (3.2.13) lib/rails/rack/logger.rb:16:in `call&#x27; 
actionpack (3.2.13) lib/action_dispatch/middleware/request_id.rb:22:in `call&#x27; 
rack (1.4.5) lib/rack/methodoverride.rb:21:in `call&#x27; 
rack (1.4.5) lib/rack/runtime.rb:17:in `call&#x27; 
activesupport (3.2.13) lib/active_support/cache/strategy/local_cache.rb:72:in `call&#x27; 
rack (1.4.5) lib/rack/lock.rb:15:in `call&#x27; 
actionpack (3.2.13) lib/action_dispatch/middleware/static.rb:63:in `call&#x27; 
railties (3.2.13) lib/rails/engine.rb:479:in `call&#x27; 
railties (3.2.13) lib/rails/application.rb:223:in `call&#x27; 
rack (1.4.5) lib/rack/content_length.rb:14:in `call&#x27; 
railties (3.2.13) lib/rails/rack/log_tailer.rb:17:in `call&#x27; 
rack (1.4.5) lib/rack/handler/webrick.rb:59:in `service&#x27; 
/Users/chalamphongpandey/.rvm/rubies/ruby-1.9.3- p327/lib/ruby/1.9.1/webrick/httpserver.rb:138:in `service&#x27; 
/Users/chalamphongpandey/.rvm/rubies/ruby-1.9.3-p327/lib/ruby/1.9.1/webrick/httpserver.rb:94:in `run&#x27; 
/Users/chalamphongpandey/.rvm/rubies/ruby-1.9.3-p327/lib/ruby/1.9.1/webrick/server.rb:191:in `block in start_thread&#x27;</code></pre> 
</div> 
<div id="Full-Trace" style="display: none;"> 
    <pre><code>activemodel (3.2.13) lib/active_model/mass_assignment_security/sanitizer.rb:48:in `process_removed_attributes&#x27; 
activemodel (3.2.13) lib/active_model/mass_assignment_security/sanitizer.rb:20:in `debug_protected_attribute_removal&#x27; 
activemodel (3.2.13) lib/active_model/mass_assignment_security/sanitizer.rb:12:in `sanitize&#x27; 
activemodel (3.2.13) lib/active_model/mass_assignment_security.rb:230:in `sanitize_for_mass_assignment&#x27; 
activerecord (3.2.13) lib/active_record/attribute_assignment.rb:75:in `assign_attributes&#x27; 
activerecord (3.2.13) lib/active_record/base.rb:498:in `initialize&#x27; 
app/controllers/users_controller.rb:8:in `new&#x27; 
app/controllers/users_controller.rb:8:in `create&#x27; 
actionpack (3.2.13) lib/action_controller/metal/implicit_render.rb:4:in `send_action&#x27; 
actionpack (3.2.13) lib/abstract_controller/base.rb:167:in `process_action&#x27; 
actionpack (3.2.13) lib/action_controller/metal/rendering.rb:10:in `process_action&#x27; 
actionpack (3.2.13) lib/abstract_controller/callbacks.rb:18:in `block in process_action&#x27; 
activesupport (3.2.13) lib/active_support/callbacks.rb:414:in `_run__1456629456087302299__process_action__2823567861297586124__callbacks&#x27; 
activesupport (3.2.13) lib/active_support/callbacks.rb:405:in `__run_callback&#x27; 
activesupport (3.2.13) lib/active_support/callbacks.rb:385:in `_run_process_action_callbacks&#x27; 
activesupport (3.2.13) lib/active_support/callbacks.rb:81:in `run_callbacks&#x27; 
actionpack (3.2.13) lib/abstract_controller/callbacks.rb:17:in `process_action&#x27; 
actionpack (3.2.13) lib/action_controller/metal/rescue.rb:29:in `process_action&#x27; 
actionpack (3.2.13) lib/action_controller/metal/instrumentation.rb:30:in `block in process_action&#x27; 
activesupport (3.2.13) lib/active_support/notifications.rb:123:in `block in instrument&#x27; 
activesupport (3.2.13) lib/active_support/notifications/instrumenter.rb:20:in `instrument&#x27; 
activesupport (3.2.13) lib/active_support/notifications.rb:123:in `instrument&#x27; 
actionpack (3.2.13) lib/action_controller/metal/instrumentation.rb:29:in `process_action&#x27; 
actionpack (3.2.13) lib/action_controller/metal/params_wrapper.rb:207:in `process_action&#x27; 
activerecord (3.2.13) lib/active_record/railties/controller_runtime.rb:18:in `process_action&#x27; 
actionpack (3.2.13) lib/abstract_controller/base.rb:121:in `process&#x27; 
actionpack (3.2.13) lib/abstract_controller/rendering.rb:45:in `process&#x27; 
actionpack (3.2.13) lib/action_controller/metal.rb:203:in `dispatch&#x27; 
actionpack (3.2.13) lib/action_controller/metal/rack_delegation.rb:14:in `dispatch&#x27; 
actionpack (3.2.13) lib/action_controller/metal.rb:246:in `block in action&#x27; 
actionpack (3.2.13) lib/action_dispatch/routing/route_set.rb:73:in `call&#x27; 
actionpack (3.2.13) lib/action_dispatch/routing/route_set.rb:73:in `dispatch&#x27; 
actionpack (3.2.13) lib/action_dispatch/routing/route_set.rb:36:in `call&#x27; 
journey (1.0.4) lib/journey/router.rb:68:in `block in call&#x27; 
journey (1.0.4) lib/journey/router.rb:56:in `each&#x27; 
journey (1.0.4) lib/journey/router.rb:56:in `call&#x27; 
actionpack (3.2.13) lib/action_dispatch/routing/route_set.rb:612:in `call&#x27; 
warden (1.2.1) lib/warden/manager.rb:35:in `block in call&#x27; 
warden (1.2.1) lib/warden/manager.rb:34:in `catch&#x27; 
warden (1.2.1) lib/warden/manager.rb:34:in `call&#x27; 
actionpack (3.2.13) lib/action_dispatch/middleware/best_standards_support.rb:17:in `call&#x27; 
rack (1.4.5) lib/rack/etag.rb:23:in `call&#x27; 
rack (1.4.5) lib/rack/conditionalget.rb:35:in `call&#x27; 
actionpack (3.2.13) lib/action_dispatch/middleware/head.rb:14:in `call&#x27; 
actionpack (3.2.13) lib/action_dispatch/middleware/params_parser.rb:21:in `call&#x27; 
actionpack (3.2.13) lib/action_dispatch/middleware/flash.rb:242:in `call&#x27; 
rack (1.4.5) lib/rack/session/abstract/id.rb:210:in `context&#x27; 
rack (1.4.5) lib/rack/session/abstract/id.rb:205:in `call&#x27; 
actionpack (3.2.13) lib/action_dispatch/middleware/cookies.rb:341:in `call&#x27; 
activerecord (3.2.13) lib/active_record/query_cache.rb:64:in `call&#x27; 
activerecord (3.2.13) lib/active_record/connection_adapters/abstract/connection_pool.rb:479:in `call&#x27; 
actionpack (3.2.13) lib/action_dispatch/middleware/callbacks.rb:28:in `block in call&#x27; 
activesupport (3.2.13) lib/active_support/callbacks.rb:405:in `_run__806197691580144289__call__628697914765895210__callbacks&#x27; 
activesupport (3.2.13) lib/active_support/callbacks.rb:405:in `__run_callback&#x27; 
activesupport (3.2.13) lib/active_support/callbacks.rb:385:in `_run_call_callbacks&#x27; 
activesupport (3.2.13) lib/active_support/callbacks.rb:81:in `run_callbacks&#x27; 
actionpack (3.2.13) lib/action_dispatch/middleware/callbacks.rb:27:in `call&#x27; 
actionpack (3.2.13) lib/action_dispatch/middleware/reloader.rb:65:in `call&#x27; 
actionpack (3.2.13) lib/action_dispatch/middleware/remote_ip.rb:31:in `call&#x27; 
actionpack (3.2.13) lib/action_dispatch/middleware/debug_exceptions.rb:16:in `call&#x27; 
actionpack (3.2.13) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call&#x27; 
railties (3.2.13) lib/rails/rack/logger.rb:32:in `call_app&#x27; 
railties (3.2.13) lib/rails/rack/logger.rb:16:in `block in call&#x27; 
activesupport (3.2.13) lib/active_support/tagged_logging.rb:22:in `tagged&#x27; 
railties (3.2.13) lib/rails/rack/logger.rb:16:in `call&#x27; 
actionpack (3.2.13) lib/action_dispatch/middleware/request_id.rb:22:in `call&#x27; 
rack (1.4.5) lib/rack/methodoverride.rb:21:in `call&#x27; 
rack (1.4.5) lib/rack/runtime.rb:17:in `call&#x27; 
activesupport (3.2.13) lib/active_support/cache/strategy/local_cache.rb:72:in `call&#x27; 
rack (1.4.5) lib/rack/lock.rb:15:in `call&#x27; 
actionpack (3.2.13) lib/action_dispatch/middleware/static.rb:63:in `call&#x27; 
railties (3.2.13) lib/rails/engine.rb:479:in `call&#x27; 
railties (3.2.13) lib/rails/application.rb:223:in `call&#x27; 
rack (1.4.5) lib/rack/content_length.rb:14:in `call&#x27; 
railties (3.2.13) lib/rails/rack/log_tailer.rb:17:in `call&#x27; 
rack (1.4.5) lib/rack/handler/webrick.rb:59:in `service&#x27; 
/Users/chalamphongpandey/.rvm/rubies/ruby-1.9.3- p327/lib/ruby/1.9.1/webrick/httpserver.rb:138:in `service&#x27; 
/Users/chalamphongpandey/.rvm/rubies/ruby-1.9.3-p327/lib/ruby/1.9.1/webrick/httpserver.rb:94:in `run&#x27; 
/Users/chalamphongpandey/.rvm/rubies/ruby-1.9.3-p327/lib/ruby/1.9.1/webrick/server.rb:191:in `block in start_thread&#x27;</code></pre> 
    </div> 
</div> 



<h2 style="margin-top: 30px">Request</h2> 
<p><b>Parameters</b>: <pre>{&quot;user&quot;=&gt;{&quot;name&quot;=&gt;&quot;name&quot;, 
&quot;username&quot;=&gt;&quot;username&quot;, 
&quot;email&quot;=&gt;&quot;[email protected]&quot;, 
&quot;password&quot;=&gt;&quot;[FILTERED]&quot;}}</pre></p> 

<p><a href="#" onclick="document.getElementById('session_dump').style.display='block'; return false;">Show session dump</a></p> 
<div id="session_dump" style="display:none"><pre></pre></div> 

<p><a href="#" onclick="document.getElementById('env_dump').style.display='block'; return false;">Show env dump</a></p> 
<div id="env_dump" style="display:none"><pre>GATEWAY_INTERFACE: &quot;CGI/1.1&quot; 
HTTP_ACCEPT: &quot;application/json&quot; 
REMOTE_ADDR: &quot;127.0.0.1&quot; 
REMOTE_HOST: &quot;localhost&quot; 
SERVER_NAME: &quot;localhost&quot; 
SERVER_PROTOCOL: &quot;HTTP/1.1&quot;</pre></div> 


<h2 style="margin-top: 30px">Response</h2> 
<p><b>Headers</b>: <pre>None</pre></p> 



</body> 
</html> 
* Connection #0 to host localhost left intact 
* Closing connection #0 
+0

確認'/ sign_up'是'UsersController'中'create'動作的路由。此外,您可能需要在查詢字符串的電子郵件部分中轉義'@'。 –

+1

您是否正在發送POST或GET請求?你應該發送一個POST。 –

+0

你在哪個rails版本上? – stephenmurdoch

回答

0

從你的錯誤它說,Can't mass-assign protected attributes: password,並從用戶模型的外觀您所做:passwordattr_accessor。瞭解兩者之間的差異很重要。 Accessor是Ruby中的一種內置的方法,它可以剔除getter方法,以便讀取屬性。而attr_accessible是一個ActiveRecord質量分配方法。這樣做的目的是讓你將價值傳遞給大衆。閱讀以下內容可能會使您受益:MassAssignmentSecurity。因此,我建議你做的是將attr_accessor :password更改爲attr_accessible :password

+0

我這樣做,現在我得到一個錯誤'未定義的方法'密碼',我知道這是因爲我沒有在我的用戶模型中的密碼列,但我需要用戶輸入密碼,所以我可以創建一個密碼哈希使用那。我怎麼做? – nupac

+0

@ChalamphongPandey你剛剛回答了你自己的問題。很明顯,您的用戶模型中沒有需要創建的密碼。還應該看看http://ruby.railstutorial.org/chapters/modeling-users#sec-adding_a_secure_password這應該會幫助你 – David

+0

我實際上不想保存用戶密碼在數據庫中,我要檢查真實性使用密碼哈希。有沒有辦法做到這一點?不必創建一個密碼列 – nupac