I sometimes buy myself a default value for a hash using something like this: h = Hash.new{|h,k| h[k] = ""}. I can now pass ''h'' any key and if a value has not yet been associated with that key I receive an empty string. I would love to do something similar in my partials so that I wouldn''t have to worry about passing in every variable that is referenced. For example, if I have a partial ''_foo.rhtml'' as follows: <%= favorite_food %> <%= favorite_color %> ... and I attempt to render it with a call of: <%= render(:partial => ''foo'', :locals => {:favorite_food => "Anything but natto."}) %> ... then I''ll be scolded since I didn''t provide a value for ''favorite_color''. I''d rather just give all unassigned variables a default value as described in the first paragraph. Anyone know of a way this can be done? -- Posted via http://www.ruby-forum.com/.
Don Walker wrote:> I would love to do something similar in my partials so that I wouldn''t > have to worry about passing in every variable that is referenced.<snip>> Anyone know of a way this can be done? >The first thing that springs to mind is a quick <% food_color = '''' if food_color.nil? -%> at the top. There may be a slicker way, but if there is it escapes me... -- Alex
Alex Young wrote:> The first thing that springs to mind is a quick > > <% food_color = '''' if food_color.nil? -%> > > at the top. There may be a slicker way, but if there is it escapes > me...Thanks, but this won''t work because ''food_color'' isn''t defined to call ''nil?'' upon. I could do what I think you intended with: <% favorite_food = '''' unless defined?(favorite_food) %> ... but as you noted it is definitely NOT slick :-) -- Posted via http://www.ruby-forum.com/.
Kevin Olbrich
2006-Jan-17 18:14 UTC
[Rails] Re: Hash.new{|h,k| h[k] = ""} ... but for views?
Don Walker wrote:> Alex Young wrote: >> The first thing that springs to mind is a quick >> >> <% food_color = '''' if food_color.nil? -%> >> >> at the top. There may be a slicker way, but if there is it escapes >> me... > > Thanks, but this won''t work because ''food_color'' isn''t defined to call > ''nil?'' upon. I could do what I think you intended with: > > <% favorite_food = '''' unless defined?(favorite_food) %> > > ... but as you noted it is definitely NOT slick :-)Couple of things here... a = Hash.new("") should return an empty string if a undefined key is used. I suppose you could set up an before filter for each of your view methods that sets default values for each of your instance variables. _Kevin -- Posted via http://www.ruby-forum.com/.
> > I would love to do something similar in my partials so that I wouldn''t > have to worry about passing in every variable that is referenced.class Foo < ActiveRecord::Base alias_method :old_favorite_food, :favorite_food def favorite_food self.old_favorite_food ||= "pizza" end end The above redefines the getter so it will never return nil. -Brian Buckley -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060117/cab9b209/attachment.html
Don Walker wrote:> Alex Young wrote: >> The first thing that springs to mind is a quick >> >> <% food_color = '''' if food_color.nil? -%> >> >> at the top. There may be a slicker way, but if there is it escapes >> me... > > Thanks, but this won''t work because ''food_color'' isn''t defined to call > ''nil?'' upon. I could do what I think you intended with: > > <% favorite_food = '''' unless defined?(favorite_food) %> > > ... but as you noted it is definitely NOT slick :-)Oops - yes, that''s what I was after :-) But a bit slicker is (and you can actually do this where you use the <%= %> tag, so the default is actually defined where you need it): <%= food_color ||= ''blue'' %> That''s a little smarter. -- Alex