Forum: Ruby on Rails create_for_*

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.
Rodrigo A. (Guest)
on 2006-02-23 18:21
Hi!

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?

Thanks,
Rodrigo.
Norman T. (Guest)
on 2006-02-24 13:54
(Received via mailing list)
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:

1. Have only one create action and try to decide what to do by examining
the coming in parameters.

2. Abstract your create logic and make a separate method from it. You
can then call this method from every action you need the logic.

3. 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.

4. 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.

What i would try to avoid, is calling one action from another, because
you cannot pass parameters to it.

I hope that helped.

--
Norman T.

http://blog.inlet-media.de
Rodrigo A. (Guest)
on 2006-02-25 14:14
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?

Exactly

>
> You have different choices to solve this:
>
> 1. 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.
>
> 2. 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.

>
> 3. 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.
>
> 4. 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!

Regards,
Rodrigo.
This topic is locked and can not be replied to.