【问题标题】:Rails render modal of another model - routes or javascript issue?Rails 渲染另一个模型的模式 - 路由或 javascript 问题?
【发布时间】:2017-03-14 11:50:33
【问题描述】:

过去几天我一直被困在这个问题上,找不到答案。

我有一个调用 template_modal 表单的个人资料表单。配置文件每次使用 template_modal 可以有许多模板上传。

模式正在显示,但在按下提交时,它会重定向到profiles_controller#create,而它应该是templates_controller#create,那么如何将模板保存到数据库?如果有一点小调整,请告诉我。预期的行为是保存模板并在配置文件表单上显示 AJAX。

SO 和博客上的所有示例都具有单一模型模式,并且没有解决 2 个控制器的问题。

我不确定路由是否有问题,因为路由决定了控制器。实际上,模板模型也有非模态形式,但我在模板视图中分别命名了模态和非模态形式的部分。从技术上讲,当创建非模态模板时,路线显示:user/1/templates/new 现在通过呈现模态我已经在 profile/new 上,它正在呈现另一个模型的模态,因此对要访问哪个控制器的创建操作感到困惑。

我从here拿到了代码sn-ps:

代码如下:

profiles_controller - 它呈现 template_modal

<%= bootstrap_form_for @profile do |f| %>
   #Some fields of profile form
   <%= link_to "Add Template", new_user_company_template_path(current_user), remote: true, class: "btn btn-primary", 'data-toggle' => 'modal' %>

        <div id="template-modal" class="modal fade"></div>

        <table class='table' id='template_table'>
          <thead>
          <tr>
            <th>Name</th>
            <th>Template</th>
            <th> View </th>
            <th> Delete </th>
          </tr>
          </thead>
          <tbody class="template-index">
          <% @company_templates.each do |co_template| %>
              <%= render "company_templates/list", locals: {company_templates: @company_templates}  %>
          <% end %>
          </tbody>
        </table>
  <% end %>

views/company_templates/_list.html.erb

<% @company_templates.each do |co_template| %>
<tr>
  <td><%= co_template.name %></td>
  <td><%= co_template.template.file.basename %></td>
  <td><%= link_to 'Download', co_template %></td>
  <td><%= link_to 'Delete', co_template, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>

views/company_templates/new.js.erb

$("#template-modal").html("<%= escape_javascript(render 'new_modal') %>")
  $("#template-modal").modal("show")

views/company_templates/_form_modal.html.erb

<div class="modal-dialog" id="new_template_modal">
  <div class="modal-content">
    <div class="modal-header">
      <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
      <h4 class="modal-title" id="myModalLabel"> Add New Template </h4>
    </div>
    <%= form_for [current_user, CompanyTemplate.new], :url => url_for(:controller => 'company_templates', :action => 'create', params: {id: current_user.id}), remote: true, html: { style: "display:inline;" } do |f| %>
        <div class="modal-body">
          <ul class="errors"></ul>

          <%= f.hidden_field :company_id, :value => params[:user_id] %>
          <div class="form-group">
            <%= f.label :" Template Name ", class:"control-label" %>
            <%= f.text_field :name, class: "form-control" %>
          </div>
          <div class="form-group">
            <%= f.label :"Upload template", class: "control-label" %>
            <%= f.file_field :template, class: "form-control" %>
            <small>(File Restrictions: Only allowed - '.doc', '.docx', '.txt', '.rtf' & '.pdf' less than 2MB).</small>
          </div>
        </div>
        <div class="modal-footer">
          <%= f.submit "Submit Template", class: "btn btn-primary" %>
          <%= link_to "Cancel", "#", class: "btn", data: {dismiss: "modal"} %>
        </div>
    <% end %>
  </div>
</div>

company_templates_controller.rb

def create
  @company_templates = current_user.company_templates.all.order(created_at: "DESC")
 @company_template = CompanyTemplate.new(company_template_params)

      respond_to do |format|
        if @company_template.save
          format.html { redirect_to @company_template, alert: 'The template was successfully created.' }
          format.json { render action: 'show', status: :created, location: @company_template }
          format.js   { render 'save', status: :created, location: @company_template }
        else
          format.html { render action: 'new', alert: 'Problem uploading CV.Check if the file is one of .doc/.docx/.pdf type & less than 2MB).'}
          format.json { render json: @company_template.errors, status: :unprocessable_entity }
          # added:
          format.js   { render json: @company_template.errors, status: :unprocessable_entity }
        end
      end

views/company_templates/_save.js.erb

$("ul.errors").html("")
<% if @company_template.errors.any? %>
  <% @company_template.errors.full_messages.each do |message| %>
    $("ul.errors").append($("<li />").html("<%= message.html_safe %>"))
  <% end %>
<% else %>
  $(".template-index").html("<%= escape_javascript( render partial: 'list', locals: {co_template: @company_template }) %>")
  $("#new_template-modal").modal("hide");

  $('#template_table').append("<%= j render partial: 'list', locals: {co_template: @company_template } %>");
  $('#new_template_modal').modal("hide");
<% end %>

请指教我做错了什么?谢谢。

【问题讨论】:

  • 没有详细说明,但您确定不是在另一个表单中呈现表单吗?
  • 谢谢曼迪普。我没有明白你的意思....从配置文件的主窗体呈现模式对话框。模式正在打开所有字段,但在按下模式的提交时,它会提交主表单 - 配置文件。
  • 您在 #template-modal 元素内注入 new_modal,该元素位于您的个人资料表单内,因此是另一个表单内的表单
  • 抱歉耽搁了,我以为我在这件事上失去了你,所以我没有回来查看是否有人回复......是的,你是对的......所以出路是什么?让我告诉你,在做模态之前,我只是通过 link_to (new_user_company_tempate_path) 访问相同的模板范式,但是有两个问题- 1)它不能在提交模板后再次重定向到 profile/new,因为 redirect_back 没有后退 2 步...对此有什么想法吗? (2)由于没有AJAX,表单会在返回时刷新profile/new。这就是我决定使用模态方法的原因。
  • 在正文中添加模态框,不要忘记在渲染之前将其删除,否则您最终将拥有多个模态框

标签: javascript jquery ruby-on-rails ajax twitter-bootstrap


【解决方案1】:

试试

#views/company_templates/new.js.erb
$("#template-modal").remove();
$("body").html("<%=j render 'new_modal' %>");
$("#template-modal").modal("show");

【讨论】:

  • 好的,很好,它去了 templates_controller/#create。但它说无效的真实性令牌我该如何解决?
  • 感谢@Mandeep。我通过 [this] (stackoverflow.com/questions/24319353/…) 解决了authentity_token。但是现在如何在不刷新的情况下重定向回 profile/new?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-11-18
  • 1970-01-01
  • 1970-01-01
  • 2014-08-24
  • 2010-12-06
  • 1970-01-01
  • 2019-03-04
相关资源
最近更新 更多