John Indra
2006-Jul-16 08:21 UTC
[Rails] Binding text_field et al to object.attribute.name
Hi all, Suppose I have a PurchaseItem model which refers to Product model... class Product < ActiveRecord::Base has_many :purchase_items end class PurchaseItem < ActiveRecord::Base belongs_to :product end To keep things simple, one of the attribute of the Product model is title. On the interface, I have a textfield that I need to bind with @purchase_item.product.title. text_field ''purchase_item'', ''product.title'' doesn''t work for me. How to achieve this using Rails 1.1.4? Does this feature exist by default in Edge? Oh and one more thing, will it matter if the attribute is an ActiveRecord object or a value object like Money? Thank you very much. Regards, John.
Mark Reginald James
2006-Jul-17 05:10 UTC
[Rails] Re: Binding text_field et al to object.attribute.name
John Indra wrote:> Suppose I have a PurchaseItem model which refers to Product model... > > class Product < ActiveRecord::Base > has_many :purchase_items > end > > class PurchaseItem < ActiveRecord::Base > belongs_to :product > end > > To keep things simple, one of the attribute of the Product model is title. > > On the interface, I have a textfield that I need to bind with > @purchase_item.product.title. > > text_field ''purchase_item'', ''product.title'' doesn''t work for me. > > How to achieve this using Rails 1.1.4? Does this feature exist by > default in Edge?Add to the PurchaseItem model methods: def product_title product.title end def product_title= (t) product.title = t end Then you can use text_field ''purchase_item'', ''product_title'' -- We develop, watch us RoR, in numbers too big to ignore.
John Indra
2006-Jul-18 05:26 UTC
[Rails] Re: Binding text_field et al to object.attribute.name
On Jul 17, 2006, at 12:09 PM, Mark Reginald James wrote:> Add to the PurchaseItem model methods: > > def product_title > product.title > end > > def product_title= (t) > product.title = t > end > > Then you can use > > text_field ''purchase_item'', ''product_title''Of course I can do that. I was doing exactly that way before I made my post. However, I feel that it''s kind of a messy hack. Are you sure there is no ''better'' solution? Thanks anyway Best regards, John