1

だから私はjsonリクエストを受け入れるレールサーバーを持っています。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

私が送ったURLは

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

アプリからではなく、ブラウザから URL を送信しました。最初にブラウザでテストしたかったのです。

投稿された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

私のデータベースは空なので、電子メールまたはユーザー名のいずれかが取得されることはありません

EDIT 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:: コメントで示唆されているように、GET の代わりに POST を使用し、curl を使用する必要があります。url を使用した POST メソッドがあることが私の rake ルートに表示され/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