Response_with format redirect = json

I encounter strange behavior in my controllers. It seems like they sometimes want to redirect rather than give a json response.

respond_to :json, :html, :js def create @favorite = current_user.favorites.build(:location_id=>params[:location_id]) if @favorite.save respond_with(@favorite) else respond_with(@favorite.errors) end end 

I think it works most of the time, but today I received a notification about this error:

NoMethodError: undefined method `` favorite_url '' for # <FavoritesController: 0x00000006171dc0>

The params hash was registered as:

 {"format"=>"json", "action"=>"create", "user_id"=>"56", "auth_token"=>"iGSty8CMIaWsbShYZEtw", "location_id"=>"47943", "controller"=>"favorites"} 

Particularly strange since it works most of the time ... I changed some of my other controllers to use the old format.json syntax {render: json => @object}, but I would like to avoid it if possible.

How could this be?

+4
source share
1 answer

In ways that are not GETs, respond_with tries to redirect the URL to what is given to it. You can override this with a custom responder

+3
source

Source: https://habr.com/ru/post/1346389/


All Articles