I am trying to get a link_to 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.
I would like to click the link "Preview Widget" and have it display _widget.html.erb in the div preview.
From my understanding the link "Preview Widget" should call the action def preview_widget which calls preview_widget.js.erb which then renders the partial _widget.html.erb in the div.
EDIT: updates link as per Ignatius Reza suggestions
show.html.erb
<%= link_to "Preview Widget", :action => 'preview_widget' , :id => @widget.id, :remote => true %> %>
<div id="preview"></div>
widget_controller.rb
def preview_widget
respond_to do | format |
format.js {render :layout => false}
end
end
preview_widget.js.erb
$( "#preview" ).html( "<%= escape_javascript( render( :partial => "widget", :locals => { :widget => @widget} ) ) %>" );
_widget.html.erb
<% @widget.videos.each do |video| %>
<h3><a href='#'><%= video.name %></a></h3>
<div>
<object height='316' width='540'>
<embed style='border: 1px solid #333333;' height='316' width='540' allowfullscreen='true' allowscriptaccess='always' type='application/x-shockwave-flash' src='<%= video.url %>'>
</object>
</div>
<% end %>
routes.rb
match 'preview_widget' => 'widgets#preview_widget'