2016-05-18 6 views
0

Ich habe ein Benutzermodell erstellt und wenn Benutzeranmeldung, E-Mail-Bestätigungsmail gesendet wird. Aber wenn ich meinem Rails-App ein Gebietsschema hinzufüge, begann es Fehler zu geben.Rails E-Mail-Benutzer Aktivierungslink mit Gebietsschema

Hier ist Benutzer # create action;

def create 
    @user = User.new(user_params) 
    respond_to do |format| 
    if @user.save 
     flash[:notice] = '..' 
     format.html { redirect_to root_url }   
     format.js { render action: 'new', status: :created } 
     @user.send_activation_email 
    else  
    format.html { render action: 'new' } 
    format.js { render json: @user.errors, status: 404 } 
     end 
    end 
    end 

Kontoaktivierungssteuerung;

class AccountActivationsController < ApplicationController 



    def edit 
     user = User.find_by(email: params[:email]) 
     if user && !user.activated? && user.authenticated?(:activation, params[:id]) 
      user.activate 
      log_in user 
      flash[:success] = "activated!" 
      redirect_to user 
     else 
      flash[:alert] = "Invalid link" 
      redirect_to root_url 
     end 
     end 
    end 

Teil user.rb

# Returns the hash digest of the given string. 
    def User.digest(string) 
    cost = ActiveModel::SecurePassword.min_cost ? BCrypt::Engine::MIN_COST : 
                BCrypt::Engine.cost 
    BCrypt::Password.create(string, cost: cost) 
    end 

    # Returns a random token. 
    def User.new_token 
    SecureRandom.urlsafe_base64 
    end 

    # Remembers a user in the database for use in persistent sessions. 
    def remember 
    self.remember_token = User.new_token 
    update_attribute(:remember_digest, User.digest(remember_token)) 
    end 

    # Returns true if the given token matches the digest. 
    def authenticated?(attribute, token) 
    digest = send("#{attribute}_digest") 
    return false if digest.nil? 
    BCrypt::Password.new(digest).is_password?(token) 
    end 


    # Forgets a user. 
    def forget 
    update_attribute(:remember_digest, nil) 
    end 

    # Activates an account. 
    def activate 
    update_attribute(:activated, true) 
    update_attribute(:activated_at, Time.zone.now) 
    end 

    # Sends activation email. 
    def send_activation_email 
    UserMailer.account_activation(self).deliver_now 
    end 

    # Sets the password reset attributes. 
    def create_reset_digest 
    self.reset_token = User.new_token 
    update_attribute(:reset_digest, User.digest(reset_token)) 
    update_attribute(:reset_sent_at, Time.zone.now) 
    end 

    # Sends password reset email. 
    def send_password_reset_email 
    UserMailer.password_reset(self).deliver_now 
    end 

    # Returns true if a password reset has expired. 
    def password_reset_expired? 
    reset_sent_at < 2.hours.ago 
    end 

    private 

    # Converts email to all lower-case. 
    def downcase_email 
     self.email = email.downcase 
    end 

    # Creates and assigns the activation token and digest. 
    def create_activation_digest 
     self.activation_token = User.new_token 
     self.activation_digest = User.digest(activation_token) 
    end 

Schließlich account_activation.html.erb

<h1>Sample</h1> 

<p>Hi <%= @user.name %>,</p> 

<p> 
Welcome to ...! Click on the link below to activate your account: 
</p> 

<%= link_to "Activate", edit_account_activation_url(@user.activation_token, 
                email: @user.email, locale: @locale) %> 

Schließlich wird der Fehler von der Konsole;

Started POST "/tr/users" for ::1 at 2016-05-18 12:01:24 +0300 
I, [2016-05-18T12:01:24.453564 #454] INFO -- : Processing by UsersController#create as JS 
I, [2016-05-18T12:01:24.453779 #454] INFO -- : Parameters: {"utf8"=>"✓", "user"=>{"name"=>"234234", "username"=>"234234", "email"=>"[email protected]", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "user_type"=>"Personal Account", "terms_of_service"=>"1"}, "commit"=>"Hesap Oluştur", "locale"=>"tr"} 
D, [2016-05-18T12:01:24.546048 #454] DEBUG -- : (0.2ms) begin transaction 
D, [2016-05-18T12:01:24.547422 #454] DEBUG -- : User Exists (0.2ms) SELECT 1 AS one FROM "users" WHERE LOWER("users"."email") = LOWER('[email protected]') LIMIT 1 
D, [2016-05-18T12:01:24.548145 #454] DEBUG -- : User Exists (0.1ms) SELECT 1 AS one FROM "users" WHERE "users"."username" = '234234' LIMIT 1 
D, [2016-05-18T12:01:24.725087 #454] DEBUG -- : SQL (0.4ms) INSERT INTO "users" ("name", "email", "password_digest", "username", "user_type", "created_at", "updated_at", "activation_digest") VALUES (?, ?, ?, ?, ?, ?, ?, ?) [["name", "234234"], ["email", "[email protected]"], ["password_digest", "$2a$10$l3JDATUxIMIKvMIYYtRhQ.AR8GSC3C9ybwXIFjGgU3ORCeUDjtpra"], ["username", "234234"], ["user_type", "Personal Account"], ["created_at", "2016-05-18 09:01:24.549035"], ["updated_at", "2016-05-18 09:01:24.549035"], ["activation_digest", "$2a$10$STp22TuKMbPbn14XXgVJa.yGYd3h9o31xMeEZxcNDsvAJQM4lpQ0."]] 
D, [2016-05-18T12:01:24.729336 #454] DEBUG -- : (2.7ms) commit transaction 
I, [2016-05-18T12:01:24.751436 #454] INFO -- : Rendered user_mailer/account_activation.html.erb within layouts/mailer (4.2ms) 
D, [2016-05-18T12:01:24.751639 #454] DEBUG -- : 
UserMailer#account_activation: processed outbound mail in 21.0ms 
I, [2016-05-18T12:01:24.751911 #454] INFO -- : Completed 500 Internal Server Error in 296ms 
F, [2016-05-18T12:01:24.801486 #454] FATAL -- : 
ActionController::UrlGenerationError (No route matches {:action=>"edit", :controller=>"account_activations", :email=>"[email protected]", :format=>nil, :id=>nil, :locale=>nil} missing required keys: [:id]): 
    app/views/user_mailer/account_activation.html.erb:9:in `_app_views_user_mailer_account_activation_html_erb___3249946951177976060_70255610070180' 
    app/mailers/user_mailer.rb:10:in `account_activation' 
    app/models/user.rb:95:in `send_activation_email' 

Der Fehler gibt es, dass über einen id params Konto Aktivierungs-Controller als Link Link Bearbeiten für id suchen ist. Das Problem ist, dass diese Struktur funktionierte, bevor ich das Gebietsschema auf Routen legte

Routen;

scope "(:locale)", locale: /#{I18n.available_locales.join("|")}/ do 
... 
resources :account_activations, only: [:edit] 
... 
end 

Also, wenn ich die ID des Benutzers im Konto Aktivierung HTML als senden;

<%= link_to "Activate", edit_account_activation_url(@user.activation_token, 
                email: @user.email, id: user.id) %> 

die Verbindung generiert;

localhost:3000/78y5fzxrFtGzwpr5RHlnCQ/account_activations/7/edit?email=....%40.....com 

Also, was ich hier sehe, ist, dass anstelle von Token sollte es locale variabel sein, sagen wir fr, und statt id sollte es wie dieses Token sein;

localhost:3000/fr/account_activations/78y5fzxrFtGzwpr5RHlnCQ/edit?email=....%40.....com 

Danke

EDIT:

Ich habe es leider schon hier;

class ApplicationController < ActionController::Base 
before_filter :set_locale 

def set_locale 
     I18n.locale = params[:locale] || I18n.default_locale 
     @locale = params[:locale] || I18n.default_locale 
    end 

    def default_url_options(options = {}) 
     {locale: I18n.locale} 

    end 

end 

Also wenn ich den Link in der E-Mail ändern als;

Es funktioniert nur gut, aber wie soll ich Gebietsschema Variable an E-Mail übergeben?

EDIT:

ich eine Variable bestanden haben: usermailer

def account_activation(user) 
    @user = user 
    @locale = I18n.locale 
    mail to: user.email, subject: "Account activation" 
    end 

dann in den Link aufrufen:

<%= link_to "Activate", edit_account_activation_url(id: @user.activation_token, 
email: @user.email, locale: @locale) %> 

Hoffnung hilft jemand anderes.

Antwort

1

Sie haben noch das Gebietsschema explizit in Ihren Routen setzen, finden Sie in diesen question, so etwas wie dies in Ihrem Application umfasst:

def set_locale 
    I18n.locale = params[:locale] || session[:locale] || I18n.default_locale 
    session[:locale] = I18n.locale 
    end 

    def default_url_options(options={}) 
    logger.debug "default_url_options is passed options: #{options.inspect}\n" 
    { locale: I18n.locale } 
    end 

Die locale Option nun automatisch zusammengeführt werden, wenn Sie url Helfer verwenden.