私はRails 3.1で簡単なjson APIを構築しています。私はRails respond_withは異なるインデックスと作成メソッドを扱います
namespace :api do
resources :dogs
end
私はhttp://localhost:3000/api/dogsにGET要求を行うとき、私は上から正しいJSONを取得していroutes.rbをで
class Api::DogsController < ActionController::Base
respond_to :json, :xml
def index
respond_with({:msg => "success"})
end
def create
respond_with({:msg => "success"})
end
end
:私は、2つの機能を持つコントローラを作成しました。
ArgumentError in Api::DogsController#create
Nil location provided. Can't build URI.
actionpack (3.1.0) lib/action_dispatch/routing/polymorphic_routes.rb:183:in `build_named_route_call`
actionpack (3.1.0) lib/action_dispatch/routing/polymorphic_routes.rb:120:in `polymorphic_url'
actionpack (3.1.0) lib/action_dispatch/routing/url_for.rb:145:in `url_for'
をしかし、私は
def create
respond_with do |format|
format.json { render :json => {:msg => "success"}}
end
end
に作成したコードを変更した場合、それはうまくJSONを返す:私は同じURLにポストを作るとき、私はレールの例外を取得します。
誰かがここで何が起こっているのか説明できますか?
場所を文字列リテラル "nil"に設定すると、「respond_with({:error => "bad"}、:status => 401、:location => "nil") ' –
@kwbeam最後の段落の実際のゼロ値(私は今引用符が誤解を招くかもしれないと分かっています) – Aubergine
文字列リテラルではなく、オブジェクトがゼロであると思っていましたが、試しても失敗しました。しかし、私はそれをnil(文字列リテラルではなくオブジェクト)に戻しただけで、動作しています。だから明らかに私はどこかで失敗しましたが、私が間違ったことを再構築することはできません。混乱させて申し訳ありません。 (私は暫定的にRailsのバージョンと多くの宝石を更新していたので、おそらく修正されたバグがあったかもしれません...しかし、わかりません) –