Joe Essey
Joe Essey

Reputation: 3527

How can I get the modal to display in the following situation?

Firstly, this does not display any errors when I click the link. Can anyone suggest why the modal is not being displayed on the button click?

I do get the contents of the .js.erb file converted to html printed in the Network/Preview section of my dev tools:

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

I've got the link_to call:

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

ContactsController method:

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

routes.rb

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

contact_form.js.erb:

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

contacts/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>

Thanks.

Upvotes: 0

Views: 82

Answers (1)

lazzi
lazzi

Reputation: 445

You just forgot to call the modal to show the modal. And modal partial should be rendered into wrapper div, so contact_form.js.erb should look like this:

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

And the view where you are adding modal should have empty div element

<div id="modal-div"></div>

Upvotes: 1

Related Questions