Norman T. wrote:
Am Donnerstag, den 23.02.2006, 17:21 +0100 schrieb Rodrigo A.:
If I have a /budget/create action, how should I make a
/budget/create_for_customer without repeating myself?
I could create a “create_for_customer” action and call “create” from
there, but stripping the customer select_tag somehow, since customer_id
will be passed from /customer/show.
How do you do the create_for_something stuff?
Just to understand you right: The code in your create action looks very
similar to the code in your create_for_customer action, right?
You have different choices to solve this:
- Have only one create action and try to decide what to do by examining
the coming in parameters.
Yes, that was one of the approach, to keep it DRY.
- Abstract your create logic and make a separate method from it. You
can then call this method from every action you need the logic.
Um, that’s interesting.
- Why not have two or more different actions, that look similar?
Perhaps you are going to make one more complex in the future and an
abstraction is not practicable anymore.
Yes, it’s not a bad idea at all, but as said above, i would like to
repeat the less possible.
- Sometime it makes sense to have a data load method called from
different actions, to ensure some necessary instance variables are
available, like for select tags, that appear in different views.
Yes, i alway use that. I make a private method called populate_form
What i would try to avoid, is calling one action from another, because
you cannot pass parameters to it.
Now i think that the best approach is to have more than one action:
create, create_for_customer, but try to “extract” common code to a
private create_budget method, something like a helper.
I hope that helped.
Of course, thanks!