Finding Actions in a Reils Rail Model

So, I am completely new to Rails and still working on principles. I believe that, like most people, I started building this basic CRUD. Good. Done.

Now I need a new action: search. As it turned out, this is not one of the 7 sacred (!) Actions (if this is correct). While I know I can implement new custom actions and resource and that's it, I have read in several places to try my best to stick to the standard ones as much as possible. Good. What would be the correct way?

Again several sources like this guy suggest thinking about my scripts in noun topics , a case where it seems I need a "search" controller ...? It just doesn't convince me that I need to create a whole class whereas I usually do def search

to keep it ResTful.

How was I wrong? What would be the general solution here?



source to share

3 answers

It depends if your search was against ONE resource or many resources. For example, if you have a ProductController and you want to implement a search function only for your products, you can create a collection action called "search" (url will be / products / search)

If your search is for many resources, I would create a SearchesController with a singleton resource: search in routes file.

Then, when you implement search functionality in your application, don't put all the logic in your controller, but create model classes to handle your search. You can even create an abstract class to match the search form and thus avoid using "* _tag" fields to create your search form.




REST is a concept, not a religion :-). But the main verbs are GET / POST / PUT / DELETE, which are matched against the corresponding HTTP verbs. What's in a URL is usually a reflection of that, and (indeed, it's a Rails philosophy) the following convention can make things a lot easier. The URLs you get with shared rails (like the scaffold) aren't particularly perfect in different ways, but they work and you can change them.

So yes, for a search (assuming it starts out just like searching for records in one model, say Product

), then you can do a GET with a query string like this

def search
  @results = Product.where("name ILIKE ?", params[:query])


This will result in an invalid url like /product/search?query="foo"

- nothin '.



If you want to stick with REST (this is really a guide, it has pros and cons), then the slideshow you link to recommends the correct way to do things.

So, for example, if you have a comment_controller and you want to be able to search for comments, you can create a comment_search_controller. The search form will be in comment_search_controller # new, which will be a POST to comments_search_controller # create.

Yes, you create another class that does it this way, but this is not much different from creating another activity in comment_controller, and it maintains consistency and separation. You don't need a new CommentSearch model or anything else, just this controller, which queries your comment model for matching search results.



All Articles