Ruby on Rails: Basic parameterized queries and URL formation

…衆ロ難τιáo~ 提交于 2019-12-11 07:28:41

问题


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

This is also my routing entry.

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

I can call this resource with the following URL:

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

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.) 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.

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?

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

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

Any help appreciated.


回答1:


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

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.

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'

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

and put state and city as fields.

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]).




回答2:


You can put any arbitrary parameters in your querystring.

For example:

http://localhost:3000/bathrooms/state/CA.json?city=Chicago

your query looks like this:

@bathrooms = Bathroom.where("state = ? and city= ?" ,params[:state], params[:city])


来源:https://stackoverflow.com/questions/10180769/ruby-on-rails-basic-parameterized-queries-and-url-formation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!