ruby on rails - Overriding as_json method with params -
First of all, I am using Rails 3.0.6 and Ruby 1.9.2
My There is a controller with two different actions, both of which should return a Jason object, but with different formats. So I'm overriding to write as_json method in my own format to write a JSON object. . The problem is that I do not know how to pass the parameters for the as_json method after it is being called automatically by the rail
My code looks like this:
Class MyController & LT; ApplicationController def action1 # My code response_to do | Format | Submission with #: json option automatically calls to_json and this call as_json format.js {rendere: json = & gt; @MyObjectEt} End and DEF Action 2 # Answer a different code- do. Format | # This action should return a JSON object but using a different format format.js {render: json = & gt; @maybajejct} and end and class MyModel & lt; ActiveRecord :: Base def as_json (options = {}) # I would like to add a conditional statement here # to write a different array based on an absolute basis from a controller: {id = & gt; Self.id,: title = & gt; Self.description ,: Description = & gt; Self-description || || "",: Start = & gt; Start_date1.rfc822,: end = & gt; (Start_date1 and start_date1.rfc822) || "",: AllDay = & gt; True, recurring = & gt; False} End End Note that @ MyObjects is a collection of objects that are class MyModel.
Any help would be appreciated. Thanks!
Call it explicitly in the controller and pass the parameter as_json string Will return and return itself to the as_json on the string. This is very common. Do not Answer Format | Submission with #: json option automatically calls to_json and this call as_json format.js {rendere: json = & gt; @ Myobjects.as_json (params)} and
Comments
Post a Comment