Topic: ugly controller

Hi, my main controller is getting very ugly, not at all dry. In trying to clean it up a bit, im unclear on the rules/best practices of controller, so here are some questions?

- can I call one method in a controller from another? (if so are there any limitations)  (im looking for the eqivalent of java servlet forward)
(I know I can redirect_to, but I dont like this because it causes an extra round trip to the browser, right??)

- if I cant do above, how can I get rid of repeating code in the following situation:

- Anytime some action (controller call) happens on my page, I need to,

1.) do the work for the action

2.) update the page, which includes:
a.) figure out what it is that changed
b.) figure out which div's are affected
c.) get the data relavent to each affected div
d.) render those divs

Each div can be fairly complex and include paging (state)

thanks
Joel

Re: ugly controller

jnylund wrote:

- can I call one method in a controller from another? (if so are there any limitations)  (im looking for the eqivalent of java servlet forward)

AFAIK, there's no easy way to do this. I sometimes wish this were possible too, but it does add some complications (the generation of the params hash for example) so it's probably a good thing it's not possible...

jnylund wrote:

(I know I can redirect_to, but I dont like this because it causes an extra round trip to the browser, right??)

Right.

jnylund wrote:

- if I cant do above, how can I get rid of repeating code in the following situation...

It's hard to say without seeing the code. There's a lot of different techniques for removing duplication: before filters, shared method, helper method and partials. If you want to post the code I can give you more details on the best solution.

Railscasts - Free Ruby on Rails Screencasts

Re: ugly controller

Hi, im a little further along on this, the simplest thing for me was to move most of the code to the model, but if I have some code that is just controller stuff, like determining and getting all the variables for the view, pagination etc, can I just create another method in the same controller and mark it protected (that way it cant get invoked from client right?) then I can also refactor common code into it?

are there any rules for calling another method on same controller? (just dont do after rendering has begun?)

Re: ugly controller

If you're in a controller, you should be able to call any other method in that controller without problems. The view doesn't have access to the controller methods by default, but if you're just setting instance variables to prepare for the view, it should work fine.

Railscasts - Free Ruby on Rails Screencasts