I have a collection of @clients with attributes id and email I want to render this json format
[
{"id":" 1","label":"[email protected]","value":"1"},{"id":" 2","label":"[email protected]","value":"2"}
]
in clients_controller I defined the following method
def search
@clients = Client.where(:user_id => current_user.id).select('id','email')
render :partial => "clients/search"
end
and here is the view _search.json.erb
[
<%= raw @client.map{|client| '{"id":"' +" #{client.id}" +'","label":"' + "#{client.email}" + '","value":"' +"#{client.id}" +'"}' }.join(",") %>
]
this is working, but I found it fugly...is there a more elegant way to generate a custom json format in a view?