如何从Ruby on Rails应用程序外部访问数据?

时间:2022-12-26 00:18:06

I'm trying to work with the data in my Rails application from within a separate Ruby script.

我正在尝试从一个独立的Ruby脚本中处理Rails应用程序中的数据。

I read this forum post in which some people suggest that the best way to work with your data is to encapsulate the database within one application, and then have this application provide an API for working with that data. Because it's apparently bad to integrate your database into several different applications.

我读过这篇论坛文章,其中有人建议,处理数据的最佳方式是将数据库封装在一个应用程序中,然后让这个应用程序提供一个API来处理这些数据。因为将数据库集成到几个不同的应用程序中显然是不好的。

Well, now I want to work with some data from my Rails app from another script, but on the same system. How can I achieve this?

现在,我想处理来自Rails应用程序的一些数据来自另一个脚本,但是是在同一个系统上。我如何做到这一点?

I might want to work with the data from my Rails app remotely in the future, but also from a script. I'm assuming this might require JSON or SOAP, but I would like to know before I go researching it.

将来我可能希望远程处理来自Rails应用程序的数据,但也希望使用脚本。我假设这可能需要JSON或SOAP,但在我研究它之前我想知道。

3 个解决方案

#1


6  

Have you take a look at ActiveResource? It's specifically designed to expose data from a Rails model to another Rails app over HTTP.

你看过ActiveResource了吗?它专门设计用于通过HTTP将来自Rails模型的数据公开给另一个Rails应用程序。

#2


1  

Since Ruby on Rails follows REST, your application is, by default, it's own API. For example, say you have the following controller:

由于Ruby on Rails遵循REST,所以默认情况下,应用程序是自己的API。例如,假设您有以下控制器:

class UsersController < ApplicationController

  def show
    @user = User.find(params[:id])
    respond_to do |format|
      format.html
      format.xml  { render :xml => @user}
      format.js
    end
  end

  def index
    @users = User.all
    respond_to do |format|
      format.html
      format.xml  { render :xml => @users}
      format.js
    end
  end
end

Now, when hitting that controller via the web browser, it will render your views as you would expect. For example:

现在,当通过web浏览器访问该控制器时,它将呈现您的视图。例如:

GET /users/1   =>  /app/views/users/show.html.erb
GET /users     =>  /app/views/users/index.html.erb

However, if you change your requests to be something like:

但是,如果你将你的请求改为:

GET /users/1.xml
GET /users.xml

You'll be returned XML data instead of your HTML views. You can now access this data from any other application by using some sort of REST Client, or simply by calling cURL from any command line.

您将返回XML数据而不是HTML视图。现在,您可以通过使用某种REST客户端来从任何其他应用程序访问这些数据,或者简单地从任何命令行调用cURL。

You can append any extension to the end of your URL, and it will find the appropriate respond_to section.

您可以将任何扩展附加到URL的末尾,它将找到适当的respond_to部分。

#3


0  

Accessing the data is simple too, just make a request to your application using something like HTTParty. Look at the examples, they're pretty good.

访问数据也很简单,只需使用HTTParty之类的工具向应用程序发出请求。看看这些例子,它们很好。

#1


6  

Have you take a look at ActiveResource? It's specifically designed to expose data from a Rails model to another Rails app over HTTP.

你看过ActiveResource了吗?它专门设计用于通过HTTP将来自Rails模型的数据公开给另一个Rails应用程序。

#2


1  

Since Ruby on Rails follows REST, your application is, by default, it's own API. For example, say you have the following controller:

由于Ruby on Rails遵循REST,所以默认情况下,应用程序是自己的API。例如,假设您有以下控制器:

class UsersController < ApplicationController

  def show
    @user = User.find(params[:id])
    respond_to do |format|
      format.html
      format.xml  { render :xml => @user}
      format.js
    end
  end

  def index
    @users = User.all
    respond_to do |format|
      format.html
      format.xml  { render :xml => @users}
      format.js
    end
  end
end

Now, when hitting that controller via the web browser, it will render your views as you would expect. For example:

现在,当通过web浏览器访问该控制器时,它将呈现您的视图。例如:

GET /users/1   =>  /app/views/users/show.html.erb
GET /users     =>  /app/views/users/index.html.erb

However, if you change your requests to be something like:

但是,如果你将你的请求改为:

GET /users/1.xml
GET /users.xml

You'll be returned XML data instead of your HTML views. You can now access this data from any other application by using some sort of REST Client, or simply by calling cURL from any command line.

您将返回XML数据而不是HTML视图。现在,您可以通过使用某种REST客户端来从任何其他应用程序访问这些数据,或者简单地从任何命令行调用cURL。

You can append any extension to the end of your URL, and it will find the appropriate respond_to section.

您可以将任何扩展附加到URL的末尾,它将找到适当的respond_to部分。

#3


0  

Accessing the data is simple too, just make a request to your application using something like HTTParty. Look at the examples, they're pretty good.

访问数据也很简单,只需使用HTTParty之类的工具向应用程序发出请求。看看这些例子,它们很好。