Ruby array to JSON and Rails JSON rendering
def autocomplete
@question = Question.all
respond_to do |format|
format.json { render :json => @question }
end
end
Efficient way to convert collection to array of hashes for render json
- Don't do it in the contoller
- Don't generate the json response with
map
, let'sas_json(*)
method do that for you. - Don't use
@
variable in the json render. - Don't use
{}.to_json
therender json: {}
do it under the hood.
in the photo model.
def as_json(*)
super(only: [:id], methods: [:label, :url, :size])
end
alias label name
def size
uploaded_image_file_size
end
def url
uploaded_image.url(:small)
end
controller code.
def list
render json: { results: current_user.photos }
end
How can I add custom value to json array in response in ruby?
You could try something like this:
render json: {"message": "somevalue", "response": response}
Simple as that, should do the trick.
By the way, you can also call the "response" field whatever you want. It's gonna be the key of the object you get in JSON later.
How Render json ruby on rails with array result
def render
begin
array= [:orange => 1,:limon => 3]
render :json => {:array => array},:callback => params[:callback]
rescue Exception => e
puts e
end
end
render custom json array in rails
This is a pretty naive way to render it, but this should get you started:
@hours = Hour.all
@users = User.where(id: @hours.map(&:user_id).uniq)
@hour_types = HourType.where(id: @hours.map(&:hour_type_id).uniq)
result = {
users: @users.map{|u| {user_id: u.id} },
hours: []
}
@hour_types.each do |hour_type|
hour_type_hash = {
hour_type_id: hour_type.id,
hours: Hour.where(hour_type_id: hour_type.id).all.map{|h| {hour_id: h.id} }
}
result[:hours] << hour_type_hash
end
render json: result_hash
When rendering an object in JSON from the controller, how do I include a field from the object's parent?
Try this one
def show
@my_object = MyObject.find(params[:id])
render :json => @my_object.to_json(:include => [:parent => {:include => :address}])
end
You have added include within the include statement,
@my_object.to_json(:include => [:parent, **:include** => :address])
so rails is searching include as one of the method or model.
You can use a array like this for including more than one relationship.
:include => [:parent, :address]
Render the full Json array in response.body
Try this:
render json: @emergency.to_json(:root => "emergency"), :status => 201
Rails render index (JSON) after update returning empty array
You need to define @events
in update action to render it as json
def update
respond_to do |format|
if @event.update(event_params)
if ...
....
else
@events = Event.all
format.json { render :index, status: :ok }
end
else
....
end
end
end
Related Topics
How to Use Rspec to Mock Stdin/Stdout to Test Console Reads & Writes
Is There a Cucumber Hook to Run Before and After Each Feature
Error: Failed to Build Gem Native Extension (Ruby Extconf.Rb): MAC Osx
Why Does Ruby String#Split Not Treat Consecutive Trailing Delimiters as Separate Entities
Insecure World Writable Dir /Users/Username in Path, Mode 040777 When Running Ruby Commands
Array of Indexes to Array of Ranges
How to Validate Xhtml with Nokogiri
Where Should My Non-Model/Non-Controller Code Live
When Did "Assigned But Unused" Become a Warning for Ruby