【问题标题】:How to Render partial on button click Rails如何在按钮单击 Rails 时渲染部分内容
【发布时间】:2022-08-16 10:28:52
【问题描述】:

我想在 Rails 中单击按钮时渲染部分内容。 我有两个控制器

  1. 字段控制器
  2. 操作员控制器

    当用户单击 index.html.erb 上的显示时,它将显示一个弹出窗口,其中包含 Operator Controller 的 index.html.erb 的内容。

    这是我的 Fields Controller 的 index.html.erb 按钮:

    <%= link_to \"Link\" ,operators_index_path(:id => field_id, :field_name=>field_name), :remote => true %>
    

    这是 Operators Controller 的索引方法:

    class OperatorsController < ApplicationController
      def index
        @operators = Api::AnalyticsQueryBuilderMetadataService::Operator.show(params[:id])
      end
    end
    

    这是我想显示为弹出窗口的 Operator Controller 的 index.html.erb 的内容:

    <%if @operators[\'items\'].empty?%>
      <div class=\"error-handling-page\">
      <div class=\"dialog\">
        <div>
          <h1>No Operator Found</h1>
        </div>
      </div>
      </div>
    <%else%>
    <table class =\"table\">
    <thead>
        <th class =\"head\">Field Name</th>
        <th class =\"head\">Name</th>
        <th class =\"head\">Description</th>
    </thead>
        <% @operators[\'items\'].each do |data| %>
            
            <% @opId =  data[\'operators\'] %>
            <% @opId.each do |value| %>
            <tr>
            <td><%= field_name %></td>
            <td><%= value[\'mnemonic\']%></td>
            <td><%= value[\'description\']%></td>
            </tr>
            <%end%>
        <%end%>
    </tbody>
    </table>
    <%end%>
    

    怎么做?

    标签: jquery ruby-on-rails ajax


    【解决方案1】:

    您可以使用 AJAX 和 UJS 做到这一点。

    这是my favorite tutorial

    如果你想成为最前沿的,Rails 7 有 Hotwire,它也可以做到这一点。有许多可通过快速 Google 搜索获得 Hotwire 教程。

    【讨论】:

      猜你喜欢
      • 2016-06-27
      • 1970-01-01
      • 1970-01-01
      • 2014-10-18
      • 1970-01-01
      • 1970-01-01
      • 2022-11-14
      • 2015-04-15
      • 1970-01-01
      相关资源
      最近更新 更多