ruby on rails - How to get the whole array of name in one array object using json? -


in code,i parsing json object [{"name":"karthi"},{"name":"shreshtt"},{"name":"jitu"},{"name":null},{"name":null},{"name":null},{"name":null}]

in this, want collect names in single array object. how controller looks of now. want store resultant name array in @hotels variable.

controller.erb

respond_to :json, :xml   def index     @hotels = hotel.all      respond_to |format|       format.html # show.html.erb       format.json { render json: @hotels.to_json(:only => [ :name ]) }     end   end 

view/hoels/index.json.erb

[  hotel: <% @hotels.each |hotel| %>     { 'name': "<%= hotel.name.to_json.html_safe %>" }      <% unless index== @hotels.count - 1%>   <% end %>    <% end %> ] 

you want add names array? how about:

a = [{name: "karthi"},{name: "shreshtt"},{name: "jitu"},{name: nil},{name: nil},{name: nil},{name: nil}] @hotel = [] a.collect{|a_name| @hotel << a_name[:name]} => ["karthi", "shreshtt", "jitu", nil, nil, nil, nil] @hotel.compact! => ["karthi", "shreshtt", "jitu"] 

Comments

Popular posts from this blog

c# - Operator '==' incompatible with operand types 'Guid' and 'Guid' using DynamicExpression.ParseLambda<T, bool> -