Rails呈现另一个模型的模式-路由或javascript问题?

Rails呈现另一个模型的模式-路由或javascript问题?,javascript,jquery,ruby-on-rails,ajax,twitter-bootstrap,Javascript,Jquery,Ruby On Rails,Ajax,Twitter Bootstrap,在过去的几天里,我一直被这个问题困扰着,找不到答案 我有一个配置文件表单,称为模板形式。每次使用template_modal时,配置文件可以上载多个模板 显示模式,但按提交后,它会重定向到profiles_controller#create,当它应该是templates_controller#create时,那么如何将模板保存到DB?如果有点小改动,请告诉我。预期的行为是保存模板,并在配置文件表单上使用AJAX显示 SO和博客上的所有示例都有一个单一的模型模式,但没有解决两个控制器的问题 我不确

在过去的几天里,我一直被这个问题困扰着,找不到答案

我有一个配置文件表单,称为模板形式。每次使用template_modal时,配置文件可以上载多个模板

显示模式,但按提交后,它会重定向到profiles_controller#create,当它应该是templates_controller#create时,那么如何将模板保存到DB?如果有点小改动,请告诉我。预期的行为是保存模板,并在配置文件表单上使用AJAX显示

SO和博客上的所有示例都有一个单一的模型模式,但没有解决两个控制器的问题

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

我从以下内容中获取了代码片段:

以下是代码:

配置文件\控制器-它呈现模板\模式

<%= 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 %>
视图/公司模板/_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 %>
$(“ul.errors”).html(“”)
$(“ul.errors”).append($(“
  • ”)html(“”) $(“.template index”).html(“”) $(“#新模板-模式”).model(“隐藏”); $(“#模板_表”)。追加(“”); $('new_template_modal').modal(“隐藏”);
  • 请告知我做错了什么?谢谢。

    试试看

    #views/company_templates/new.js.erb
    $("#template-modal").remove();
    $("body").html("<%=j render 'new_modal' %>");
    $("#template-modal").modal("show");
    
    #视图/公司模板/new.js.erb
    $(“#模板模式”).remove();
    $(“body”).html(“”);
    $(“#模板模式”).model(“显示”);
    
    没有详细说明,但您确定没有在另一个表单中呈现表单吗?Thx Mandeep。我不明白你的意思…一个模态对话框是从配置文件的主窗体呈现出来的。模态打开时包含所有字段,但在按下模态的“提交”时,它会提交主表单-配置文件。您在配置文件表单中的模板模态元素中注入了新的#模态元素,因此在另一个表单中插入了一个表单。哦,很抱歉延迟,我想我在这方面失去了您,所以我没有检查是否有人回复……是的,你是对的……那么出路是什么呢?让我告诉你,在进行模态分析之前,我只是通过链接(新用户、公司、临时路径)访问相同的模板普通表单,但有两个问题-1)在提交模板后无法将模板重定向回profile/new,因为重定向没有返回两步…对此有什么想法吗??(2) 由于没有AJAX,表单将在返回时刷新profile/new。这就是我决定使用模态方法的原因。将模态附加到主体中,并且在渲染之前不要忘记删除它,否则您将拥有多个模态OK great it’s God to templates_controller/#create。但它说的是无效的真实性令牌,我该如何解决它呢?许多Thx@Mandeep。我用[这个]()解了你的代币。但是现在我如何在不刷新的情况下重定向回profile/new?
    <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>
    
    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
    
    $("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 %>
    
    #views/company_templates/new.js.erb
    $("#template-modal").remove();
    $("body").html("<%=j render 'new_modal' %>");
    $("#template-modal").modal("show");