title: twitter bootstrap modal with ruby on rails
body: <p>I have run into a bit of a problem and a head scratcher, as I'm not sure whether what I want to do is even possible with RoR.</p>
  
  <p>A bit of background info: I have an app with several controllers... and would like to work with 2 of them for this modal example. The first is a users_controller and the other is a recommendations_controller.</p>
  
  <p>Here is what I'm trying to do: In the user index view I have a lists of posts. Each post has a  recommend button. If a user clicks it he/she is sent to the recommendation index page, and can search and find a user he/she would like to share the post with. The reason I have it set up this way is because the Recommendation model creates the recommend relationship. </p>
  
  <p>I would like like it so when the user clicks the recommend button on the User index page, a modal appears (one that accesses the recommendation controller) and the user can search for the user he/she would like to share the post with. Basically, I want to know whether it's possible to access the Recommendation controller via the User controller's index view page.</p>
  
  <p>If it's not, is there a work around? I can post the code I have if it's helpful, but I'm not sure that would help in this case--as I'm trying to see whether it's even possible to do what I'm trying to do.</p>
  
  <p>Thank you!</p>
  
  <p><strong>More Details:</strong></p>
  
  <p>recommendations_controller:</p>
  
  <pre><code>  def index
      @user = Search.find_users(params[:name], current_profile)
  
      respond_to do |format|
        format.html
        format.js
      end   
    end
  </code></pre>
  
  <p>index.js.haml (located in the view/recommendations folder)</p>
  
  <pre><code>  :plain
      $(#my-modal).show();
  </code></pre>
  
  <p>index.html.haml (located in the view/recommendations folder)</p>
  
  <pre><code>  = form_tag post_recommendations_url, :method =&gt; "get" do
      = text_field_tag :name, '', :class =&gt; "span12", :placeholder =&gt; "Please enter the name of the users you would like to share this post with."
      %center
        = submit_tag "Search", :class =&gt; "btn btn-primary"
  </code></pre>
  
  <p>index.html.haml (located in the view/posts folder)</p>
  
  <pre><code>  %a{:href =&gt; "#{post_recommendations_path(post)}", :remote =&gt; true}
      %i.icon-share.icon-large
      Recommend Post
  
  #my-modal.modal.hide.fade
    .modal-header
      %a.close{"data-dismiss" =&gt; "modal"} ×
      %h6 This is a header
    .modal-body
    %p This is where I would like the contents of the index.html.haml file, located in the view/recommendations folder to appear.
  </code></pre>
  
  <h2><strong>Part 2: Displaying the search results inside the modal/partial</strong></h2>
  
  <p>Matzi, at the moment, a user clicks a link near the post they want to recommend. This link renders the modal with a partial (_recommendation.html.haml) inside of it.</p>
  
  <p>This partial (now inside the modal) contains the search <code>form_tag</code> and the code to render all the users that match the search results. Unfortunately, when I try to run a search by entering a name and clicking the search button (again, now located inside of the modal) it takes me to the following url instead of rendering the results inside the modal.</p>
  
  <p><code>http://localhost:3000/posts/2/recommendations?utf8=%E2%9C%93&amp;name=Test&amp;commit=Search</code></p>
  
  <p>here is what my updated <strong>index.html.haml (located in the view/posts folder)</strong> looks like:</p>
  
  <pre><code>= link_to 'Recommend Post', post_recommendations_path(post), :remote =&gt; true, "data-toggle" =&gt; "modal"
  
  #my-modal.modal.hide
  .modal-header
    %a.close{"data-dismiss" =&gt; "modal"} ×
    %h6 
      %i.icon-share.icon-large
      Recommend Post
  .modal-body
    #modal-rec-body
      %p *this is where _recommendation.html.haml is rendered*
  </code></pre>
  
  <p>updated <strong>index.js.haml</strong></p>
  
  <pre><code>:plain
    $("#modal-rec-body").html("#{escape_javascript(render('recommendations/recommendation'))}");
  
    $('#my-modal').modal({
      keyboard: true,
      show: true
    });
  </code></pre>
  
  <p><strong>_recommendation.html.haml</strong></p>
  
  <pre><code>.span12
    = form_tag post_recommendations_path, :method =&gt; "get" do
      = text_field_tag :name, '', :class =&gt; "span12", :placeholder =&gt; "Please enter the name of the user you would like to share this post with.", :style =&gt; "max-width:520px;"
      %center
        = submit_tag "Search", :class =&gt; "btn btn-primary", :remote =&gt; "true"
  
  - @user.each do |i|
    - unless current_profile == i
      .row-fluid
        .span6
          .row-fluid
            .well{:style =&gt; "margin-left:0px;"}
              .row-fluid
                .span2
                  =image_tag i.avatar(:bio), :class=&gt; "sidebar_avatar"
                .span6
                  %dl{:style =&gt; "margin:0px"}
                    %dt
                      %i.icon-user
                      Name
                    %dd= i.name
                    %dt
                      %i.icon-map-marker
                      Location
                    %dd= i.location
                .span4
                  - form_for :recommendation do |r|
                    = r.hidden_field :friend_id, :value =&gt; i.account.id 
                    = r.submit "Send Recommendation", :class =&gt; "btn btn-primary"          
  </code></pre>
  
  <p><strong>Problem:</strong> Unfortunately it seems that when I click the submit (search) button inside the modal instead of rendering the results inside the modal it re-directs the browser to the <code>post_recommendations_path (posts/post.id/recommendations)</code>. I would like to display the search results inside the modal without having it redirect to the post recommendations path. </p>
  
  <p>As always, thank you so much! I'm extremely grateful for your help--and I've gotten a much better grasp for AJAX thanks to you. Thank you!</p>