2

I have an Ruby on rails source,code now i want to parse the data, and send the data.In my code,it will fetches the name from user and display it,How to parse the data in ROR.

This is my controller.rb code

def index
    @hotels = Hotel.all

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @hotels }
    end
  end

  # GET /hotels/1
  # GET /hotels/1.json
  def show
    @hotel = Hotel.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @hotel }
    end
  end

  # GET /hotels/new
  # GET /hotels/new.json
  def new
    @hotel = Hotel.new

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @hotel }
    end
  end

  # GET /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 do |format|
      if @hotel.save
        format.html { redirect_to @hotel, notice: 'Hotel was successfully 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 do |format|
      if @hotel.update_attributes(params[:hotel])
        format.html { redirect_to @hotel, notice: 'Hotel was successfully 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 do |format|
      format.html { redirect_to hotels_url }
      format.json { head :no_content }
    end
  end

How to parse these data in ROR using Json HOw to write parsing json file for these data,how to do that one

3 Answers 3

2
    **Answer of my question**

This part we have to do in controller hotelscontroller.erb

  respond_to :json, :xml
      def index
        @hotels = Hotel.all

        respond_to do |format|
          format.html # show.html.erb
          format.json { render json: @hotels.to_json(:only => [ :id, :name ]) }
        end
      end

view/index.json.erb

[
  <% @hotels.each do |hotel| %>
    { 'id': <%= hotel.id %>, 'name': "<%= hotel.name %>" },
  <% end %>
]

routes.rb

get 'hotels' => 'hotels#index', :as => 'hotels'
Sign up to request clarification or add additional context in comments.

Comments

1

There are many ways:

  1. Create method to_json for each model
  2. Create view named, i.e. hotels/index.json.erb and write JSON code using ERb templating engine

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

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

4 Comments

could you provide answer for my solution,i am new to one,
How to write json file for the above controller,could you give the solution
Dude this answer is enough or not?
1

You can covert a hash to json by to_json method. For reference please see http://apidock.com/rails/ActiveRecord/Serialization/to_json

for example, if you want to write all hotels to external file.

file = File.open("hotels.txt", "w")
file.puts Hotel.all.to_json
file.close

this will write all hotels in external file hotels.txt

8 Comments

Dude could you give your answer,how to write the json file for my controller.I am new to json,give me answer for my question
I want to get the data from the user,that what in my code,its a scaffolding.how to parse the data,which user providing
could you provide solution for me @Ramiz Raja
Do you want to write json version of user object to an external file?
Do you want to write json version of user object to an external file?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.