i''m trying to print out user locations in my view. I''d like to just pass a user model to the view and print out the user location as user.location.city I can access user.location.city in the controller, but in the view user.location returns "#" Could someone explain what''s going on and maybe give an alternative solution?
user.location is returning an object, so when to_s is called you get the inspect string, like #<Location 34327832> user.location.city should print the string. Mike On Jan 23, 2009, at 4:36 PM, kevin lochner wrote:> i''m trying to print out user locations in my view. I''d like to just > pass a user model to the > view and print out the user location as user.location.city > > I can access user.location.city in the controller, but in the view > user.location returns "#" > > Could someone explain what''s going on and maybe give an alternative > solution? > > _______________________________________________ > Facebooker-talk mailing list > Facebooker-talk at rubyforge.org > http://rubyforge.org/mailman/listinfo/facebooker-talk-- Mike Mangino http://www.elevatedrails.com
I was wrong, it''s not just in the view, it''s a problem in the
controller as well.
This may be considered a bug - if the user location is empty, the
object returned by user.location is a string,
so calling user.location.city throws an exception.
In my controller I iterate over users with the following:
> friends.each do |f|
> logger.warn "class: #{f.current_location.class}"
> logger.warn "location: #{f.current_location}"
> if f.current_location
logger.warn "city: #{f.current_location.city} \n"
> end
> end
and I get the following output:
>class: Facebooker::Location
>location: #<Facebooker::Location:0x7fe614b68eb8>
>city:
>class: Facebooker::Location
>location: #<Facebooker::Location:0x7fe614b67b58>
>city: Ann Arbor
>class: String
>location:
> NoMethodError (undefined method `city'' for "":String):
and for verification i dumped the hash from which the user was
populated:
> nameJeff
SmithuidXXXXXlocaleen_USfirst_nameJeffcurrent_locationlast_nameSmith
On Jan 23, 2009, at 5:26 PM, Mike Mangino wrote:
> user.location is returning an object, so when to_s is called you get
> the inspect string, like #<Location 34327832>
>
> user.location.city should print the string.
>
> Mike
>
> On Jan 23, 2009, at 4:36 PM, kevin lochner wrote:
>
>> i''m trying to print out user locations in my view.
I''d like to
>> just pass a user model to the
>> view and print out the user location as user.location.city
>>
>> I can access user.location.city in the controller, but in the view
>> user.location returns "#"
>>
>> Could someone explain what''s going on and maybe give an
alternative
>> solution?
>>
>> _______________________________________________
>> Facebooker-talk mailing list
>> Facebooker-talk at rubyforge.org
>> http://rubyforge.org/mailman/listinfo/facebooker-talk
>
> --
> Mike Mangino
> http://www.elevatedrails.com
>
>
>
here''s the where the breakdown happens, from model.rb. You can see
that location gets
set to "" if it''s not a hash.
def hash_settable_writer(symbol, klass)
define_method("#{symbol}=") do |value|
instance_variable_set("@#{symbol}", value.kind_of?(Hash) ?
klass.from_hash(value) : value)
end
end
- kevin
On Jan 23, 2009, at 5:40 PM, kevin lochner wrote:
> I was wrong, it''s not just in the view, it''s a problem in
the
> controller as well.
>
> This may be considered a bug - if the user location is empty, the
> object returned by user.location is a string,
> so calling user.location.city throws an exception.
>
> In my controller I iterate over users with the following:
>
> > friends.each do |f|
> > logger.warn "class: #{f.current_location.class}"
> > logger.warn "location: #{f.current_location}"
> > if f.current_location
> logger.warn "city: #{f.current_location.city} \n"
> > end
> > end
>
> and I get the following output:
>
> >class: Facebooker::Location
> >location: #<Facebooker::Location:0x7fe614b68eb8>
> >city:
>
> >class: Facebooker::Location
> >location: #<Facebooker::Location:0x7fe614b67b58>
> >city: Ann Arbor
>
> >class: String
> >location:
> > NoMethodError (undefined method `city'' for
"":String):
>
> and for verification i dumped the hash from which the user was
> populated:
> > nameJeff
> SmithuidXXXXXlocaleen_USfirst_nameJeffcurrent_locationlast_nameSmith
>
>
> On Jan 23, 2009, at 5:26 PM, Mike Mangino wrote:
>
>> user.location is returning an object, so when to_s is called you
>> get the inspect string, like #<Location 34327832>
>>
>> user.location.city should print the string.
>>
>> Mike
>>
>> On Jan 23, 2009, at 4:36 PM, kevin lochner wrote:
>>
>>> i''m trying to print out user locations in my view.
I''d like to
>>> just pass a user model to the
>>> view and print out the user location as user.location.city
>>>
>>> I can access user.location.city in the controller, but in the view
>>> user.location returns "#"
>>>
>>> Could someone explain what''s going on and maybe give an
>>> alternative solution?
>>>
>>> _______________________________________________
>>> Facebooker-talk mailing list
>>> Facebooker-talk at rubyforge.org
>>> http://rubyforge.org/mailman/listinfo/facebooker-talk
>>
>> --
>> Mike Mangino
>> http://www.elevatedrails.com
>>
>>
>>
>
How about setting to nil if value isn''t a hash. The object is
hash_settable, and it doesn''t
seem to make sense to assign it an object of arbitrary class (string)
if it''s not given a hash
from which to populate:
def hash_settable_writer(symbol, klass)
define_method("#{symbol}=") do |value|
instance_variable_set("@#{symbol}", value.kind_of?(Hash) ?
klass.from_hash(value) : nil)
end
end
On Jan 23, 2009, at 5:52 PM, kevin lochner wrote:
> def hash_settable_writer(symbol, klass)
> define_method("#{symbol}=") do |value|
> instance_variable_set("@#{symbol}", value.kind_of?(Hash)
?
> klass.from_hash(value) : value)
> end
> end
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://rubyforge.org/pipermail/facebooker-talk/attachments/20090123/efd7218e/attachment.html>
I think that makes sense. I''ve added you as a committer to the project. Feel free to commit a patch with tests! Mike On Jan 23, 2009, at 6:06 PM, kevin lochner wrote:> How about setting to nil if value isn''t a hash. The object is > hash_settable, and it doesn''t > seem to make sense to assign it an object of arbitrary class > (string) if it''s not given a hash > from which to populate: > > def hash_settable_writer(symbol, klass) > define_method("#{symbol}=") do |value| > instance_variable_set("@#{symbol}", value.kind_of?(Hash) ? > klass.from_hash(value) : nil) > end > end > > On Jan 23, 2009, at 5:52 PM, kevin lochner wrote: > >> def hash_settable_writer(symbol, klass) >> define_method("#{symbol}=") do |value| >> instance_variable_set("@#{symbol}", value.kind_of?(Hash) ? >> klass.from_hash(value) : value) >> end >> end >-- Mike Mangino http://www.elevatedrails.com
thanks. There was one problem with that solution - it wouldn''t let
you assign already instantiated
objects to the member variables. I''m now looking at the following:
def hash_settable_writer(symbol, klass)
define_method("#{symbol}=") do |value|
if value.kind_of?(Hash)
instance_variable_set("@#{symbol}",klass.from_hash(value))
elsif value.kind_of?(klass)
instance_variable_set("@#{symbol}", value)
end
end
end
should be able to get a patch & tests out this weekend.
- kevin
On Jan 23, 2009, at 6:33 PM, Mike Mangino wrote:
> I think that makes sense. I''ve added you as a committer to the
> project. Feel free to commit a patch with tests!
>
> Mike
>
> On Jan 23, 2009, at 6:06 PM, kevin lochner wrote:
>
>> How about setting to nil if value isn''t a hash. The object is
>> hash_settable, and it doesn''t
>> seem to make sense to assign it an object of arbitrary class
>> (string) if it''s not given a hash
>> from which to populate:
>>
>> def hash_settable_writer(symbol, klass)
>> define_method("#{symbol}=") do |value|
>> instance_variable_set("@#{symbol}",
value.kind_of?(Hash) ?
>> klass.from_hash(value) : nil)
>> end
>> end
>>
>> On Jan 23, 2009, at 5:52 PM, kevin lochner wrote:
>>
>>> def hash_settable_writer(symbol, klass)
>>> define_method("#{symbol}=") do |value|
>>> instance_variable_set("@#{symbol}",
value.kind_of?(Hash) ?
>>> klass.from_hash(value) : value)
>>> end
>>> end
>>
>
> --
> Mike Mangino
> http://www.elevatedrails.com
>
>
>