3

enter image description here I've met a problem , I can't solved. I've created a sign in form, and I would like when it send it, the user receive an email with token for validate the sign. But when i send the form I received this error:

ActionController::UrlGenerationError in Users#create
No route matches {:action=>"confirm", :controller=>"users", :id=>26, :token=>"8ftJFxWKJqiMDDhtrqf1rVCq"}, missing required keys: [:locale]

Thanks for your help!

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 

in confirm_email.html.erb

<%= confirm_user_url( id: @user.id, token: @user.confirmation_token ) %>

and in user.rb

def confirm_email(user)
  @user = user 
  mail( to: user.email, subject: "Votre inscription") 
end 
Fernand
  • 1,293
  • 1
  • 10
  • 18
  • If you want to make the locale parameter optional so that it defaults to for example en you need to define the scope as `(:locale)`. Or use `default_url_options` to fill in the locale. – max Apr 05 '20 at 08:04

2 Answers2

2

I finally fixed the problem with :

<%= confirm_user_url( id: @user.id, token: @user.confirmation_token, locale: I18n.locale ) %>  

and add my application.rb thise line:

config.action_mailer.default_url_options = { host: ' ' }
Dharman
  • 30,962
  • 25
  • 85
  • 135
1

Try adding the following in your email:

<%= confirm_user_url( id: @user.id, token: @user.confirmation_token, locale: 'fr' ) %>

That might work.

stephenmurdoch
  • 34,024
  • 29
  • 114
  • 189