質問

I'm using this guide from Devise, to set the ability of the user to create an account without a password, but to set it later on confirmation.

However errors are being showed through the <%= devise_error_messages! %> excerpt. I want to change the controller, so as it's show on the usual flash from Rails.

How can I do that?

The controller I'm overriding is as follow:

# 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
役に立ちましたか?

解決

I think you may have error messages and flash messages mixed up. When you get errors being shown through the <%= devise_error_messages! %>, those error messages are coming from the validations that are set on the model. For example in the User model,

validates :password, presence: true

These are different errors compared to the flash messages. You set flash messages in the controller like this

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

These flash messages just respond to the conditions you specify in the controller actions. Hope this helps

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top