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

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

问题描述

我试图让 link_to 通过 jquery ajax 显示部分,但似乎无法让它工作(返回空白屏幕),我不确定我是什么我不见了任何帮助将不胜感激.

我想点击预览小部件"链接,让它在 div 预览 中显示_widget.html.erb.

根据我的理解,链接预览小部件"应该调用操作 def preview_widget 调用 preview_widget.js.erb 然后呈现部分 _widget.html.erb 在 div 中.

根据 Ignatius Reza 的建议更新链接

show.html.erb

<%= link_to "Preview Widget", :action =>'preview_widget' , :id =>@widget.id, :remote =>真%>%><div id="预览"></div>

widget_controller.rb

def preview_widgetresponse_to do |格式|format.js {render :layout =>错误的}结尾结尾

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><对象高度='316' 宽度='540'><embed style='border: 1px solid #333333;'height='316' width='540' allowfullscreen='true' allowscriptaccess='always' type='application/x-shockwave-flash' src='<%= video.url %>'></对象>

<%结束%>

routes.rb

匹配'preview_widget' =>'小部件#preview_widget'

解决方案

好的,通过以下更改终于可以使用了.
routes.rb(向小部件资源添加成员)

 资源 :widgets 做会员做得到'preview_widget'结尾结尾

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

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

现在显示部分.我仍然不能 100% 确定之前的代码有什么问题 - 至少现在可以使用了.

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'

解决方案

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 (changed link_to to match routes)

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

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天全站免登陆