How to parsing the json data or (json code) from Ruby on rails -


i have ruby on rails source,code want parse data, , send data.in code,it fetches name user , display it,how parse data in ror.

this controller.rb code

def index     @hotels = hotel.all      respond_to |format|       format.html # index.html.erb       format.json { render json: @hotels }     end   end    # /hotels/1   # /hotels/1.json   def show     @hotel = hotel.find(params[:id])      respond_to |format|       format.html # show.html.erb       format.json { render json: @hotel }     end   end    # /hotels/new   # /hotels/new.json   def new     @hotel = hotel.new      respond_to |format|       format.html # new.html.erb       format.json { render json: @hotel }     end   end    # /hotels/1/edit   def edit     @hotel = hotel.find(params[:id])   end    # post /hotels   # post /hotels.json   def create     @hotel = hotel.new(params[:hotel])      respond_to |format|       if @hotel.save         format.html { redirect_to @hotel, notice: 'hotel created.' }         format.json { render json: @hotel, status: :created, location: @hotel }       else         format.html { render action: "new" }         format.json { render json: @hotel.errors, status: :unprocessable_entity }       end     end   end    # put /hotels/1   # put /hotels/1.json   def update     @hotel = hotel.find(params[:id])      respond_to |format|       if @hotel.update_attributes(params[:hotel])         format.html { redirect_to @hotel, notice: 'hotel updated.' }         format.json { head :no_content }       else         format.html { render action: "edit" }         format.json { render json: @hotel.errors, status: :unprocessable_entity }       end     end   end    # delete /hotels/1   # delete /hotels/1.json   def destroy     @hotel = hotel.find(params[:id])     @hotel.destroy      respond_to |format|       format.html { redirect_to hotels_url }       format.json { head :no_content }     end   end 

how parse these data in ror using json how write parsing json file these data,how one

there many ways:

  1. create method to_json each model
  2. create view named, i.e. hotels/index.json.erb , write json code using erb templating engine

    [   <% @hotels.each |hotel| %>     { 'id': <%= hotel.id %>, 'name': "<%= hotel.name %>" },   <% end %> ] 
  3. use library jbuilder (on bottom of page list of alternatives jbuilder)

    # hotels/index.json.jbuilder json.array!(@hotels) |hotel|   json.id hotel.id   json.name hotel.name end 

Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -