4412

Simple custom jbuilder json response

Question:

I'd like to be able to create some super simple json string with jbuilder since it's included in rails 4 by default.

Say I got some Categories current_project.categories.

At the moment I'm using this jbuilder construct to get only the category titles:

json.array! current_project.categories do |c| json.title c.title end

This genervtes the following response:

[{"title":"Allgemeine Fragen"},{"title":"Dauerkarten"},{"title":"Heimspiele"},{"title":"Online-Ticketing"},{"title":"Rollstuhlfahrer"},{"title":"Auswärtsspiele"},{"title":"Busfahrten"},{"title":"Meine Kategorie"},{"title":"Meine neue Kat"}]

It would be nice if I could keep that json response way more simple, e.g.:

["Allgemeine Fragen", "Dauerkarten", "Heimspiele", "Online-Ticketing", "Rollstuhlfahrer", "Auswärtsspiele", "Busfahrten", "Meine Kategorie", "Meine neue Kat"]

How can I accomplis this using jbuilder?

Thanks!

Answer1:

You can use

json.array! current_project.categories.pluck(:title)

or the extended version

json.array! current_project.categories.map(&:title)

The first is more efficient.

Recommend

  • How do I work with DriveApi.MetadataBufferResult?
  • how to load a file from folder to memory stream buffer
  • How to use a save file dialog from a servlet?
  • Mediawiki API error: Unrecognized value for parameter 'prop': extracts
  • What does the “?” mean in the following statement
  • Cannot convert a char value to money. The char value has incorrect syntax
  • Is there a chance to get -splash: work for SWT applications that require -XstartOnFirstThread?
  • Scrolling News Ticker Jquery - Issues
  • Change navbar in bootstrap if user login
  • Using extern @class in order to add a category?
  • “A GKScore must specify a leaderboard.”
  • Should I be afraid to use UDP to make a client/server broadcast talk?
  • include dlls in visual studio c++ 2008
  • Ionic storage “get” returns null only on the second call within method
  • Google OAuth: can't get refresh token with authorization code
  • What command do i need to pass in SabreCommandLLSRQ to get current price of PNR?
  • In Java, how can I construct a File from a resource?
  • Django foreign key drop down
  • Primefaces ManyCheckbox inside ui:repeat calls setter method only for last loop
  • R Split data.frame using a column that represents and on/off switch
  • Can't delete or rename original file after resizing
  • Execute scripts AJAX returns
  • MS Access - How to change the linked table path by amend the table
  • Seeking advice on Jetty HttpClient Hang
  • Using variable in a value field in jMeter
  • Why is an OPTIONS request sent to the server?
  • C# - Is there a limit to the size of an httpWebRequest stream?
  • DotNetZip - Calculate final zip size before calling Save(stream)
  • How to add date and time under each post in guestbook in google app engine
  • Rearranging Cells in UITableView Bug & Saving Changes
  • How to pass list parameters for each object using Spring MVC?
  • Is there a mandatory requirement to switch app.yaml?
  • File upload with ng-file-upload throwing error
  • ExecuteAsync RestSharp to allow backgroundWorker CancellationPending c#
  • KeystoneJS: Relationships in Admin UI not updating
  • AngularJs get employee from factory
  • trying to dynamically update Highchart column chart but series undefined
  • Benchmarking RAM performance - UWP and C#
  • Angular 2 constructor injection vs direct access
  • IndexOutOfRangeException on multidimensional array despite using GetLength check