我无法在Rails中重定向到其他网站URL

时间:2019-02-04 07:09:22

标签: ruby-on-rails

我有以下控制人

  

Users :: ConfirmationsController

采取行动

def show
  self.resource = resource_class.confirm_by_token(params[:confirmation_token])
  yield resource if block_given?
  if resource.errors.empty?
    respond_with_navigational(resource){ redirect_to after_confirmation_path_for(resource_name, resource) }
  else
    respond_with_navigational(resource.errors, status: :unprocessable_entity){ redirect_to after_confirmation_path_for(resource_name, resource) }
  end
end

受保护的方法after_confirmation_path进行重定向,

protected
  def after_confirmation_path_for(resource_name, resource)
   flash[:notice] = "Your account was successfully confirmed. Please sign  in using the mobile application."
   redirect_to "http://referd.net"
  end
end

2 个答案:

答案 0 :(得分:0)

在您的show操作中,而不是像这样直接将after_confirmation_path_for覆盖redirect到其他网址。

def show
  self.resource = resource_class.confirm_by_token(params[:confirmation_token])
  if resource.errors.empty?
    flash[:notice] = "Your account was successfully confirmed. Please sign  in using the mobile application."
    redirect_to "http://referd.net"
  else
    respond_with_navigational(resource.errors, status: :unprocessable_entity){ redirect_to after_confirmation_path_for(resource_name, resource) }
  end
end

答案 1 :(得分:0)

实际上问题出在AbstractController :: DoubleRenderError,现在可以了。

class Users::ConfirmationsController < Devise::SessionsController

    def show
      self.resource = resource_class.confirm_by_token(params[:confirmation_token])
      yield resource if block_given?
      if resource.errors.empty?
        respond_with_navigational(resource){ redirect_to after_confirmation_path_for(resource_name, resource) }
      else
        respond_with_navigational(resource.errors, status: :unprocessable_entity){ redirect_to after_confirmation_path_for(resource_name, resource) }
      end
    end
    protected

    def after_confirmation_path_for(resource_name, resource)
      flash[:notice] = "Your account was successfully confirmed. Please sign in using the mobile application."
      **"http://referd.net"**
    end
end