Write to ruby-on-Rails 3.2

Asked

Viewed 65 times

1

Following the tutorial of railscast to add an image of the site Ravatar I found a lagged information with the updates of Rails. The code to create a default image in case of absence of registration on the Ravatar site does not work as in the code below suggests. I tried to exchange "images" for "Assets", does anyone have any idea that can solve the problem? follow the code:

module ApplicationHelper
  def avatar_url(user)
    default_url = "#{root_url}assets/default.jpg"
    gravatar_id = Digest::MD5::hexdigest(user.email).downcase
    "http://gravatar.com/avatar/#{gravatar_id}.png?s=48&d=#{CGI.escape(default_url)}"
  end
end

Follow the View code that calls the image:

<%= image_tag avatar_url(f) %>

Follow the link of the broken image that looks like it:

http://i2.wp.com/localhost/assets/default.jpg

  • 1

    Thank you Sergio!

1 answer

1

Gravatar is very simple, put the line below in your helper, 'app/helpers/application_helper.Rb':

  def avatar_url(user)
    if user.url_image.present?
      user.url_image
    else
      gravatar_id = Digest::MD5.hexdigest(user.email.downcase)
      "http://gravatar.com/avatar/#{gravatar_id}.png?s=48"
    end
  end

And under 'app/views/home/index.html', call the helper by passing the current user as parameter:

Logado como <%= avatar_url(current_user) %> <%= current_user.email %>. Não é você?

The method code is very simple, first it checks if there is any image url in the field 'url_image' that you placed earlier, otherwise it will use the user’s email as an id to fetch the image that is stored in Gravatar, the parameter’s=48' at the end is to set the size to resize the image.

Source: http://vagnerzampieri.blogspot.com.br/2011/03/ruby-on-rails-do-zero-devise.html

  • However I am not using a user to store an image. I would like to use a default image for everyone who does not have a registered email. In railscast it says that this can be done by the parameter’d=#{CGI.escape(default_url)}' and that the default_url would take my image from the Assets folder.

  • You’re making a mistake?

  • The image does not appear. And searching the image path is not the path I passed.

  • Tried to restart the server? Do the following paste your view code there in the question for me take a look and the link that is coming.

  • Yes I already tried restarting the server. I edited updating the question with the requested information

  • Email images it can pull and works normally. Only the path to the folder where the default image is found that does not work.

  • Dude, the path you’re passing it on default is right? instead of images try #{root_url}Assets/guest.png

  • yes I switched. "images" was used in the Rails version before 3.2

  • Apparently this ok your code. Do you have it on github? Maybe I can download it here and test it if you want.

  • unfortunately I don’t have it in git. it’s in a closed repository.

  • have skype? pass me there.

  • ygorbr1. Add me there. However I can not answer at the moment.

Show 7 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.