2013-09-10 22 views
0

我在入門級Rails。我用腳手架製作了數據庫(名稱爲「商店」)。我還做了顯示錶格概覽的頁面,並跳轉到顯示記錄特定信息的其他頁面。 現在,當點擊表格的一行上的「顯示」鏈接時,我嘗試使記錄的特定信息顯示在表格概覽的同一頁面中的頁面。Ajax in Rails,它不起作用,因爲它跳轉到其他頁面而不停留在當前頁面中。

但是,儘管我的期望,它跳轉到其他頁面,因爲我已經做了。 我用了好幾個小時,所以想找人幫忙。

謝謝。

\應用\控制器\ shops_controller.rb

 class ShopsController < ApplicationController 

     # GET /shops 
     # GET /shops.json 
     def index 
      @shops = Shop.all 

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

    # GET /shops/1 
    # GET /shops/1.json 
    def show 
     @shop = Shop.find(params[:id]) 

     respond_to do |format| 
      format.html { render layout: (request.headers["X-Requested-With"] != 'XMLHttpRequest') } 
      format.json { render json: @shop } 
     end 
    end 

    # GET /shops/new 
    # GET /shops/new.json 
    def new 
     @shop = Shop.new 

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

    # GET /shops/1/edit 
    def edit 
     @shop = Shop.find(params[:id]) 
    end 

    # POST /shops 
    # POST /shops.json 
    def create 
     @shop = Shop.new(params[:shop]) 

     respond_to do |format| 
     if @shop.save 
      format.html { redirect_to @shop, notice: 'Shop was successfully created.' } 
      format.json { render json: @shop, status: :created, location: @shop } 
     else 
      format.html { render action: "new" } 
      format.json { render json: @shop.errors, status: :unprocessable_entity } 
     end 
     end 
    end 

    # PUT /shops/1 
    # PUT /shops/1.json 
    def update 
     @shop = Shop.find(params[:id]) 

     respond_to do |format| 
      if @shop.update_attributes(params[:shop]) 
      format.html { redirect_to @shop, notice: 'Shop was successfully updated.' } 
      format.json { head :no_content } 
      else 
      format.html { render action: "edit" } 
      format.json { render json: @shop.errors, status: :unprocessable_entity } 
      end 
     end 
    end 

    # DELETE /shops/1 
    # DELETE /shops/1.json 
    def destroy 
     @shop = Shop.find(params[:id]) 
     @shop.destroy 

     respond_to do |format| 
     format.html { redirect_to shops_url } 
     format.json { head :no_content } 
     end 
    end 
end 

\應用\視圖\商店\ index.html.erb

<h2>Shops</h2> 

    <table> 
    <tr> 
     <th>Name</th> 
     <th>Link_ID</th> 
     <th>Offer</th> 
     <th></th> 
     <th></th> 
     <th></th> 
    </tr> 

    <% @shops.each do |shop| %> 
    <tr> 
     <td><%= shop.name %></td> 
     <td><%= shop.link_id %></td> 
     <td><%= shop.offer %></td> 
     <td><%= link_to 'Show', shop, :remote => true, "data-type" => "html", :class => 'show' %></td> 
     <td><%= link_to 'Edit', edit_shop_path(shop) %></td> 
     <td><%= link_to 'Destroy', shop, method: :delete, data: { confirm: 'Are you sure?' } %></td> 
    </tr> 
    <% end %> 
    </table> 

    <br /> 

    <div id="show_area"></div> 
     <%= javascript_tag do %> 
      $('a.show').on('ajax:success', function (data, status, xhr) { 
      $('#show_area').html(status);}) 
     <% end %> 

\應用\視圖\商店\ show.js.erb

$('#show_area').html("<%= raw(j(render :partial => 'show_body')) %>") 
+0

如果你使它'data:{type:「script」}'而不是你的「數據類型」等等,它會改變什麼? –

回答

0

如果你在你的Gemfile jQuery的軌道(寶石 'jQuery的軌道'),並具有:

//= require jquery 
//= require jquery_ujs 

在你的application.js中,你有什麼應該工作。減少show.js.erb,這將需要您將數據類型更改爲腳本而不是html。雖然根據您的描述,但不需要show.js.erb。你在index.html.erb中的腳本就足夠了。

相關問題