Your method is defined in the UsersHelper
, however your view is for Devise::Registration
. So you should move to the proper helper, to the application helper, or create a class with shared User
and Devise::Registration
methods and include in both helpers.
undefined method `gravatar_for' within edit registration path for devise
-
31-05-2022 - |
题
I have a failing rspec test that I can't seem to figure out what could be wrong. The method is clearly defined, and it works on the site itself; just is failing the test(s).
UserPages edit with invalid information
Failure/Error: visit edit_user_registration_path(user)
ActionView::Template::Error:
undefined method `gravatar_for' for_app_views_devise_registrations_edit_html_erb
# ./app/views/devise/registrations/edit.html.erb:8:in
`_app_views_devise_registrations_edit_html_erb
# ./spec/requests/user_pages_spec.rb:114:in `block (3 levels) in <top (required)>'
Here is the rspec test:
describe "edit" do
let(:user) {FactoryGirl.create(:user)}
before do
sign_in user
visit edit_user_registration_path(user)
end
describe "page" do
it { should have_selector('h1', text: "Update your profile")}
it { should have_selector('title', text: "Edit user")}
it { should have_link('change', href: "http://gravatar.com/emails")}
end
describe "with invalid information" do
before { click_button "Save changes"}
it { should have_content('error')}
Here is the actual method:
module UsersHelper
#Returns the Gravatar for the given user.
def gravatar_for(user, options = { size: 50})
gravatar_id = Digest::MD5::hexdigest(user.email.downcase)
size = options[:size]
gravatar_url = "https://secure.gravatar.com/avatar/#{gravatar_id}?s=#{size}"
image_tag(gravatar_url, alt: user.name, class: "gravatar")
end
end
And lastly the code within the view itself:
<%= gravatar_for @user %>
<a href="http://gravatar.com/emails" target="_blank">change avatar</a>
解决方案
不隶属于 StackOverflow