Json形式のAPIによる複数レコード作成
rails+AngularJSでAPIを利用して投稿に複数のタグを同時につけることやろうとしていますが、
うまくいきません。アドバイスいただけないでしょうか。
<やりたいこと>
ポストに対して1回のAPIで複数のタグレコードを検索、作成する。
<POSTするJsonイメージ>
[{name: 'tag1'},{name: 'tag2'}]
<APIの処理>
#ポストに紐づくタグの更新
resource "posts/:id/tag" do
desc "投稿に紐づくタグ作成"
params do
requires :id, type: Integer, desc: "Post id."
end
post do
@post = Post.find(params[:id])
params[:name].each do |name|
@post.tags.find_or_create_by({name: name)
end
end
end
上記の処理で下記のようなNoMethodErrorのエラーがでてしまいます。
そちら酒精したのですが、治らず。。。
下記のようなエラーが出ています。
NoMethodError at /api/v1/posts/1/tag
==================================== > undefined method `each' for "tag1":String app/api/aplication/api.rb, line 103
-------------------------------
``` ruby
98 params do
99 requires :id, type: Integer, desc: "Post id."
100 end
101 post do
102 @post = Post.find(params[:id])
> 103 params[:name].each do |name|
104 @post.tags.find_or_create_by({
105 name: name
106 })
107 end
108 end
``` App backtrace -------------
app/api/cospic/api.rb:103:in `block (2 levels) in ' Full backtrace --------------
app/api/cospic/api.rb:103:in `block (2 levels) in '
grape (0.10.1) lib/grape/endpoint.rb:47:in `block in generate_api_method'
grape (0.10.1) lib/grape/endpoint.rb:247:in `run'
grape (0.10.1) lib/grape/endpoint.rb:195:in `block in call!'
grape (0.10.1) lib/grape/middleware/base.rb:24:in `call!'
grape (0.10.1) lib/grape/middleware/base.rb:18:in `call'
grape (0.10.1) lib/grape/middleware/base.rb:24:in `call!'
grape (0.10.1) lib/grape/middleware/base.rb:18:in `call'
grape (0.10.1) lib/grape/middleware/error.rb:27:in `block in call!'
grape (0.10.1) lib/grape/middleware/error.rb:26:in `call!'
grape (0.10.1) lib/grape/middleware/base.rb:18:in `call'
rack (1.5.2) lib/rack/head.rb:11:in `call'
rack (1.5.2) lib/rack/builder.rb:138:in `call'
grape (0.10.1) lib/grape/endpoint.rb:196:in `call!'
grape (0.10.1) lib/grape/endpoint.rb:184:in `call'
rack-mount (0.8.3) lib/rack/mount/route_set.rb:152:in `block in call'
rack-mount (0.8.3) lib/rack/mount/code_generation.rb:96:in `block in recognize'
rack-mount (0.8.3) lib/rack/mount/code_generation.rb:68:in `optimized_each'
rack-mount (0.8.3) lib/rack/mount/code_generation.rb:95:in `recognize'
rack-mount (0.8.3) lib/rack/mount/route_set.rb:141:in `call'
grape (0.10.1) lib/grape/api.rb:102:in `call'
grape (0.10.1) lib/grape/api.rb:33:in `call!'
grape (0.10.1) lib/grape/api.rb:29:in `call'
actionpack (4.1.6) lib/action_dispatch/journey/router.rb:73:in `block in call'
actionpack (4.1.6) lib/action_dispatch/journey/router.rb:59:in `call'
actionpack (4.1.6) lib/action_dispatch/routing/route_set.rb:678:in `call'
omniauth (1.2.2) lib/omniauth/strategy.rb:186:in `call!'
omniauth (1.2.2) lib/omniauth/strategy.rb:164:in `call'
omniauth (1.2.2) lib/omniauth/builder.rb:59:in `call'
rack (1.5.2) lib/rack/config.rb:17:in `call'
warden (1.2.3) lib/warden/manager.rb:35:in `block in call'
warden (1.2.3) lib/warden/manager.rb:34:in `call'
rack (1.5.2) lib/rack/etag.rb:23:in `call'
rack (1.5.2) lib/rack/conditionalget.rb:35:in `call'
rack (1.5.2) lib/rack/head.rb:11:in `call'
actionpack (4.1.6) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
actionpack (4.1.6) lib/action_dispatch/middleware/flash.rb:254:in `call'
rack (1.5.2) lib/rack/session/abstract/id.rb:225:in `context'
rack (1.5.2) lib/rack/session/abstract/id.rb:220:in `call'
actionpack (4.1.6) lib/action_dispatch/middleware/cookies.rb:560:in `call'
activerecord (4.1.6) lib/active_record/query_cache.rb:36:in `call'
activerecord (4.1.6) lib/active_record/connection_adapters/abstract/connection_pool.rb:621:in `call'
activerecord (4.1.6) lib/active_record/migration.rb:380:in `call'
actionpack (4.1.6) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
activesupport (4.1.6) lib/active_support/callbacks.rb:82:in `run_callbacks'
actionpack (4.1.6) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (4.1.6) lib/action_dispatch/middleware/reloader.rb:73:in `call'
actionpack (4.1.6) lib/action_dispatch/middleware/remote_ip.rb:76:in `call'
better_errors (2.1.1) lib/better_errors/middleware.rb:84:in `protected_app_call'
better_errors (2.1.1) lib/better_errors/middleware.rb:79:in `better_errors_call'
better_errors (2.1.1) lib/better_errors/middleware.rb:57:in `call'
actionpack (4.1.6) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
actionpack (4.1.6) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
railties (4.1.6) lib/rails/rack/logger.rb:38:in `call_app'
railties (4.1.6) lib/rails/rack/logger.rb:20:in `block in call'
activesupport (4.1.6) lib/active_support/tagged_logging.rb:68:in `block in tagged'
activesupport (4.1.6) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (4.1.6) lib/active_support/tagged_logging.rb:68:in `tagged'
railties (4.1.6) lib/rails/rack/logger.rb:20:in `call'
actionpack (4.1.6) lib/action_dispatch/middleware/request_id.rb:21:in `call'
rack (1.5.2) lib/rack/methodoverride.rb:21:in `call'
rack (1.5.2) lib/rack/runtime.rb:17:in `call'
activesupport (4.1.6) lib/active_support/cache/strategy/local_cache_middleware.rb:26:in `call'
rack (1.5.2) lib/rack/lock.rb:17:in `call'
actionpack (4.1.6) lib/action_dispatch/middleware/static.rb:64:in `call'
rack (1.5.2) lib/rack/sendfile.rb:112:in `call'
railties (4.1.6) lib/rails/engine.rb:514:in `call'
railties (4.1.6) lib/rails/application.rb:144:in `call'
rack (1.5.2) lib/rack/lock.rb:17:in `call'
rack (1.5.2) lib/rack/content_length.rb:14:in `call'
rack (1.5.2) lib/rack/handler/webrick.rb:60:in `service'
/Users/masah/.rbenv/versions/2.1.4/lib/ruby/2.1.0/webrick/httpserver.rb:138:in `service'
/Users/masah/.rbenv/versions/2.1.4/lib/ruby/2.1.0/webrick/httpserver.rb:94:in `run'
/Users/masah/.rbenv/versions/2.1.4/lib/ruby/2.1.0/webrick/server.rb:295:in `block in start_thread'a
よろしくお願いします。