0

I have the following rails route

resources :apps do
  post "name/:text/,
     :on => :member, :action => :register, :as => :register,
     :text => /[^\/]+/

end

The url that hits it in json format is the following:

curl -v -s -H "Content-type: application/json" -d '' -X POST http://0.0.0.0:3000/apps/1/name/hello.json

When my rails controller receives the request above, it can't see that the .json at the end of the url is part of the url encoding/extension and not part of hello. It sees the params as the following:

Parameters: {"id"=>"1", "text"=>"hello.json"}

instead of having "text" => "hello".

I also have respond_to :json in my AppsController.

Any idea why this happens? and how to make rails parse the .json out?

Matilda
  • 1,708
  • 3
  • 25
  • 33

2 Answers2

1

Two things:

  • You didn't include the format part in your route, i.e: /path/to(.:format)
  • I'm not really sure what your regex does, but you may want to review it

I tried the following and it seems to work fine:

resources :apps do
  post "name/:text(.:format)",
     :on => :member, :action => :register, :as => :register,
     :text => /[a-z]+/

end
Hesham
  • 2,327
  • 2
  • 20
  • 24
  • So my text could have any . or - or any other character in it. the (.:format) didn't work for me, but thanks for pointing me to :format, I did `:format => true` in my routes and it did the job. Thanks! – Matilda Feb 21 '14 at 21:35
0

Doing :format => true in the route fixed this issue for me. Looks like adding (.:format) worked for @H-man.

Also this is the reference for :format http://guides.rubyonrails.org/v3.2.13/routing.html#route-globbing

However setting :format => true forces the format to be always presented, and :format => false pretty much ignores the format, which is what's happening now.

I realized the issue was with my regex /[^\/]+/ matches everything besides / which matches .json as well.

Matilda
  • 1,708
  • 3
  • 25
  • 33