0

首先,当我单击链接时,这不会显示任何错误。任何人都可以建议为什么在按钮单击时不显示模式?

我确实将 .js.erb 文件的内容转换为在我的开发工具的 Network/Preview 部分打印的 html:

$("#contactForm").html("<div class=\"modal fade\" id=\"contactForm\">\n  <div class=\"modal-dialog\">\n    <div class=\"modal-content\">\n      <div class=\"modal-header\">\n ...

我接到了link_to电话:

<%= link_to 'Contact', contact_form_path,  {:remote => true, 'data_toggle' =>  "modal", 'data-target' => '#contactForm'}  %>

联系人控制器方法:

  def contact_form
    @contact = Contact.new
    respond_to do |format|
     format.js 
    end
  end

路线.rb

get "contacts/contact_form" => 'contacts#contact_form', :as => :contact_form

contact_form.js.erb:

$("#contactForm").html("<%= escape_javascript(render(:partial => 'contacts/contact_form') )%>");

联系人/contact_forml.html.erb

<div class="modal fade" id="contactForm">
  <div class="modal-dialog">
    <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">send me a message</h4>
      </div>
      <div class="modal-body">
       <% if flash[:notice] %>  
         <p class='notice'><%=h flash[:notice] %></p>
       <% end %>
       <%= form_for(@contact) do |f| %>  
        <%= f.text_field :name, :placeholder => "first name" %>
        <%= f.text_field :email, :placeholder => "email" %>   
         <%= f.text_area :message, :placeholder => "your message..."%>
         <div class="actions"> 
           <%= f.submit :value => "Send Feedback", :class => "btn" %>   
         </div>
       <% end %>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
      </div>
    </div>
  </div>
</div>

谢谢。

4

1 回答 1

1

您只是忘记调用modal来显示模态。并且模态部分应该呈现到包装器 div 中,因此 contact_form.js.erb 应该如下所示:

$("#modal-div").html("<%= escape_javascript(render(:partial => 'contacts/contact_form') )%>");
$("#contactForm").modal();

并且您添加模态的视图应该有空的 div 元素

<div id="modal-div"></div>
于 2013-08-06T22:23:01.680 回答