简体   繁体   English

如何在 Ruby on Rails 5.2 中将来自不同 controller 的视图渲染为模态 window?

[英]How to render a view from a different controller as modal window in Ruby on Rails 5.2?

My application implements a Sumit/Approve/Reject workflow upon Skills.我的应用程序根据技能实施提交/批准/拒绝工作流程。 When a Skill is rejected, it creates a notification of which the reason should be input by the user through a modal window. Unfortunately, I am not comfortable with front-end development, and the modal window does not show up: it is not generated in the html output of the page.当一个技能被拒绝时,它会创建一个通知,用户应该通过模态 window 输入原因。不幸的是,我对前端开发不满意,模态 window 没有显示:它没有生成在 html output 的页面中。 Here is the code I wrote:这是我写的代码:

1 - Skills controller creates a notification when the Reject button (in the Skill's Show view) is hit, and tries to render the notifications_edit_description_form.html.erb partial of the Notifications controller (which uses the @notification variable). 1 - 技能 controller 在点击拒绝按钮(在技能的显示视图中)时创建通知,并尝试呈现通知 controller 的 notifications_edit_description_form.html.erb 部分(使用@notification 变量)。

  def reject
    puts "-------------------------------------------------------"
    puts "------------------ REJECTED ---------------------------"
    puts "-------------------------------------------------------"
    @skill.reject!
    @skill.update_attribute(:status_id, statuses.find { |x| x["code"] == "REJECTED" }.id || 0)
    @notification = Notification.create(playground_id: current_playground, description: t('SkillRejected'), severity_id: options_for('rules_severity').find { |x| x["code"] == "CORRECTION" }.id || 0,
    status_id: statuses.find { |x| x["code"] == "NEW" }.id || 0, expected_at: Time.now + 1.day, responsible_id: @skill.parent.parent.reviewer_id, owner_id: current_user.id, created_by: current_login, updated_by: current_login,
    topic_type: @skill.class.name, topic_id: @skill.id, deputy_id: @skill.parent.parent.responsible_id, organisation_id: @skill.organisation_id, code: @skill.code, name: t('SkillRejected'))
    # Create title and description for current language
    @notification.name_translations.create(field_name: 'name', language: current_language, translation: "#{t('Skill')} #{@skill.code} #{@skill.workflow_state}")
    @notification.description_translations.create(field_name: 'description', language: current_language, translation: "#{format_datetime(Time.now)} #{current_user.name} :\n> #{t('SkillRejected')}")
    render partial: 'notifications/edit_description_form', data: { toggle: "modal", target: "#editNotificationModal" }
  end

2 - The notifications_edit_description_form.html.erb partial is: 2 - notifications_edit_description_form.html.erb 部分是:

<%= form_with model: @notification, html: {id: "edit_description_form"} do |f| %>

  <div class="modal-header">
    <h5 class="modal-title" id="editNotificationModalLabel">
      <% if content_for?(:notificationModalTitle) %>
        <%= yield(:notificationModalTitle) %>
      <% else %>
        <%= t('EditNotification') %>
      <% end %>
    </h5>
    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
      <span aria-hidden="true">&times;</span>
    </button>
  </div>
  <div class="modal-body">

    <%= render partial: "shared/error_list", locals: { errors: @notification.errors } %>

    <fieldset>
      <div class="row mat-form-row">
        <div class="mat-form-field col-md-4">
          <%= f.label :notification_severity_id, t('NotificationSeverity'), class: "mat-form-field-label" %>
          <%= f.collection_select :severity_id, options_for('rules_severity') , :id, :name, {}, { class: "mat-input-element" } %>
        </div>
        <div class="mat-form-field col-md-4">
          <label for="notification-issuer" class="mat-form-field-label">
            <%= t('Issuer') %>
          </label>
          <input id="notification-issuer" class="mat-input-element" readonly
            value="<%= @notification.owner.name %>" />
        </div>
        <div class="mat-form-field col-md-4">
          <%= f.label :notification_expected_at, t('NotificationDueDate'), class: "mat-form-field-label" %>
          <%= f.date_field :expected_at, { class: "mat-input-element" } %>
        </div>
      </div>
      <div class="row mat-form-row">
        <div class="mat-form-field col-md-4">
          <%= render partial: "shared/translated_field_form", locals: {
            f: f,
            field: "description",
            fields: :description_translations,
            translations: @notification.description_translations,
            isTextarea: true,
            smallTextarea: true,
            label: t('Description') } %>
        </div>
      </div>
    </fieldset>
  </div>

  <div class="modal-footer">
    <button type="button" class="mat-stroked-button mat-button-base" data-dismiss="modal">
      <%= t('Cancel') %>
    </button>
    <button class="mat-flat-button mat-button-base mat-primary">
      <%= t('Submit') %>
    </button>
  </div>

<% end %>

3 - The target is defined in the Skill's Show view. 3 - 目标在技能的显示视图中定义。

<!-- Notification Modal -->
<aside class="modal fade modal-remote" id="editNotificationModal" tabindex="-1" role="dialog" aria-labelledby="editNotificationModalLabel" aria-hidden="true">
  <div class="modal-dialog modal-dialog-centered modal-lg">
    <div class="modal-content"></div>
  </div>
</aside>

4 - The console reports that the partial has been rendered: 4 - 控制台报告部分已呈现:

  Rendered notifications/_edit_description_form.html.erb (67.1ms)

What did I miss?我错过了什么? Where is my modal gone, and how to solve this?我的模态哪里去了,如何解决这个问题?

Thanks a lot for your help!非常感谢你的帮助!

The solution I found may not be optimum, but this works: from the Show view, link to new Notification path with modal target actually opens the notification in the modal window.我找到的解决方案可能不是最佳的,但它有效:从“显示”视图,链接到具有模态目标的新通知路径实际上在模态 window 中打开通知。

        <%= link_to t('Reject'), new_notification_path(
                                  playground_id: current_playground,
                                  description: t("#{this_object.class.name}#{'Rejected'}"),
                                  severity_id: options_for('rules_severity').find { |x| x["code"] == "CORRECTION" }.id || 0,
                                  status_id: statuses.find { |x| x["code"] == "NEW" }.id || 0,
                                  expected_at: Time.now + 1.day,
                                  responsible_id: this_object.owner_id,
                                  owner_id: current_user.id,
                                  created_by: current_login,
                                  updated_by: current_login,
                                  topic_type: this_object.class.name,
                                  topic_id: this_object.id,
                                  deputy_id: this_object.parent.responsible_id,
                                  organisation_id: this_object.organisation_id,
                                  code: this_object.code,
                                  name: t("#{this_object.class.name}#{'Rejected'}")
                                    ),
                                  title: t('Reject'), data: { 
                                  toggle: "modal", target: "#editNotificationModal" 
                                  }, class: "mat-stroked-button mat-button-base mat-primary" %>

When the notification modal window closes, the Skill show view is still available.当通知模式 window 关闭时,技能展示视图仍然可用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Ruby on Rails:如何从相同的控制器渲染具有不同路线的视图? - Ruby on Rails: how to render a view from same controller but with a different route? 在Rails 5.2中将不同的控制器方法呈现为JSON - Render different controller methods as JSON in Rails 5.2 Ruby on Rails 3:尝试从不同的控制器渲染视图。 但我到同一个控制器 - Ruby on Rails 3 : Trying to render a view from a different controller. But I get to the same controller 如何将一个控制器的局部视图渲染到轨道上的ruby中的另一个控制器 - How to render partial view of one controller to another controller in ruby on rails Ruby on Rails,如何让控制器在不重定向的情况下呈现不同的页面? - Ruby on Rails, How to make a controller render different page without redirect? 如何从其他控制器渲染视图? - How to render view from of a different controller? 如何从控制器渲染不同的视图 - How to render different view from controller Ruby on Rails:如何使用不同的控制器从一个视图/页面链接/路由到另一个视图/页面 - Ruby on Rails: How to link/route from one view/page to another view/page with a different controller 如何最好地在Rails中的Ruby视图中渲染不同的部分 - How best to render different partials in a view in ruby on rails 在 Rails 5.2 中更新失败后如何呈现模式? - How to render a modal after failed update in Rails 5.2?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM