设计自定义类中未定义的辅助方法

时间:2018-12-11 07:11:05

标签: ruby-on-rails devise

我一直在寻找这个bug有点...我的辅助方法似乎没有定义。或它们定义不正确。 user_unlock_url是一个具体示例。

我的控制器遵循维基:

class MyDeviseMailer < Devise::Mailer
  helper :application # gives access to all helpers defined within `application_helper`.
  include Devise::Controllers::UrlHelpers # Optional. eg. `confirmation_url`
  # Ensure that the mailer uses the devise views
  default template_path: 'devise/mailer'
  ...
end

但是调用它时会失败

> MyDeviseMailer.unlock_instructions(User.first, 'token').deliver_now
Rendering devise/mailer/unlock_instructions.html.erb within layouts/mailer
Rendered devise/mailer/unlock_instructions.html.erb within layouts/mailer (9.2ms)
MyDeviseMailer#unlock_instructions: processed outbound mail in 236.1ms
ActionView::Template::Error: undefined method `user_unlock_url' for #<ActionDispatch::Routing::RoutesProxy:0x00007fcfc621cd78> 
from /Users/brett.dudo/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/actionpack-5.2.2/lib/action_dispatch/routing/routes_proxy.rb:50:in `method_missing'
Caused by NoMethodError: undefined method `user_unlock_url' for #<ActionDispatch::Routing::RoutesProxy:0x00007fcfc621cd78> 
from /Users/brett.dudo/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/actionpack-5.2.2/lib/action_dispatch/routing/routes_proxy.rb:50:in `method_missing'

我检查了助手是否正确初始化:

> mappings = Devise.mappings.values.map(&:used_helpers).flatten.uniq
=> [:session, :password, :registration, :unlock, :authy, :passwords_with_policy, :invitation]

> routes = Devise::URL_HELPERS.slice(*mappings)
=> {:session=>[nil, :new, :destroy],
 :password=>[nil, :new, :edit],
 :registration=>[nil, :new, :edit, :cancel],
 :unlock=>[nil, :new],
 :authy=>[],
 :passwords_with_policy=>[],
 :invitation=>[nil, :new, :accept]} 

> routes.each do |module_name, actions|
*   [:path, :url].each do |path_or_url|
*     actions.each do |action|
*       action = action ? "#{action}_" : ""
*       puts "#{action}#{module_name}_#{path_or_url}"
*     end
*   end
* end

...
unlock_path
new_unlock_path
unlock_url
new_unlock_url
...

似乎已包含在内,但是为什么要user_放在前面?这似乎是我的问题,但我不知道为什么。

我的邮件发送者肯定使用了正确的值:

<p><%= link_to 'Unlock my account', unlock_url(@resource, unlock_token: @token, host: @site_host) %></p>

1 个答案:

答案 0 :(得分:0)

您的devise_for中有什么?

  devise_for :users, :path=> 'members', only: :omniauth_callbacks, controllers: {omniauth_callbacks: 'omniauth_callbacks'}

devise_for(:user)被添加到路径和URL帮助器之后会发生什么