Rails 3/jquery-使用ajax选项卡呈现局部 [英] Rails 3/jquery - Tabs rendering partials using ajax

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

问题描述

使用Rails 3和jquery,我试图为我的post#show页面设置一个简单的3标签导航,单击时将_post _comments和_related局部渲染到视图中.

Using Rails 3 and jquery, I'm trying to setup a simple 3 tab navigation for my post#show page, rendering _post _comments and _related partials into the view when clicked.

这些部分目前没有自己的控制器动作.

These partials don't have their own controller actions, currently.

处理这些ajax调用的最干净的Rails方法是什么?

What would be the cleanest Rails way to handle these ajax calls?

我试过了,但是没用:

后置控制器:

def tab  
  respond_to do |format|
    format.js
  end
end

发布后视图:

<%= link_to "Comments", {:action => 'tab'}, :remote => true %>

/views/posts/tab.js.erb

/views/posts/tab.js.erb

$('#postshow').html("<%= escape_javascript(render('comments')) %>");

但是,如果执行此操作,当我尝试打开我的跟踪显示"页面时,我会因此错误而挂断电话 没有路线匹配{:action =>"tab",:controller =>"posts"}"

However, if I do this, when I try to open my Track Show page i get hungup with this error "No route matches {:action=>"tab", :controller=>"posts"}"

我想我为Tab定义了一个动作,该动作应该呈现tab.js.erb....但更重要的是,仅在三个部分之间切换似乎有点笨拙.

i thought i defined an action for tab that should render the tab.js.erb... But more importantly this seems a little clunky to just switch around between three partials.

我们非常感谢您的帮助.谢谢.

Any help is much appreciated. Thanks.

推荐答案

好,知道了.只是需要将资源添加到route.rb中...以为那部分发生了神奇的变化.

Ok, got it. Just needed to add the resource to routes.rb... thought that part just magically happened.

将其放入routes.rb

put this in routes.rb

resources :posts do
  ...
  member do
    get 'tab'
  end
end

现在它找到路线并呈现javascript.

and now it finds the route and renders the javascript.

曾经说过,如果有人阅读了这篇文章,并指出了一种更干净或更简单的方法来为呈现的部分设置ajax链接,那将是非常美好的事情.

having said that, if anyone reads this post and can point out a way cleaner or simpler way to set up ajax links for rendered partials, that would be super sweet.

这篇关于Rails 3/jquery-使用ajax选项卡呈现局部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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