Ruby on rails 没有路由匹配缺少必需的键:[:locale]
我遇到了一个无法解决的问题。我已经创建了一个登录表单,我希望当它发送时,用户会收到一封带有令牌的电子邮件,用于验证该签名。但当我发送表单时,我收到了以下错误: ActionController::UrlGenerationError in Users#createRuby on rails 没有路由匹配缺少必需的键:[:locale],ruby-on-rails,ruby,mailcatcher,Ruby On Rails,Ruby,Mailcatcher,我遇到了一个无法解决的问题。我已经创建了一个登录表单,我希望当它发送时,用户会收到一封带有令牌的电子邮件,用于验证该签名。但当我发送表单时,我收到了以下错误: ActionController::UrlGenerationError in Users#create 没有路线匹配 {:action=>“confirm”,:controller=>“users”,:id=>26, :token=>“8ftJFxWKJqiMDDhtrqf1rVCq”},缺少必需的键:[:locale] 谢谢你的帮助
没有路线匹配 {:action=>“confirm”,:controller=>“users”,:id=>26, :token=>“8ftJFxWKJqiMDDhtrqf1rVCq”},缺少必需的键:[:locale] 谢谢你的帮助 用户\u controller.rb
def new
@user = User.new
end
def create
@user = User.new(user_params)
if @user.valid?
@user.save
UserMailer.confirm_email(@user).deliver_now
redirect_to :new
else
render 'new'
end
end
private
def user_params
params.require(:user).permit(:username, :email, :password, :password_confirmation)
end
routes.rb
scope "/:locale", locale: /en|fr|nl/ do
resources :projects, only:[:index]
resources :technos, only:[:index]
resources :blogs
resources :users, only: [:new, :create] do
member do
get 'confirm'
end
end
end
在confirm_email.html.erb中
<%= confirm_user_url( id: @user.id, token: @user.confirmation_token ) %>
尝试在电子邮件中添加以下内容:
<%= confirm_user_url( id: @user.id, token: @user.confirmation_token, locale: 'fr' ) %>
这可能行得通。我终于解决了以下问题:
<%= confirm_user_url( id: @user.id, token: @user.confirmation_token, locale: I18n.locale ) %>
如果要使locale参数成为可选参数,使其默认为例如en,则需要将范围定义为
(:locale)
。或者使用default\u url\u options
填充区域设置。
<%= confirm_user_url( id: @user.id, token: @user.confirmation_token, locale: I18n.locale ) %>
config.action_mailer.default_url_options = { host: ' ' }