This project is archived and is in readonly mode.
Unexpected behavior of render :json => obj
-
johnnypez
Hi,
sorry I left that out, long day.The object is completely basic subclass of Exception
class ApiException < Exception def to_json return { :error => self.class.name, :message => self.to_str }.to_json end endWhenever the exception is raised, it is caught with this around_filter in my app controller:
#application_controller.rb respond_to :json around_filter :catch_api_errors def catch_api_errors begin yield rescue ApiException => e respond_with e, :status => 400 end endI expect the output of this to be the result of
:to_jsonbeing sent to the ApiException instance but instead I just the the value of:to_str -
Neeraj Singh
- State changed from new to open
- Milestone set to 3.x
- Assigned user set to José Valim
- Tag changed from render json render_with to render json render_with, patched
Attached is patch with test.
Discussed this issue with Mr. Valim. Assigning it to him.
-
Repository
- State changed from open to resolved
(from [fa2a5ae0339c90d023a7559e681a588219dc3903]) If a user wants json output then try best to render json output. In such cases prefer kind_of(String) over respond_to?(to_str)
[#5841 Unexpected behavior of render :json => obj state:resolved]
Signed-off-by: José Valim jose.valim@gmail.com
https://github.com/rails/rails/commit/fa2a5ae0339c90d023a7559e681a5... -
Repository
(from [4e2a981bdd162dfd52b15f34b4e45ba87726ff53]) If a user wants json output then try best to render json output. In such cases prefer kind_of(String) over respond_to?(to_str)
[#5841 Unexpected behavior of render :json => obj state:resolved]
Signed-off-by: José Valim jose.valim@gmail.com
https://github.com/rails/rails/commit/4e2a981bdd162dfd52b15f34b4e45...
