【发布时间】:2020-04-04 23:25:07
【问题描述】:
enter image description here 我遇到了一个问题,我无法解决。我创建了一个登录表单,我希望当它发送它时,用户会收到一封带有验证标志的电子邮件。但是当我发送表单时,我收到了这个错误:
ActionController::UrlGenerationError in Users#create
没有路线匹配 {:action=>"确认", :controller=>"users", :id=>26, :token=>"8ftJFxWKJqiMDDhtrqf1rVCq"},缺少必需的键:[:locale]
感谢您的帮助!
users_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 ) %>
在user.rb中
def confirm_email(user)
@user = user
mail( to: user.email, subject: "Votre inscription")
end
【问题讨论】:
-
如果你想让 locale 参数可选,以便它默认为例如 en,你需要将范围定义为
(:locale)。或者使用default_url_options填写语言环境。
标签: ruby-on-rails ruby mailcatcher