Note that there are some explanatory texts on larger screens.

plurals
  1. PORails 3 - How do I dynamically render a partial for a single object based on selected div using Jquery?
    primarykey
    data
    text
    <p>I have a simple jquery auto complete search in my Rails 3 app based on the one in <a href="http://railscasts.com/episodes/240-search-sort-paginate-with-ajax" rel="nofollow noreferrer">Railscasts episode #240</a> that filters a list of location objects on the location index page by rendering a partial using jquery.</p> <p>In the partial (<strong>_locations.html.erb</strong>), the location objects are each wrapped in their own div and are styled to look like a clickable element. When the user clicks one of them, I'd like it to do two things: </p> <ol> <li><p>Change the selected div's background color to show that it's selected (this part already works fine).</p></li> <li><p>Render content that's specific to the selected object in an empty div. A link to the show page will be included in the rendered content. This will basically function like a quickview/preview.</p></li> </ol> <p>This all changes dynamically according to the current selection without refreshing the page.</p> <h3>My Problem:</h3> <p>I'm having troubles with the second point. I can't seem to get the partial to render when the location element is clicked. I've tried a lot of different things over the past week. There are a couple examples that have gotten me close (<a href="https://stackoverflow.com/questions/4766383/rails-3-link-to-to-call-partial-using-jquery-ajax">Rails 3 - link_to to call partial using jquery ajax</a>, <a href="https://stackoverflow.com/questions/6901332/render-a-partial-in-rails-using-jquery">Render a partial in rails using jquery</a>) in addition to many others that aren't as relevant but follow a similar strategy.</p> <p>I suspect it might be a problem in the way I'm calling a partial within a partial, but I'm not sure. I included my code below. When I try it out, I'm taken to a blank page with the following address: localhost:3000/locations/1/show_quickview </p> <p>Thanks for any help in advance. Also, thanks for helping me get this far - you guys are great!</p> <p><strong>index.html.erb</strong> </p> <pre class="lang-rb prettyprint-override"><code>&lt;%= form_tag locations_path, :method =&gt; 'get', :id =&gt; "locations_search" do %&gt; &lt;p&gt; &lt;%= text_field_tag :search, params[:search] %&gt; &lt;%= submit_tag "Search", :name =&gt; nil %&gt; &lt;/p&gt; &lt;% end %&gt; &lt;div id="quickview"&gt;&lt;/div&gt; #placed here only for testing out &lt;div id="locations"&gt; &lt;%= render 'locations' %&gt; &lt;/div&gt; </code></pre> <p><strong>_locations.html.erb</strong></p> <pre class="lang-rb prettyprint-override"><code>&lt;% @locations.each do |location| %&gt; &lt;div class="location"&gt; &lt;h4&gt;&lt;%= link_to location.name, show_quickview_location_path(:id =&gt; location.id), :remote =&gt; true %&gt;&lt;/h4&gt; &lt;/div&gt; &lt;% end %&gt; </code></pre> <p><strong>_quickview.html.erb</strong></p> <pre class="lang-rb prettyprint-override"><code>&lt;p&gt;&lt;%= location.name %&gt;&lt;/p&gt; #keeping it simple for now </code></pre> <p><strong>routes.rb</strong></p> <pre class="lang-rb prettyprint-override"><code> resources :locations do member do get 'show_quickview' end end </code></pre> <p><strong>locations_controller.rb</strong></p> <pre class="lang-rb prettyprint-override"><code> def show_quickview respond_to do |format| format.js { render :layout =&gt; false } end end </code></pre> <p><strong>show_quickview.js.erb</strong></p> <pre class="lang-rb prettyprint-override"><code>$("#quickview").html("&lt;%= escape_javascript(render(:partial =&gt; "quickview", :locals =&gt; { :location =&gt; location }))) %&gt;"); </code></pre> <p><strong>application.js</strong></p> <pre class="lang-rb prettyprint-override"><code>$(function() { $("#locations_search input").keyup(function() { $.get($("#locations_search").attr("action"), $("#locations_search").serialize(), null, "script"); return false; }); $(".location").click(function() { $(".location").not(this).removeClass('location-clicked h4'); $(this).toggleClass('location-clicked h4'); }); }); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload