0

I'm using autocomplete widget of jquery-ui. I found that it sends its parameters as term and it needs a json like this

[{"label": "Client1"},{"label": "Client2"} ]

Instead I am getting

[{"client":{"label":"Cliente"}}]

clients_controller.rb

def index
  @client = Client.all
  @client = Client.paginate(:page => params[:page])
  if params[:term].present?
    @client = @client.where("client_name LIKE ? ", "%#{params[:term]}%")
  else
    @client = @client.paginate(page: params[:page])
  end

  respond_to do |format|
    format.html
    format.json
  end
end

_client.json.jbuilder

json.client do
  json.label client.client_name
end

How can I format a json as I needing it?

Cloud1988
  • 199
  • 2
  • 11

1 Answers1

0

use json.(@client, :client_name) instead of looping.

Ashutosh Tiwari
  • 984
  • 6
  • 16
  • I'm getting a syntax error. I also tried `json.label(@clients, :client_name)` but it doesn't work either. Could you give me any other suggestions please? =) – Cloud1988 Apr 03 '17 at 05:01
  • I'm trying to render a json from a like query like [this](http://stackoverflow.com/questions/43040615/rails-5-best-controller-action-to-write-a-like-query) one. But I also want to give client_name an label allias to return a json like this. `[{"label": "Client1"},{"label": "Client2"} ]` in wich label is actually **client_name** column. Sorry if I'm not making myself clear. Feel free to ask any questions :) – Cloud1988 Apr 03 '17 at 05:15