Hi all, In my application all users have the ability to upload a profile picture through a file field but it is not compulsory. On my index page it displays all users along with their profile pictures but not all users have a profile picture and therefore some users have no image at all. How would I go about getting a generic image to display for the rest of the users. I have the following section in the index: <% for user in @users %> <tbody> <td align = "center"><%= image_tag user.photo.url, :height => 50, :width => 50 %></td> <td align = "center"><%= user.username %></td> <td align = "center"><%= user.favourite_console %></td> <td align = "center"><%= user.games.count %></td> <td align = "center"><%= link_to "Show", user %></body> </tbody> <% end %> For the following line I want to do something like this: If user has photo <td align = "center"><%= image_tag user.photo.url, :height => 50, :width => 50 %></td> else display this photo end How would I go about doing this? Thanks -- Posted via http://www.ruby-forum.com/. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 28 February 2012 16:58, Christopher Jones <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Hi all, > > In my application all users have the ability to upload a profile picture > through a file field but it is not compulsory. > > On my index page it displays all users along with their profile pictures > but not all users have a profile picture and therefore some users have > no image at all. > > How would I go about getting a generic image to display for the rest of > the users.Add a method to model User called image_url (for example) that returns the actual photo url if there is one or the url of the default image if there is not.> > I have the following section in the index: > > <% for user in @users %> > <tbody> > <td align = "center"><%= image_tag user.photo.url, :height => 50, > :width => 50 %></td>Then here use image_tag user.image_url Always move things down to the model if possible. This has the additional advantage that you can test it in the model unit test which is much easier than testing it in the view. 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org 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.
On Feb 28, 2012, at 11:58 AM, Christopher Jones wrote:> Hi all, > > In my application all users have the ability to upload a profile picture > through a file field but it is not compulsory. > > On my index page it displays all users along with their profile pictures > but not all users have a profile picture and therefore some users have > no image at all. > > How would I go about getting a generic image to display for the rest of > the users. > > I have the following section in the index: > > <% for user in @users %> > <tbody> > <td align = "center"><%= image_tag user.photo.url, :height => 50, > :width => 50 %></td> > <td align = "center"><%= user.username %></td> > <td align = "center"><%= user.favourite_console %></td> > <td align = "center"><%= user.games.count %></td> > <td align = "center"><%= link_to "Show", user %></body> > </tbody> > <% end %> > > For the following line I want to do something like this: > > If user has photo<%- if user.photo.url? -%>> <td align = "center"><%= image_tag user.photo.url, :height => > 50, :width => 50 %></td> > else > display this photo<%= image_tag ''generic_avatar.png'' %>> end > > How would I go about doing this?Ideally, you would extract this into a helper method, since you don''t want to be repeating all that conditional logic all over the place. Then you could simply call the ''avatar'' method: #users_helper.rb def avatar(user) if user.photo.url? raw image_tag(user.photo.url) else raw image_tag(''generic_avatar.png'') end end Untested, but it ought to work. Walter> > Thanks > > -- > Posted via http://www.ruby-forum.com/. > > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. >-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Walter Davis wrote in post #1049293:> On Feb 28, 2012, at 11:58 AM, Christopher Jones wrote: > >> the users. >> <td align = "center"><%= link_to "Show", user %></body> >> </tbody> >> <% end %> >> >> For the following line I want to do something like this: >> >> If user has photo > > <%- if user.photo.url? -%> > >> <td align = "center"><%= image_tag user.photo.url, :height => >> 50, :width => 50 %></td> >> else >> display this photo > > <%= image_tag ''generic_avatar.png'' %> > >> end >> >> How would I go about doing this? > > Ideally, you would extract this into a helper method, since you don''t > want to be repeating all that conditional logic all over the place. Then > you could simply call the ''avatar'' method: > > #users_helper.rb > > def avatar(user) > if user.photo.url? > raw image_tag(user.photo.url) > else > raw image_tag(''generic_avatar.png'') > end > end > > Untested, but it ought to work. > > WalterHey Walter, I placed the above in my user helper and my application helper but no luck> It doesn''t change anything. I even changed the above to def photo(user) I tried changing my index.html bit from what it is to image_tag photo.url(user) but that caused an error. I am not sure now, I tried using the old code I had for gravatar but that doesn''t do anything. Thanks Christopher Jones -- Posted via http://www.ruby-forum.com/. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 29 February 2012 13:47, Christopher Jones <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Walter Davis wrote in post #1049293: >> On Feb 28, 2012, at 11:58 AM, Christopher Jones wrote: >> >>> the users. >>> <td align = "center"><%= link_to "Show", user %></body> >>> </tbody> >>> <% end %> >>> >>> For the following line I want to do something like this: >>> >>> If user has photo >> >> <%- if user.photo.url? -%> >> >>> <td align = "center"><%= image_tag user.photo.url, :height => >>> 50, :width => 50 %></td> >>> else >>> display this photo >> >> <%= image_tag ''generic_avatar.png'' %> >> >>> end >>> >>> How would I go about doing this? >> >> Ideally, you would extract this into a helper method, since you don''t >> want to be repeating all that conditional logic all over the place. Then >> you could simply call the ''avatar'' method: >> >> #users_helper.rb >> >> def avatar(user) >> if user.photo.url? >> raw image_tag(user.photo.url) >> else >> raw image_tag(''generic_avatar.png'') >> end >> end >> >> Untested, but it ought to work. >> >> Walter > > Hey Walter, I placed the above in my user helper and my application > helper but no luck> It doesn''t change anything. I even changed the above > to def photo(user) > > I tried changing my index.html bit from what it is to image_tag > photo.url(user) but that caused an error. > > I am not sure now, I tried using the old code I had for gravatar but > that doesn''t do anything.I think my suggestion was better. If you can put something in the model it is generally better than in a helper. But to use Walter''s method, did you change the code in the view to use the helper method? 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org 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.
On Feb 29, 2012, at 8:47 AM, Christopher Jones wrote:> Walter Davis wrote in post #1049293: >> On Feb 28, 2012, at 11:58 AM, Christopher Jones wrote: >> >>> the users. >>> <td align = "center"><%= link_to "Show", user %></body> >>> </tbody> >>> <% end %> >>> >>> For the following line I want to do something like this: >>> >>> If user has photo >> >> <%- if user.photo.url? -%> >> >>> <td align = "center"><%= image_tag user.photo.url, :height => >>> 50, :width => 50 %></td> >>> else >>> display this photo >> >> <%= image_tag ''generic_avatar.png'' %> >> >>> end >>> >>> How would I go about doing this? >> >> Ideally, you would extract this into a helper method, since you don''t >> want to be repeating all that conditional logic all over the place. Then >> you could simply call the ''avatar'' method: >> >> #users_helper.rb >> >> def avatar(user) >> if user.photo.url? >> raw image_tag(user.photo.url) >> else >> raw image_tag(''generic_avatar.png'') >> end >> end >> >> Untested, but it ought to work. >> >> Walter > > Hey Walter, I placed the above in my user helper and my application > helper but no luck> It doesn''t change anything. I even changed the above > to def photo(user) > > I tried changing my index.html bit from what it is to image_tag > photo.url(user) but that caused an error.IF you created the helper method I posted earlier, you would want to replace your entire image tag code in your index.html with <%= avatar(user) %> But I like Colin''s idea even better, since it separates concerns more neatly. #models/user.rb ... def user_photo (photo.url?) ? photo.url : ''default.png'' end And then in index.html.erb: <%= image_tag user.user_photo %> Much cleaner that way, since you don''t have to look in the helper for the HTML or the accessor logic, rather you look for HTML in the view and logic in the model, as Rails intends. Walter> > I am not sure now, I tried using the old code I had for gravatar but > that doesn''t do anything. > > Thanks > Christopher Jones > > -- > Posted via http://www.ruby-forum.com/. > > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. >-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
> #models/user.rb > ... > def user_photo > (photo.url?) ? photo.url : ''default.png'' > end > > And then in index.html.erb: > > <%= image_tag user.user_photo %>Hi, I done the above but I get the following error: undefined method `url?'' for /photos/original/missing.png:Paperclip::Attachment Any ideas why this? must I change it to photo.url.present? Thanks -- Posted via http://www.ruby-forum.com/. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Feb 29, 2012, at 9:20 AM, Christopher Jones wrote:>> #models/user.rb >> ... >> def user_photo >> (photo.url?) ? photo.url : ''default.png'' >> end >> >> And then in index.html.erb: >> >> <%= image_tag user.user_photo %> > > Hi, > > I done the above but I get the following error: > > undefined method `url?'' for > /photos/original/missing.png:Paperclip::Attachment > > Any ideas why this? must I change it to photo.url.present?Yes, or maybe photo.url.blank? or just photo.url (without the question mark) would work. Walter> > Thanks > > -- > Posted via http://www.ruby-forum.com/. > > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. >-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Walter Davis wrote in post #1049458:> On Feb 29, 2012, at 9:20 AM, Christopher Jones wrote: > >> Hi, >> >> I done the above but I get the following error: >> >> undefined method `url?'' for >> /photos/original/missing.png:Paperclip::Attachment >> >> Any ideas why this? must I change it to photo.url.present? > > Yes, or maybe photo.url.blank? or just photo.url (without the question > mark) would work. > > WalterHey Walter. If I put the line: (photo.url.blank?) ? photo.url : ''images/guest.png'' They all display the guest image, even those users who have a display picture. If I put the line: (photo.url.present?) ? photo.url : ''/images/guest.png'' The users who have images have their photos displayed but those who don''t get a blank image. Any ideas? -- Posted via http://www.ruby-forum.com/. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Feb 29, 2012, at 9:38 AM, Christopher Jones wrote:> Walter Davis wrote in post #1049458: >> On Feb 29, 2012, at 9:20 AM, Christopher Jones wrote: >> >>> Hi, >>> >>> I done the above but I get the following error: >>> >>> undefined method `url?'' for >>> /photos/original/missing.png:Paperclip::Attachment >>> >>> Any ideas why this? must I change it to photo.url.present? >> >> Yes, or maybe photo.url.blank? or just photo.url (without the question >> mark) would work. >> >> Walter > > Hey Walter. > > If I put the line: > > (photo.url.blank?) ? photo.url : ''images/guest.png'' > > They all display the guest image, even those users who have a display > picture. > > If I put the line: > > (photo.url.present?) ? photo.url : ''/images/guest.png'' > > The users who have images have their photos displayed but those who > don''t get a blank image. > > Any ideas?Unless you have a sub-folder named images inside your images folder, you need to just pass guest.png to the image_tag helper (it already assumes that your pictures are in the images folder). Walter -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
I have had no luck :( I have got the following in my model def user_photo (photo.url.present?) ? photo.url : ''guest.png'' #tried with just photo.url end and the following my in index <%= image_tag user.user_photo, :height => 50, :width => 50 %> I don''t know what is stopping it from displaying the guest.png because if I changed the line to photo.url.blank? then it will display the guest.png for every user but with the present or simply photo.url line it just doesn''t display the guest.png for any of them. Thanks Christopher Jones -- Posted via http://www.ruby-forum.com/. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Feb 29, 2012, at 10:04 AM, Christopher Jones wrote:> I have had no luck :( > > I have got the following in my model > > def user_photo > (photo.url.present?) ? photo.url : ''guest.png'' #tried with just > photo.url > end > > and the following my in index > > <%= image_tag user.user_photo, :height => 50, :width => 50 %> > > I don''t know what is stopping it from displaying the guest.png because > if I changed the line to photo.url.blank? then it will display the > guest.png for every user but with the present or simply photo.url line > it just doesn''t display the guest.png for any of them.Could you put just this in your view (replacing your current image tag): <%= user.user_photo %> And see what prints out in the browser. My guess is that it will be something, but I don''t know what that is. Also, are you seeing any errors in the console when you test this locally? Walter> > Thanks > Christopher Jones > > -- > Posted via http://www.ruby-forum.com/. > > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. >-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 29 February 2012 15:04, Christopher Jones <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> I have had no luck :( > > I have got the following in my model > > def user_photo > (photo.url.present?) ? photo.url : ''guest.png'' #tried with just > photo.url > end > > and the following my in index > > <%= image_tag user.user_photo, :height => 50, :width => 50 %> > > I don''t know what is stopping it from displaying the guest.png because > if I changed the line to photo.url.blank? then it will display the > guest.png for every user but with the present or simply photo.url line > it just doesn''t display the guest.png for any of them.Use rails console to work out what is going on. In the console ''find'' a record with no url and then you can call your method and see what it returns, also you can call photo.url.present? and so on to see what they give. 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org 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.
Walter Davis wrote in post #1049473:> On Feb 29, 2012, at 10:04 AM, Christopher Jones wrote: > >> >> <%= image_tag user.user_photo, :height => 50, :width => 50 %> >> >> I don''t know what is stopping it from displaying the guest.png because >> if I changed the line to photo.url.blank? then it will display the >> guest.png for every user but with the present or simply photo.url line >> it just doesn''t display the guest.png for any of them. > > Could you put just this in your view (replacing your current image tag): > > <%= user.user_photo %> > > And see what prints out in the browser. My guess is that it will be > something, but I don''t know what that is. Also, are you seeing any > errors in the console when you test this locally? > > WalterHey Walter, good shout. I just checked my console and I see this for the images section for those users who don''t have an image: Started GET "/photos/original/missing.png" for 127.0.0.1 at 2012-02-29 15:29:16 +0000 ActionController::RoutingError (No route matches [GET] "/photos/original/missing.png"): I don''t know where it is getting that line from. Thanks -- Posted via http://www.ruby-forum.com/. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Feb 29, 2012, at 10:31 AM, Christopher Jones wrote:> Walter Davis wrote in post #1049473: >> On Feb 29, 2012, at 10:04 AM, Christopher Jones wrote: >> >>> >>> <%= image_tag user.user_photo, :height => 50, :width => 50 %> >>> >>> I don''t know what is stopping it from displaying the guest.png because >>> if I changed the line to photo.url.blank? then it will display the >>> guest.png for every user but with the present or simply photo.url line >>> it just doesn''t display the guest.png for any of them. >> >> Could you put just this in your view (replacing your current image tag): >> >> <%= user.user_photo %> >> >> And see what prints out in the browser. My guess is that it will be >> something, but I don''t know what that is. Also, are you seeing any >> errors in the console when you test this locally? >> >> Walter > > Hey Walter, good shout. > > I just checked my console and I see this for the images section for > those users who don''t have an image: > > > Started GET "/photos/original/missing.png" for 127.0.0.1 at 2012-02-29 > 15:29:16 +0000 > > ActionController::RoutingError (No route matches [GET] > "/photos/original/missing.png"): > > I don''t know where it is getting that line from.Aha! That is coming from deep in the heart of Paperclip. You have two options. First, put your missing icon image at that path in your application. Second, choose a different method to test on your model. Instead of using the image accessor, look for one of the actual database columns in your model. (Look in db/schema.rb at your column names, pick something like photo_file_name or the local equivalent.) Then make the test in the method something to match: (user.photo_file_name?) ? photo.url : ''default.png'' The problem, as I should have picked up on from your earlier mention of the missing photo never appearing, is that photo.url always returns *something*, so it''s not a good test subject for the ternary operator. Walter> > Thanks > > -- > Posted via http://www.ruby-forum.com/. > > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > 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. >-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org 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.
Walter Davis wrote in post #1049480:> On Feb 29, 2012, at 10:31 AM, Christopher Jones wrote: > >>> >> Hey Walter, good shout. >> >> I don''t know where it is getting that line from. > > Aha! That is coming from deep in the heart of Paperclip. You have two > options. First, put your missing icon image at that path in your > application. Second, choose a different method to test on your model. > Instead of using the image accessor, look for one of the actual database > columns in your model. (Look in db/schema.rb at your column names, pick > something like photo_file_name or the local equivalent.) Then make the > test in the method something to match: > > (user.photo_file_name?) ? photo.url : ''default.png'' > > The problem, as I should have picked up on from your earlier mention of > the missing photo never appearing, is that photo.url always returns > *something*, so it''s not a good test subject for the ternary operator. > > WalterFixed it, I simply added default_url : guest.png to my has_attached_file :photo model method. -- Posted via http://www.ruby-forum.com/. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Feb 29, 2012, at 10:45 AM, Christopher Jones wrote:> Walter Davis wrote in post #1049480: >> On Feb 29, 2012, at 10:31 AM, Christopher Jones wrote: >> >>>> >>> Hey Walter, good shout. >>> >>> I don''t know where it is getting that line from. >> >> Aha! That is coming from deep in the heart of Paperclip. You have two >> options. First, put your missing icon image at that path in your >> application. Second, choose a different method to test on your model. >> Instead of using the image accessor, look for one of the actual database >> columns in your model. (Look in db/schema.rb at your column names, pick >> something like photo_file_name or the local equivalent.) Then make the >> test in the method something to match: >> >> (user.photo_file_name?) ? photo.url : ''default.png'' >> >> The problem, as I should have picked up on from your earlier mention of >> the missing photo never appearing, is that photo.url always returns >> *something*, so it''s not a good test subject for the ternary operator. >> >> Walter > > Fixed it, > > I simply added default_url : guest.png to my has_attached_file :photo > model method.Good one. Then you can also remove the whole custom accessor method, because any request of photo.url will always return something useful now. Just go back to where you started, with image_tag user.photo.url. Walter -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.