On Viernes, 7 de Mayo de 2010 11:57:56 Iñaki Baz Castillo escribió:
> Hi, Client has_many Trunks. In TrunkController I set:
>
>   def before_create_save(trunk)
>     pre_save(trunk)
>   end
>
>   def before_update_save(trunk)
>     pre_save(trunk)
>   end
>
>   def pre_save(trunk)
>     Utils.strip(trunk.ip_addr)
>   end
>   private :pre_save
>
>
> Utils.strip(trunk.ip_addr) is a method defined by me which does
> "strip!" in case the argument is a string.
>
> When I create/update a Trunk using TrunkController the "ip_addr"
> string is stripped correctly, but when I create such Trunk using a
> subform of a Client, "ip_addr" is not stripped, this is,
> before_create_save and before_update_save() are not executed for
> subforms. Do I miss something? I strongly think such functions should
> be executed also for subforms.

When you fill a subform, you are loading the parent controller, so methods from 
another controller can't be used. Anyway before_create_save and 
before_update_save should be used only when you need something from session, 
in other cases is better to use ActiveRecord's callbacks. In this case you 
can:

1. To use :before_save in Trunk model to strip ip_addr:
before_save :strip_ip_addr
def strip_ip_addr
  Utils.strip(self.ip_addr)
end

2. Override ip_addr= method in Trunk model:
def ip_addr=(value)
  self[:ip_addr] = Util.strip(value)
end

I prefer second way.

-- 
Sergio Cambra .:: entreCables S.L. ::.
Mariana Pineda 23, 50.018 Zaragoza
T) 902 021 404 F) 976 52 98 07 E) [email protected]

-- 
You received this message because you are subscribed to the Google Groups 
"ActiveScaffold : Ruby on Rails plugin" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/activescaffold?hl=en.

Reply via email to