Forum: Ruby on Rails Thoughts on controllers

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Elliott G. (Guest)
on 2008-12-17 04:22
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.
John Y. (Guest)
on 2008-12-17 05:22
(Received via mailing list)
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>
Hafeez S. (Guest)
on 2008-12-17 06:50
(Received via mailing list)
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
Elliott G. (Guest)
on 2008-12-17 08:45
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!
Ryan B. (Guest)
on 2008-12-17 09:10
(Received via mailing list)
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 :)
David A. Black (Guest)
on 2008-12-17 12:24
(Received via mailing list)
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
Coming in 2009: The Well-Grounded Rubyist (http://manning.com/black2)
This topic is locked and can not be replied to.