如何使用JQuery通过ruby on rails action方法从数据库加载内容? [英] How can I use JQuery to load content from a database via a ruby on rails action method?

查看:83
本文介绍了如何使用JQuery通过ruby on rails action方法从数据库加载内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个页面需要从数据库和其他可能的来源中提取内容.

I have a page which needs to pull content from a database and potentially other sources.

这就是我在Rails应用程序中构建红宝石的方式.

This is how I've structured my ruby on rails application.

/Index-它具有三个不同的部分,需要加载三种不同类型的内容.每个人返回的时间会有所不同,因此我想在播放每个div时显示一条加载消息,以将数据填充到每个div中.

/Index - this has three different sections which needs to load in three different types of content. Each one will take different amount of time to return, as such I want to display a loading message while I'm playing for each div to be filled with data.

要加载数据,理想情况下,我想在控制器上调用Ruby方法,然后呈现部分视图.

To load the data, ideally I want to call into a Ruby method on my controller and then render a partial.

我已经进行了搜索,但是我看不到如何加载页面索引,然后从操作方法作为单独的操作加载数据.

I've had a search around, but I can't see how you can load the page index and then load data from action methods as a separate action.

推荐答案

如果您使用的是Rails的最新版本,并且希望以一种不引人注目的方式进行此操作,则可以使用以下命令中的:respond_to选项您的控制器,就像这样:

If you're using a more recent version of Rails, and wanted to do this in an unobtrusive fashion as possible, you'd use the :respond_to options in your controller, like so:

class MyController < ApplicationController

  def first_method
     respond_to do |format|
        format.html # this handles normal requests asking for html
        format.js # this handles requests asking for javascript to be sent instead
      end
  end

end

如果您对点击做出响应,则可以执行诸如在页面上触发查询之类的操作

If you're responding to a click, you'd do something like firing the query like so on the page

$("a.element_you_click_to_trigger").click(function(e) {
  e.preventDefault(); // make sure clicking doesn't trigger unneeded event bubbling
  $.ajax({url: "/controller/first_method", type: "POST", dataType: "script"}); // send a request and tell the server that you want javascript back
}

就像在该控制器的views文件夹中有一个index.html.erb文件一样,在侧面的相关views文件夹中也有一个类似的first_method.js.erb文件 您的index.html.erb文件:

Just like you have an index.html.erb file in your views folder for that controller, you'd have a similar first_method.js.erb file in relevant views folder along side your index.html.erb file:

views
  controller
    index.html.erb
    first_method.js.erb

这将返回在客户端执行的javascript,但在构建服务器端返回,因此您可以包含rails ERB片段,因此您可以执行以下操作:

This will return javascript that gets executed client side, but it gets build server side, so you can contain rails ERB fragments, so you can do something like this:

$('#loading_placeholder_element').html('<%= escape_javascript(render(:partial => "create")) %>');

// do some other stuff you fancy in the page

// then make the next call once the other stuff is over (you may need to add this as a call back on an animation):
$.ajax({url: "/controller/second_method", type: "POST", dataType: "script"});

然后,您将对fsmf突出显示的其他每个更长的方法再次重复相同的过程.

You'd then repeat the same process again for each of the others longer methods that fsmf highlighted.

我发现这基于jQuery的视频广播在我学习了几个月后非常有用以前,我真的会推荐它.

I found this Railscast on jQuery incredibly helpful when I was learning this a couple of months ago, and I'd really recommend it.

希望这会有所帮助!

这篇关于如何使用JQuery通过ruby on rails action方法从数据库加载内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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