Rails 3-link_to使用jQuery Ajax调用部分 [英] Rails 3 - link_to to call partial using jquery ajax

查看:79
本文介绍了Rails 3-link_to使用jQuery Ajax调用部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图获取 link_to 以通过jquery ajax显示部分内容,但似乎无法使其正常工作(返回黑屏),我不确定该怎么办不见了.任何帮助将不胜感激.

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.

我想单击预览小部件"链接,并使其在div 预览中显示 _widget.html.erb .

I would like to click the link "Preview Widget" and have it display _widget.html.erb in the div preview.

据我所知,链接"Preview Widget"应该调用操作 def Preview_widget ,该操作调用 preview_widget.js.erb ,然后呈现部分 _widget.html .erb 在div中.

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.

编辑:根据Ignatius Reza的建议更新链接

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'

推荐答案

好吧,终于可以将其与以下更改一起使用了.
routes.rb (将成员添加到小部件资源)

Ok finally got this to work with the following changes.
routes.rb (added member to widget resources)

  resources :widgets do
    member do
      get 'preview_widget'
    end
  end

show.html.erb (已更改link_to以匹配路线)

show.html.erb (changed link_to to match routes)

<%= link_to 'Preview', preview_widget_widget_path(:id => @widget.id),  :remote => true %>

现在显示部分内容.我仍然不能100%地确定先前的代码出了什么问题-至少现在可以正常工作.

This now shows the partial. I am still not 100% sure what was wrong with the previous code - at least it now works.

这篇关于Rails 3-link_to使用jQuery Ajax调用部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆