Ruby on rails JSON呈现:如何包含开头括号描述?

Ruby on rails JSON呈现:如何包含开头括号描述?,ruby-on-rails,Ruby On Rails,我有一些Ruby代码输出JSON,如下所示: def nearby #@bathrooms = Bathroom.geo_scope(:origin =>[params[:lat],[params[:lon]]], :within => 5) lat = params[:lat] lon = params[:lon] #@bathrooms = Bathroom.geo_scope(:within => 5, :origin => [45.580639,-

我有一些Ruby代码输出JSON,如下所示:

def nearby
  #@bathrooms = Bathroom.geo_scope(:origin =>[params[:lat],[params[:lon]]], :within => 5)
  lat = params[:lat]
  lon = params[:lon]
  #@bathrooms = Bathroom.geo_scope(:within => 5, :origin => [45.580639,-122.677682], :order=>'distance')
  @bathrooms = Bathroom.geo_scope(:within => 3, :origin => [lat,lon], :order=>'distance')
  respond_to do |format|
    format.json  { render :json => @bathrooms }
    format.js   { render :nothing => true } 
  end   
end
 [{"ID":129,"access":"1","avail":"0","bathroomtype":"0","city":"PORTLAND","comment":"","country":"US","created":"0000-00-00 00:00:00","directions":"The two bathrooms are in the long hallway at the back of the restaurant, past the bar.","distance":"2.94986114636676","lat":"45.539217","lon":"-122.661795","modifed":"0000-00-00","name":"Echo","postal":"97212-3727","slug":"echo-portland170","source":"","state":"OR","street":"2225 NE Martin Luther King Jr. Blvd"},
结果是这样的:

def nearby
  #@bathrooms = Bathroom.geo_scope(:origin =>[params[:lat],[params[:lon]]], :within => 5)
  lat = params[:lat]
  lon = params[:lon]
  #@bathrooms = Bathroom.geo_scope(:within => 5, :origin => [45.580639,-122.677682], :order=>'distance')
  @bathrooms = Bathroom.geo_scope(:within => 3, :origin => [lat,lon], :order=>'distance')
  respond_to do |format|
    format.json  { render :json => @bathrooms }
    format.js   { render :nothing => true } 
  end   
end
 [{"ID":129,"access":"1","avail":"0","bathroomtype":"0","city":"PORTLAND","comment":"","country":"US","created":"0000-00-00 00:00:00","directions":"The two bathrooms are in the long hallway at the back of the restaurant, past the bar.","distance":"2.94986114636676","lat":"45.539217","lon":"-122.661795","modifed":"0000-00-00","name":"Echo","postal":"97212-3727","slug":"echo-portland170","source":"","state":"OR","street":"2225 NE Martin Luther King Jr. Blvd"},
我不知道该怎么做的是给这一组命名?例如,在第一个括号之后,我想命名这套浴室。如何在Rails中执行此操作?

请尝试:

render json: { bathrooms: @bathrooms }
尝试:


看起来@bathrooms是一个列表,所以不能为列表中的元素指定键。你需要先做张地图。例如,如果只需要第一个元素:

format.json { render :json => {:bathrooms => @bathrooms[0]} }
要设置所有列表元素,请执行以下操作:

format.json { render :json => {:bathrooms => @bathrooms} }

看起来@bathrooms是一个列表,所以不能为列表中的元素指定键。你需要先做张地图。例如,如果只需要第一个元素:

format.json { render :json => {:bathrooms => @bathrooms[0]} }
要设置所有列表元素,请执行以下操作:

format.json { render :json => {:bathrooms => @bathrooms} }

不行:我尝试了:呈现json:{bathrooms:@bathrooms},它看起来不像有效的ruby语法。请看我的答案,这是一个完美的Ruby 1.9.2语法。。。如果你更喜欢Ruby 1.9.2,请使用好的hashrockets,直到Ruby 1.9.2变得更像Javascript:)是的,Ruby这样更轻,我喜欢它,除了当它涉及到
foo::bar
时,它看起来很奇怪,但没有做到这一点:我尝试过:呈现json:{bathrooms:@bathrooms}它看起来不像有效的Ruby语法。请看我的答案,这是一个完美的Ruby 1.9.2语法。。。如果你更喜欢Ruby 1.9.2,请使用旧的hashrockets,直到Ruby 1.9.2变得更像Javascript:)没错,Ruby这样更轻,我喜欢它,除了当它涉及到
foo::bar
时,它看起来很奇怪,看起来更好,因为括号现在把它命名为浴室。但是,如何指定列表中的所有元素?这不仅仅是第一个记录?另一个老派科技的胜利:)这看起来更好,因为括号现在命名为浴室。但是,如何指定列表中的所有元素?这不仅仅是第一个记录?另一个老派科技的胜利:)