# Thoughts on controllers

**URL:** <https://www.ruby-forum.com/t/thoughts-on-controllers/155809>\
**Category:** Rails\
**Created:** [December 17, 2008, 3:22am UTC](https://www.ruby-forum.com/t/thoughts-on-controllers/155809 "2008-12-17T03:22:04Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![elliottg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b77776/32.png) [@elliottg](https://www.ruby-forum.com/u/elliottg)\
**Post date:** [December 17, 2008, 3:22am UTC](https://www.ruby-forum.com/t/thoughts-on-controllers/155809/1 "2008-12-17T03:22:04Z")

</div>

I am new to Rails. I have read a couple books and ran through a few  
tutorials as well. I have been using scaffolding a bit to structure a  
few projects that I am building for learning purposes. I am curious what  
your take on adding additional actions to the default RESTful actions in  
the generated controllers is.

Is it best to somehow only use the generated controller actions? Is this  
even feasible, depending on what your app is trying to accomplish? Or is  
it fairly common to add numerous custom actions that either expand on  
the default seven or add completely new functionality to the controller?

Thanks for your input.

---

<div class="post-metadata">

**Author:** ![elliottg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b77776/32.png) [@elliottg](https://www.ruby-forum.com/u/elliottg)\
**Post date:** [December 17, 2008, 4:22am UTC](https://www.ruby-forum.com/t/thoughts-on-controllers/155809/2 "2008-12-17T04:22:34Z")

</div>

Personally, I try to keeping my applications restful. It may require  
me to rethink how I am going to accomplish a task, but I find it  
usually forces the most elegant solution in the end.

That’s not so say I haven’t added extra actions to my controllers, but  
they really shouldn’t have too much clutter in them

My $0.02.

On Dec 16, 8:22 pm, Elliott G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![elliottg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b77776/32.png) [@elliottg](https://www.ruby-forum.com/u/elliottg)\
**Post date:** [December 17, 2008, 7:45am UTC](https://www.ruby-forum.com/t/thoughts-on-controllers/155809/3 "2008-12-17T07:45:03Z")

</div>

Thanks for the the thoughts on this so far.

Here’s a further question…

With Rails, I will primarily be building dynamic sites powered by my own  
custom Rails CMS, which I will build in the near future. Also I don’t  
plan on these sites or CMS being accessed as services. With this in  
mind, should I even build these using the REST architecture at all, and  
if so what would the benefits be?

Thanks heaps!

---

<div class="post-metadata">

**Author:** ![elliottg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b77776/32.png) [@elliottg](https://www.ruby-forum.com/u/elliottg)\
**Post date:** [December 17, 2008, 5:50am UTC](https://www.ruby-forum.com/t/thoughts-on-controllers/155809/4 "2008-12-17T05:50:48Z")

</div>

Hi Elliott G…,

If you want to use Restful you can ,And even if you want to add more  
actions  
you can but they need to be defined as those seven methods only…,

if the controller is small then better to do with out RESTful …

Enjoy and Gud luck

Regards  
hafeez

---

<div class="post-metadata">

**Author:** ![elliottg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b77776/32.png) [@elliottg](https://www.ruby-forum.com/u/elliottg)\
**Post date:** [December 17, 2008, 11:24am UTC](https://www.ruby-forum.com/t/thoughts-on-controllers/155809/5 "2008-12-17T11:24:09Z")

</div>

Hi –

On Tue, 16 Dec 2008, John Y. wrote:

> Personally, I try to keeping my applications restful. It may require  
> me to rethink how I am going to accomplish a task, but I find it  
> usually forces the most elegant solution in the end.
> 
> That’s not so say I haven’t added extra actions to my controllers, but  
> they really shouldn’t have too much clutter in them

Keep in mind, though, that the resource routing is designed with hooks  
to let you create extra methods, and they’re just as RESTful as the  
seven canonical methods. REST per se is method-name neutral.

Still, I think it’s good to be conservative about adding extra  
actions. Whenever I find myself writing a new-named method in a  
controller, I try to think whether I’m actually creating/updating/  
etc. something. For example, instead of having a borrow\_book action in  
a users controller, I would probably favor a create action in a loans  
controller, simply because it converges more predictably on the  
CRUD-related semantics of the resource routing.

David

–  
David A. Black / Ruby Power and Light, LLC  
Ruby/Rails consulting & training: [http://www.rubypal.com](http://www.rubypal.com)  
Coming in 2009: The Well-Grounded Rubyist ([The Well-Grounded Rubyist](http://manning.com/black2))

---

<div class="post-metadata">

**Author:** ![elliottg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b77776/32.png) [@elliottg](https://www.ruby-forum.com/u/elliottg)\
**Post date:** [December 17, 2008, 8:10am UTC](https://www.ruby-forum.com/t/thoughts-on-controllers/155809/6 "2008-12-17T08:10:00Z")

</div>

Take a forum system, hypothetically. You have topics and posts  
controllers which both contain the 7 default RESTful actions. Then you  
realise “Oh, I want to add in a functionality that allows me to split  
topics based on the positioning of certain posts”. Ah! A conundrum! So  
what do you do and where do you put it?

Well, since you’re going to be splitting a _topic_ based on _certain  
posts_ I would put this functionality not in the topics controller,  
but the posts controller. The reason for this is because most likely  
we’re going to want to split a topic based on a particular post. How  
we split it isn’t really important right now. So we create a new  
action called split in our posts controller which has two ways of  
operating, first it will prompt a user how to split a post and then  
it’ll undertake the task. So firstly we add it to our routes.rb file:

map.resources :posts, :member =\> { :split =\> [:get, :post] }

Notice how we’re specifying the :member option here, signifying we  
only want a single post and then after that specifying an array of  
methods that our action will respond to.

When we click the link to go to this split we’ll assume that the user  
gets prompted with a form on how they want to split the post (like, do  
they want to split the topic with all the posts after this post,  
before this post and if the new topic should include the post that is  
our candidate) and then when they submit this form it undertakes the  
actual splitting.

Now that’s (what I’d like to think of as a) good example of adding a  
“RESTful” action that’s not one of “The Seven” to your controller.

A bad example would be going into the topics controller and adding  
actions such as new\_post, create\_post, edit\_post, etc. because post is  
its own resource. The creation, listing, showing, editing and  
destroying of separate resources should be kept separate.

Just a short description 🙂
