2

Ruby on Rails ソースがあり、データを解析してデータを送信したいコードがあります。コードでは、ユーザーから名前を取得して表示します。ROR でデータを解析する方法。

これは私のcontroller.rbコードです

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

Json を使用して ROR でこれらのデータを解析する方法 これらのデータの 解析 json ファイルを作成する方法、その方法

4

3 に答える 3

2
    **Answer of my question**

この部分は、コントローラー 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

ビュー/index.json.erb

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

ルート.rb

get 'hotels' => 'hotels#index', :as => 'hotels'
于 2013-05-10T07:12:46.800 に答える
1

多くの方法があります:

  1. to_json各モデルの作成方法
  2. 名前付きのビューを作成します。つまりhotels/index.json.erb、ERb テンプレート エンジンを使用して JSON コードを記述します。

    [
      <% @hotels.each do |hotel| %>
        { 'id': <%= hotel.id %>, 'name': "<%= hotel.name %>" },
      <% end %>
    ]
    
  3. 次のようなライブラリを使用しますjbuilder(ページの下部にJBuilderの代替リストがあります)

    # hotels/index.json.jbuilder
    json.array!(@hotels) do |hotel|
      json.id hotel.id
      json.name hotel.name
    end
    
于 2013-05-09T12:24:40.697 に答える