1

所以我有一个接受 json 请求的 rails 服务器。我打算将它用作我的 iphone 应用程序的服务器。无论如何我都会注册失败。

我的用户控制器是

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

我发送的网址是

http://localhost:3000/sign_up.json?username=chalami&password=chalami&name=chalami&email=3020@gmail.com

我通过浏览器而不是通过应用程序发送了网址。我想先通过浏览器测试它。

通过发布的网址,我得到一个

{"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:rake 路线的输出:

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=3020@gmail.com" 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"=>"3020@gmail.com"}
   (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 路由中看到有一个带有 url 的 POST 方法/users。所以我做了一个卷曲

 curl -v -H "Accept: application/json" -H "Content-type: application/json" -X POST -d ' {"user":{"name":"name","username":"username","email":"email@email.com","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@email.com&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
4

1 回答 1

0

从您的错误中可以看出Can't mass-assign protected attributes: password,从您的用户模型的外观来看,您已经制作:passwordattr_accessor. 了解两者之间的差异很重要。访问器是 Ruby 中的一个内置事物,它拒绝使用 getter 方法,以便可以读取属性。而attr_accessibleActiveRecord 批量分配方法。这样做的目的是允许您将值传递给质量分配。您可能会受益于阅读以下内容:MassAssignmentSecurity。所以我建议你做的是attr_accessor :password改变attr_accessible :password

于 2013-05-27T18:28:22.187 回答