ruby on rails - Why do I need to specify :location=>some_url(@object) in respond_with in the case of POST -
i implementing restful api using ror. here userscontroller:
class applicationcontroller < actioncontroller::api [...] end module api module v1 class userscontroller < applicationcontroller respond_to :json def create @user = user.new(user_params) @user.save respond_with @user, :location => api_v1_user_url(@user) end def show @user = user.find(params[:id]) respond_with @user end end end end if omit @user param :location => api_v1_user_url(@user) in userscontroller#create i.e. have so:
respond_with @user, :location => api_v1_user_url i following error:
actioncontroller::urlgenerationerror in api::v1::userscontroller#create no route matches {:action=>"show", :controller=>"api/v1/users", :format=>"json"} missing required keys: [:id] i added @user param api_v1_user_url on hunch after saw error , works.
my question going on here? not understand why need pass @user when of examples i've seen not involve passing object @user :location attribute.
the output of rake routes follows:
$ rake routes ignoring bcrypt-3.1.7 because extensions not built. try: gem pristine bcrypt-3.1.7 prefix verb uri pattern controller#action api_v1_users /api/v1/users(.:format) api/v1/users#index {:format=>"json"} post /api/v1/users(.:format) api/v1/users#create {:format=>"json"} new_api_v1_user /api/v1/users/new(.:format) api/v1/users#new {:format=>"json"} edit_api_v1_user /api/v1/users/:id/edit(.:format) api/v1/users#edit {:format=>"json"} api_v1_user /api/v1/users/:id(.:format) api/v1/users#show {:format=>"json"} patch /api/v1/users/:id(.:format) api/v1/users#update {:format=>"json"} put /api/v1/users/:id(.:format) api/v1/users#update {:format=>"json"} delete /api/v1/users/:id(.:format) api/v1/users#destroy {:format=>"json"} static_index /static/index(.:format) static#index root / static#index another place i've seen (just name one):
the reason being api_v1_user_url or api_v1_user_path single instance , need pass id of user should , render. hence why got error go.
in examples saw api_v1_users_url or api_v1_users_path renders users. notice users_ vs user_
Comments
Post a Comment