How to render json some variables without brackets inside brackets on AMS - ruby-on-rails

I'd like to know if is possible to render variable without params on render with brackets with AMS?
for exemple:
render json: { comments: #comments, status: 200, hasMore: hasMore }
i'd like to render just #comments without the comments: and with hasMore: hasMore showing on the json that is just possible inside brackets!
with this way
render json: #comments, hasMore: hasMore, include: ['user', 'replies.**']
did not show the hasMore on the json

If you're rendering json, you need to make sure what you pass as json is actually valid json. Here's a most simple example:
# this does not work and will throw a syntax error
render json: 'foo: 'foo', bar: 'bar'
but
# this works
render json: {foo: 'foo', bar: 'bar'}
# this works because it will render an array type
render json: #comments # assuming that is a collection

Related

Ruby Active Model serializer custom serializer for same model

lets say I have a method as follows
def index
applications = Application.all
render json: applications, each_serializer: ApplicationIndexSerializer, status: 200
end
now this works and it returns me my custom serializer, but if I want to add custom keys to the payload like {data: application} it stops working.
however when I change the render method to
render json: {meta: {}, data: applications}, each_serializer: ApplicationIndexSerializer, status: 200
it throughs an error or the serializer does not get hit.
You probably want something like this:
render json: applications, each_serializer: ApplicationIndexSerializer, meta: {}, status: 200

Rails: Find or initialize and then merge in params

I use find_or_initialize_by in my controller and then would like to apply the additional params to the record, whether new or existing. Is this possible to do in a Railsy way or do I have to loop through the parameters?
def create
re_evaluation = #client.re_evaluations.find_or_initialize_by(program_id: re_evaluation_params[:program_id], check_in_id: re_evaluation_params[:check_in_id])
# apply remaining re_evaluation_params here
if re_evaluation.save
render json: re_evaluation, status: :created
else
render json: re_evaluation.errors.full_messages, status: :unprocessable_entity
end
end
You should be able to assign the parameters as you'd do normally.
re_evaluation.assign_attributes(re_evaluation_params)
if re_evaluation.save
...

Render in action is shown on the screen - Ruby on rails

I am new programming in ruby ​​on rails and I need help with a problem of a render in an action of my controller
This is my action:
def events_calendar
render json: {events: all_events}, status: :ok
end
This is what it shows me:
I want you to show me my web page and not the data of my render, how could I solve this?
Take a look at respond_to.
def events_calendar
respond.to do |f|
f.html
f.js { render json: {events: all_events}, status: :ok }
end
end
That way, you get the JSON when you call events_calendar using AJAX, and still render events_calendar.html.erb when you use a plain HTTP resquest.

dynamic rendering based on data results

I am curious if this is even possible. I am looking to render data based on the results of an action in ruby on rails. Here is the code:
def convert
render :text => Item.convert(params[:file], params[:output])
end
As you can see currently it just renders text. The item model takes in two parameters: a file and output. The output could be xml, json, plain text, etc. Some type of data. What I was hoping I could do is that I could render the output based on the output parameter. But make it dynamic. I was hoping I could do:
def convert
#items = Item.convert(params[:file], params[:output])
respond_to do |format|
format.json { render :json => #items }
format.xml { render :xml => #items }
end
end
Something that would call the method and based on the data returned it would render the appropriate data. Testing the above code returns an empty page. The body tag is completely empty.
Any ideas?
thanks
Mike Riley
If you are relying on the name of params[:output] to determine what to render, I would write is as below:
def convert
#items = Item.convert(params[:file], params[:output])
if File.extname(params[:output].match("text"))
render :text => Item.convert(params[:file], params[:output])
elsif File.extname(params[:output].match("json"))
render :json => Item.convert(params[:file], params[:output])
elsif File.extname(params[:output].match("xml"))
render :xml => Item.convert(params[:file], params[:output])
else
render :html => "default_html"
end
end
and you should have a default_html.html.erb that you would render if the extension name of the file does not match what is expected

Rails render template with status

What is the simplest/shortest way to respond in an API controller. Currently the following works:
respond_to do |format|
format.json { render 'client', status: :ok }
end
however this controller will only ever respond to json (respond_to :json) so the whole respond_to do |format| thing seems like unnecessary code.
Ideally I would just like to do something simple like:
render 'client', status: :ok
Update:
I neglected to mention that: 'client' is a jbuilder template that does not match my action name.
You can use render directly
render json: 'client', status: :ok
According to #hassasin, you can indicate your controller to render json: format on each action of your controller.
Other option is to take advantage of your config.routes.rb to set the entire response format of your controller, e.g. contacts_controller:
resources :contacts, defaults: {format: :json}
If you want to indicate the status, add this to your actions:
def index
render status: :ok # 200, 400, 500, what ever you want
end
I tested the code above with Rails 3.2.16
I hope it helps you.
Since you are using json views (assume you are using jbuilder), you dont need that render statement if your action name matches the view name.

Resources