Note that there are some explanatory texts on larger screens.

plurals
  1. PORails 3 - link_to to call partial using jquery ajax
    text
    copied!<p>I am trying to get a <strong>link_to</strong> to display a partial via jquery ajax, but can't seem to get it to work (edit: returning blank screen) and I'm not sure what I'm missing. Any help would be appreciated.</p> <p>I would like to click the link "Preview Widget" and have it display <strong>_widget.html.erb</strong> in the div <strong>preview</strong>. </p> <p>From my understanding the link "Preview Widget" should call the action <strong>def preview_widget</strong> which calls <strong>preview_widget.js.erb</strong> which then renders the partial <strong>_widget.html.erb</strong> in the div.</p> <p><strong>EDIT:</strong> updates link as per Ignatius Reza suggestions</p> <p><strong>show.html.erb</strong> </p> <pre><code>&lt;%= link_to "Preview Widget", :action =&gt; 'preview_widget' , :id =&gt; @widget.id, :remote =&gt; true %&gt; %&gt; &lt;div id="preview"&gt;&lt;/div&gt; </code></pre> <p><strong>widget_controller.rb</strong> </p> <pre><code>def preview_widget respond_to do | format | format.js {render :layout =&gt; false} end end </code></pre> <p><strong>preview_widget.js.erb</strong></p> <pre><code>$( "#preview" ).html( "&lt;%= escape_javascript( render( :partial =&gt; "widget", :locals =&gt; { :widget =&gt; @widget} ) ) %&gt;" ); </code></pre> <p><strong>_widget.html.erb</strong> </p> <pre><code>&lt;% @widget.videos.each do |video| %&gt; &lt;h3&gt;&lt;a href='#'&gt;&lt;%= video.name %&gt;&lt;/a&gt;&lt;/h3&gt; &lt;div&gt; &lt;object height='316' width='540'&gt; &lt;embed style='border: 1px solid #333333;' height='316' width='540' allowfullscreen='true' allowscriptaccess='always' type='application/x-shockwave-flash' src='&lt;%= video.url %&gt;'&gt; &lt;/object&gt; &lt;/div&gt; &lt;% end %&gt; </code></pre> <p><strong>routes.rb</strong> </p> <pre><code>match 'preview_widget' =&gt; 'widgets#preview_widget' </code></pre>
 

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