【问题标题】:Change flash error from Devise confirmation从设计确认更改闪存错误
【发布时间】:2013-06-01 02:57:29
【问题描述】:

我正在使用来自 Devise 的 guide 来设置用户在没有密码的情况下创建帐户的能力,但稍后在确认时进行设置。

但是,<%= devise_error_messages! %> 摘录显示了错误。我想更改控制器,使其显示在 Rails 的常用 flash 上。

我该怎么做?

我要覆盖的控制器如下:

# app/controllers/confirmations_controller.rb
class ConfirmationsController < Devise::ConfirmationsController

  layout "login"

  # Remove the first skip_before_filter (:require_no_authentication) if you
  # don't want to enable logged users to access the confirmation page.
  skip_before_filter :require_no_authentication
  skip_before_filter :authenticate_user!

  # PUT /resource/confirmation
  def update
    with_unconfirmed_confirmable do
      if @confirmable.has_no_password?
        @confirmable.attempt_set_password(params[:user])
        if @confirmable.valid?
          do_confirm
        else
          do_show
          @confirmable.errors.clear #so that we wont render :new
        end
      else
        self.class.add_error_on(self, :email, :password_allready_set)
      end
    end

    if !@confirmable.errors.empty?
      render 'devise/confirmations/new' #Change this if you don't have the views on default path
    end
  end

  # GET /resource/confirmation?confirmation_token=abcdef
  def show
    with_unconfirmed_confirmable do
      if @confirmable.has_no_password?
        do_show
      else
        do_confirm
      end
    end
    if !@confirmable.errors.empty?
      self.resource = @confirmable
      render 'devise/confirmations/new' #Change this if you don't have the views on default path 
    end
  end

  protected

  def with_unconfirmed_confirmable
    @confirmable = User.find_or_initialize_with_error_by(:confirmation_token, params[:confirmation_token])
    if !@confirmable.new_record?
      @confirmable.only_if_unconfirmed {yield}
    end
  end

  def do_show
    @confirmation_token = params[:confirmation_token]
    @requires_password = true
    self.resource = @confirmable
    render 'devise/confirmations/show' #Change this if you don't have the views on default path
  end

  def do_confirm
    @confirmable.confirm!
    set_flash_message :notice, :confirmed
    sign_in_and_redirect(resource_name, @confirmable)
  end
end

【问题讨论】:

    标签: ruby-on-rails devise devise-confirmable


    【解决方案1】:

    我认为您可能混淆了错误消息和闪存消息。当您通过&lt;%= devise_error_messages! %&gt; 显示错误时,这些错误消息来自模型上设置的验证。例如在User 模型中,

    validates :password, presence: true
    

    与 Flash 消息相比,这些是不同的错误。您像这样在控制器中设置flash 消息

    def create
      # code .....
      if @post.save
        flash[:success] = "Post was successfully created"
        redirect_to [@investigation, @post]
      else
        flash.now[:error] = "Post was not created"
        render 'new'
      end
    end
    

    这些闪现消息仅响应您在控制器操作中指定的条件。希望这会有所帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-16
      • 2019-01-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多