ruby on rails - Best practice for displaying data the user might not have -
I got Ruby on Rail App where I do the following:
@ User = User.find (: first,: conditions => ['lower (user name) =' ', current_ subdomain. Downcount] ,: = include:> bio)
: Included =>: Bio 'is the important part.
Then in the scene, I want to display some bio:
& Lt;% = @user .bio.title% & gt; & Lt; / Em> & Lt; Br / & gt; & Lt;% = @ user.bio.city% & gt; & Lt; Br / & gt; & Lt;% = @ user.bio.state% & gt; & Lt; Br / & gt; & Lt;% = @ user.bio.country% & gt;
But if the user does not have any information then it will replace some blank lines.
I have tried some different things, no one has worked so far ...
& lt;% if @ user.bio.title% & Gt; # Is always correct & lt;% if @ user.bio.title & gt; 0% & gt; # Triggers an error if @ user.bio.title is not set & lt;% as long as @ user.bio.title.empty? & Gt%;
Like the above
Any solution to display data to the user?
Thanks in advance!
Update
OK, if there are some things to locate:
& lt;% if @ user .bio.title% & gt; # Works if bio is not set at all. & Lt;% if @ user.bio.title & gt; 0% & gt; # Works if bio set.
So I can use a sollution that looks:
& lt;% if @ user.bio.title% & gt; & Lt;% If the user is .bio.title & gt; '0'% & gt; & Lt;% = @ user.bio.title% & gt; & Lt;% end% & gt; & Lt;% end% & gt;
But does it seam a bit more? Any better suggestions?
Thank you!
One way to think about this is by the user has a bio and in the title field Is full. In Ruby:
& lt;% if @ user.bio & amp; Amp; !@user.bio.blank? & Gt%;
If you are going to display multiple fields of bio, then you can separate it in 2 checks eg.
& lt;% if @ user.bio% & gt; & Lt;% as long as @ user.bio.title.blank? & Gt%; Title: & lt;% = @ user.bio.title% & gt; & Lt;% end% & gt; & Lt;% as long as @ user.bio.other_field.blank? & Gt%; Other fields: & lt;% = @ user.bio.other_field% & gt; & Lt;% end% & gt; & Lt;% end% & gt;
Optional
Another method is to put methods on your model to provide direct access to bio areas. For example
# Return the user's title or zero if a bio-DRT title is bio.title if bio end
then you just do this Can:
& lt;% as long as user.title.blank? & Gt%; Title: & lt;% = @ user.title% & gt; & Lt;% end% & gt;
Comments
Post a Comment