Controller usage

I understand that controller's are basically the link between views and models. However, I'm a little confused as to how a controller should be structured beyond CRUD functionality.

Here's a use case:

During the creation of a new object the user will be asked to input a parameter. The parameter will be used to perform a search to help ensure the object doesn't already exist in the system before continuing.

I'm not sure where that search procedure should be located. I could put it in middleware controller but I'm not sure if that's the most appropriate place.

Do any of you more seasoned Rails developers have any insight?

Does this parameter entry occur along with the object parameters? As in, on the “new” page? It sounds like the search is part of the object creation, i.e. in the create action, to me. If it fails, redirect back to the new page just like a failed save would.

Adam Stegman

Here’s the logic:

  1. A user will navigate to the “new” page
  2. A user will be presented with an input box
  3. The user enters a value
  4. A search will be performed using AJAX
  5. If a search comes up with nothing a transition will occur providing the user the ability to enter the rest of the information

It’s logically separate from actually creating the object.

What you have is a problem of validation, so creation is like this, you have a form in the view , pass the info/paremeters to the controller and the controller will try to save the information, when it passes the object to the model for saving validation will kick in, as validation is a responsibility of the model , so you will have something like this in the model, validates_uniqueness_of :whatever and you can even scope that uniqueness adding :scope => city so the model will check that there is only one whatever per city. the model will scream to the action and the actions will show the error is you have a error_message_for helper in the view.

Here’s the logic:

  1. A user will navigate to the “new” page
  2. A user will be presented with an input box
  3. The user enters a value
  4. A search will be performed using AJAX
  5. If a search comes up with nothing a transition will occur providing the user the ability to enter the rest of the information

It’s logically separate from actually creating the object.

Ah, I see now. That would certainly be a separate action, then. Depending on how your search is structured, it may be something like

/things/search?q=

or

/search?q=

Assuming the first, you’d define your action

oh i see, you want to check if a value was used? ok , you will have to add a custom restful action, for the search, then catch the click event , i recomend using jquery and respond with a template that has js.erb extantion to excute the code is quite simple maybe sustitude a partial or redirect.

That’s not what I’m quite trying to accomplish.

This might help:

If a user wants to create a Book in the system I would first ask them to perform a search on the title as part of the process to create a Book so the user can verify uniqueness. Now, there can be multiple different books with the same title. I just want the user to make sure that the exact same book does not exist before creating a duplicate.

Hopefully that makes more sense.

yea just saw it ,

put a button with a link to the search action put and id in it then with jquery

$(function() { $(“#the_button”).live(“click”, function() {
$.get(this.href, null, null, “script”); return false; }); });

make a js.erb file with

$(“#id_with_partial_to_replace”).html(“#{escape_javascript(render :partial =>“i_failed”)}”);

or

window.location = <%= you_fail_path %>

Ah, that will help with what I need to do next.

However, let’s continue with my Book analogy:

Here’s a sample of an object model:

Book (ActiveModel)

Title (ActiveModel)

Now, the user will be performing a search on the Title. However, this is to help facilitate the creation of a Book which contains a Title. Should the search logic be performed in the BookController, TitleController, TitleSearchController or something else?

I’m just trying to figure out the best way to structure a controller for this type of situation.

Thanks for your help and patience!

of coure in your search action you should to

respond_to do |format| if Book.find(params[:search])
format.js
end end

why there is a title model ? there is no need to , the title is a field of the books table, and you only need to add a new action to the BooksController (plural for controllers), to make the new action restful add this next to the map.resources :book , :member => {:make_up_a_name => :post}

then do rake routes and checkout the new restful route you have. It even has a helper method which you can use as the href for the button that triggers the ajax, the method is make_up_a_name_book_path in this example.

with plural for controllers i meant that you wrote BookController and is BooksController model names go plural in the controllers

Why would it be a member action? If he’s searching for a book, he doesn’t know yet what book he wants - it would be a collection action. I would also expect it to be a GET, not a POST, since he’s retrieving search results, not creating anything.

GET /books/search?title=

Adam Stegman

true is a :get not a :post( typo maybe i was thinking :get but wrote :post), but it is a member action because only one name will be returned, note that he is checking if the is more than one book with that name if that is the case he will not allow the creation so there should never be more than one and the action will only return one record

:get member action create singular methods and return one object :get collection actions create plural methods and return and array of objects

Let’s suppose a Book can have multiple Title’s based on language. So there can be an english title and a Spanish title for a single book. So the object model would look more like this:

Book (ActiveModel)

Titles (array of ActiveModels)

Solr will be used for search.

Logically, where should the search action be placed? A model/view doesn’t always have a 1-to-1 relationship. So putting a search action in the BooksController doesn’t seem logical to me. I guess I don’t completely understand the controller’s relationship between the model/controller. Does a controller lean more towards a view or model? If it has more of a bias towards a view then it seems like a TitlesSearchController makes more sense. If it’s more model based, then a TitlesController with a search action would make more sense.

A RESTful controller is based around a resource. In this case, you’re searching a resource to see if it has a record matching your query, so it makes sense to me to put it in /titles/search.

Adam Stegman

true is a :get not a :post( typo maybe i was thinking :get but wrote :post), but it is a member action because only one name will be returned, note that he is checking if the is more than one book with that name if that is the case he will not allow the creation so there should never be more than one and the action will only return one record

A member action acts on a single member. How can you act on an unknown member?

resources :titles do

member do

get :search

end

end

would create a URL like:

/titles/:id/search What would you suggest putting as the ID?

Instead, this is acting upon the /titles collection of resources, searching for something that exists within it. As such, the URL would be:

/titles/search

Adam Stegman

Okay, I think I’m getting caught up on the resources. I’ll look more into that for the time being.

Thanks a lot for you help!

yes is true, i missed that , i think i didnt think ahead, well where i live is pass midnight so that may be it , i think im going to sleep