7923
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
    
```

よろしくお願いします。
CC BY-SA 3.0
2015-03-14T17:23:18.920
7918
2015-03-15T03:25:57.407
2015-03-15T03:17:13.777
85
5891
question
ruby-on-rails api
Json形式のAPIによる複数レコード作成
JSON側とRuby側でKeyが異なる（`tag_name`と`name`になっている）点と、Ruby側で`params_tags`をハッシュのように扱っている点が怪しい気がします。

Keyを一致させて、かつ渡されたパラメータをeachで回す部分を修正するとこんな感じになると思います。

```

    params[:tag_name].each do |name|
      # ブロック引数のnameはStringであるはず
      @post.tags.find_or_create_by(name: name)
    end
    
```

それでも直らない場合はlog/development.logを開き、リクエスト発生（`Started
POST`以降）からエラーで出力されるスタックトレースの最後までを追記してください。

### 追記

ログを見たところ、パラメータの送信もしくはRuby側のパラメータの扱いが意図した通りに行われていないようです。（`params[:name]`の中身が配列ではなくただの文字列になっている）

ログの中の`Started POST ...`から`Parameters: ...`の部分を確認すると、渡されたパラメータの中身を確認できます。

ログの出力例

```

    Started POST "/news_releases" for 127.0.0.1 at 2015-03-15 09:59:09 +0900
    Processing by NewsReleasesController#create as HTML
      Parameters: {"utf8"=>"✓", "news_release"=>{"title"=>"BigCo switches to Rails", "released_on"=>"2013-07-29", "body"=>"BigCo has released a new website built with open source."}, "commit"=>"Create News release"}
    
```

ログを見ても問題が解決しない場合は、その部分を追記してください。  
（解決した場合もその旨追記してくださいね）
CC BY-SA 3.0
2015-03-14T20:50:57.827
7923
2015-03-15T03:25:57.407
2015-03-15T03:25:57.407
85
85
7918
answer