0

我处于Rails的入门级。我用脚手架制作了数据库(名称为“shop”)。我还制作了显示表格概述的页面并跳转到显示记录特定信息的其他页面。现在我尝试使记录的具体信息显示在表格概述的同一页面中,当单击表格的一行上的“显示”链接时。

但是,尽管出乎我的意料,它还是像我已经完成的那样跳转到了其他页面。我花了很多时间来解决这个问题,所以想得到别人的帮助。

谢谢你。

\app\controllers\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

\app\views\shops\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 %>

\app\views\shops\show.js.erb

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

1 回答 1

0

如果你的 gemfile (gem 'jquery-rails') 中有 jquery rails 并且有:

//= require jquery
//= require jquery_ujs

在您的 application.js 中,您所拥有的应该可以工作。减去 show.js.erb,这需要您将数据类型更改为脚本而不是 html。尽管根据您的描述,不需要 show.js.erb 。您在 index.html.erb 中的脚本就足够了。

于 2013-09-11T02:01:11.890 回答