Ruby on Rails:基本的参数化查询和URL形成 [英] Ruby on Rails: Basic parameterized queries and URL formation

查看:148
本文介绍了Ruby on Rails:基本的参数化查询和URL形成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试学习如何查询Rails数据库并将结果作为JSON返回.在我的示例中,我想使用参数,城市和州来查询数据.

I'm trying to learn how to query a rails database and return the results as JSON. In my example, I want to query the data using the parameters, city and state.

到目前为止,在我的控制器中,我已执行以下操作.

So far, in my controller, I have gotten the following action to work.

  def state
    @bathrooms = Bathroom.where("state = ?" ,params[:state])
    respond_to do |format|
      format.json  { render :json => @bathrooms }
      format.js   { render :nothing => true } 

    end
  end

这也是我的路由条目.

  match '/bathrooms/state/:state',
              :controller => "bathrooms",
              :action => "state"

我可以使用以下URL调用此资源:

I can call this resource with the following URL:

http://localhost:3000/bathrooms/state/CA.json

这很好,但是我不知道如何通过多个参数进行查询.在控制器中添加AND子句似乎很容易.

That's all good but I don't know how to query by more than one parameter. Adding and AND clause in the controller seems to be easy enough.

但是....我不知道如何

BUT....I don't know how to

a.)正确编写路由条目? b.)如果我在浏览器中对其进行测试,URL会是什么样?

a.) Correctly write the routing entry? b.) What would the URL look like if I tested it in a browser?

我试图了解耙的路线,但我一定缺少一些东西.

I've tried to understand rake routes but I must be missing something.

有人可以提供一个基本示例来说明该操作的外观吗?路由条目应该是什么样的,访问资源的URL是什么样的?

Could someone provide a basic example for what the action should look like? What the routing entry should look like and what does the URL to access the resource look like?

同样,如果用SQL编写,这就是我想要返回的内容.

Again, if written in SQL, this is what I would like to be returned.

SELECT * from bathrooms WHERE city='Chicago' AND state = 'IL'

任何帮助表示赞赏.

推荐答案

您不必通过路由传递所有内容-网址还支持GET参数-这些是您所需要的参数通常会在网址中的问号后面看到.您可以添加这些GET参数,而无需更改路由:http://localhost:3000/bathrooms/state/IL.json?city=Chicago.然后,您可以通过params[:city]访问city参数.但是对于您而言,我认为使用http://localhost:3000/bathrooms/index.json?state=IL&city=Chicago会更好.您还需要将路由更改为

You don't have to pass everything by the route - the URL also support GET parameters - those are the parameters you usually see after the question mark in the URL. You can add those GET parameters without changing your routes: http://localhost:3000/bathrooms/state/IL.json?city=Chicago. Then your can access the city parameter via params[:city]. but in your case, I think it will be better to use http://localhost:3000/bathrooms/index.json?state=IL&city=Chicago. You'll also need to change your routing to

match '/bathrooms/index',
    :controller=>:bathrooms,
    :action=>:index

,然后将代码放入BathroomsControllerindex方法中.您可以以相同的方式访问参数-但概念不同-您无需输入州并按城市查找浴室,而只需按州和城市查找浴室即可.

and put the code in the index method of BathroomsController. You access the parameters the same - but the concept is different - you don't enter a state and look for bathrooms by city, you just look for bathrooms by state and city.

无论如何,您都不想手工编写URL-您想在Rails助手或HTML表单中生成它:

Anyways, you don't want to write the URL by hand - you want to a Rails helper or an HTML form generate it:

link_to "bathroom in Chicago, IL",:controller=>:bathrooms,:action=>:index,:state=>'IL',:city=>'Chicago'

如果要使用表格(让用户选择自己的州和城市),则需要将其方法设置为GET:

If you want to use a form(to let the users choose their own state and city), you need to set it's method to GET:

form_tag {:controller=>:bathrooms,:action=>:index},:method=>:get do

,然后将statecity用作字段.

值得注意的是,虽然您可以使用SQL的AND通过多个字段执行搜索,但也可以链接where方法:Bathroom.where(:state=>params[:state]).where(:city=>params[:city]).

It's also worth noting that while you can use SQL's AND to perform a search by multiple fields, you can also chain where methods: Bathroom.where(:state=>params[:state]).where(:city=>params[:city]).

这篇关于Ruby on Rails:基本的参数化查询和URL形成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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