Ruby on Rails Tuesday, June 1, 2010

On 1 June 2010 16:20, Matt Royer <lists@ruby-forum.com> wrote:
> Colin Law wrote:
>> Rather than attempt to add a method to the String class, which is what
>> you seem to be suggesting, You could add a method to your model that
>> returns the id and name as a formatted string.  So in order.rb
>> something like
>> def order_code
>>   "#{'%05d' % id} #{name}"
>> end
>> Then in the view just use
>> <%= @order.order_code %>
>
> Hi Colin,
>
> Would this work if I wanted to put the leading zeros elsewhere in my app
> (other views tied to other models and controllers)? Would I just
> reference the Order model and then do this?

No, that an instance method of Order so will only work on Order
objects. It will work in any view (associated with any controller)
but only if you have an Order object.

Colin

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

No comments:

Post a Comment