ruby - How to modify to_json method and add a dynamic property to it - Rails 4 -
so have controller , want add dynamic attribute along other data in @events instance variable
i have search , tried things @events.attributes.merge(appointment: true)
appointment = true want add events object.
def find params = event_params current_user = 2 @events = event.where('date ?',"%#{params[:month]}%") def @events.as_json(options = { }) h = super(options) h[:appointments] = false # or combine above h[:appointments] = self.appointments? h end respond_to |format| if current_user == 1 if @events format.json { render json: @events.to_json } else render 'index' end else format.json { render json: @events.to_json } end end
end
ajax code here
function retrieve(date_partial) { var jsondata = { events: { month: date_partial, } } $.ajax({ cache: false, type: "post", url: "/events/find", data: jsondata, success: function(data) { console.log(data); (var = 0; < data.length; i++) { var day = data[i].date.substring(0, 2); $("td[data-day='" + day + "']").addclass('added'); } }, error: function(xhr) { alert("the error code is: " + xhr.statustext); } });
so how can add property?
this work ? maybe json output isn't expected ?
format.json { render :json => {events: @events, appointments: true} }
Comments
Post a Comment