English 中文(简体)
如何在Devise确认后改用网页
原标题:How to redirect page after confirmation in Devise

Say a的用户点击了与一个受保护的网页链接。 然后将其转至可以登录的屏幕上签字。 如果是的话,它们就会成功地转向该网页。 但是,如果他们没有账户,他们必须签字。 这是因为我用电子邮件确认。

By clicking a link it creates a new session can I can t automatically redirect the user to that protected page. I m trying to change this by putting in a reference to the redirect inside the confirmation link. I would like to do:

<%= link_to  Confirm my account , confirmation_url(@resource, :confirmation_token => @resource.confirmation_token, :redirect_to => stored_location_for(@resource)) %>

但是,我可以说明如何进入<条码>存储-地点。 (或者即便是正确的地点)。 http://rubydoc.info/github/plataformatec/devise/master/Devise/Controllers/Helpers#stored_ place_for-instance_method” rel=“nofollow noreferer”>:Controllers:Helpers<>>,但这是一种实例方法,因此我可以打上。 Devise:Controllers:Helpers.stored_place_for(......)

我怎样才能查阅<条码> 存储-地点_for。 或者这样做的更好方式是什么?

我的目标是这样做,然后在我的习俗确认中这样做。 主计长界定:

def show
  if params[:redirect_to]
    session["user_return_to"] = params[:redirect_to]
  end
  super

end

这应该正确吗?

最佳回答

我是这样说的。 我不敢肯定的是,随着更新的“Devise”的改变,昨天在“Devise:邮局”的制作中做了很多工作。 (见codeticket ,以供参考)。

基本上,它 b倒了无法在邮件地址查阅<代码>session。 因此,你必须把这一方向作为变量。 浏览器使用<代码>以下关于贵用资源的方法(User<>/code>,就我而言),然后发送确认电子邮件。 这意味着,我只能把会议直接通过给邮寄人。 因此,我感到,这只是为了取得这一功能而进行的初步工作,但在这里是:

查阅<代码>redirect_to 用户必须增加一个变量,从而:

class User < ActiveRecord::Base
  …
  attr_accessor :return_to
  …
end

然后,当你首次创建用户时,你必须确定这一变量。

我已经为登记设立了一个海关控制员。 (见Devise Readme, 说明如何建立这一系统,或见@ramc关于方向的答复)。 但是,完成这一部分相对容易,我只是把它添加到参数上,让其他人自己照顾。

class RegistrationsController < Devise::RegistrationsController
  def create
    params[:user][:return_to] = session[:user_return_to] if session[:user_return_to]
    …
    super
  end
end

现在,用户有一套可变的<代码>return_to,现列出。 我们只需要查阅<代码>确认_instructions email。 我已经重新撰写了确认部分内容——内容结构。

<% if @resource.return_to %>
  <%= link_to  Confirm my account , confirmation_url(@resource, :confirmation_token => @resource.confirmation_token, :redirect_to => @resource.return_to) %>
<% else %>
  <%= link_to  Confirm my account , confirmation_url(@resource, :confirmation_token => @resource.confirmation_token) %>
<% end %>

(对于新加入者而言,@resource 是用于界定用户的可变幻用途。)

现在,一旦用户点击这一环节,我们就需要重新定位。 @ramc s 之前很好地完成了这项工作:

class ConfirmationsController < Devise::ConfirmationsController
  before_filter :set_redirect_location, :only => :show

  def set_redirect_location
    session[:user_return_to] = params[:redirect_to] if params[:redirect_to]
  end
end

这将考虑到这样的情况,即新用户进入受保护的网页,然后打上标识,点击确认链接,并适当改用受保护的网页。

现在,我们只是需要关注用户做上述事情的情况,而不是点击这一联系,而是试图回到受保护的网页上。 在这种情况下,要求他们签署/签署。 他们签了字,然后被要求确认其电子邮件,并有权选择转拨确认书的电子邮件。 这些文件载于其电子邮件,我们现在需要将redirect_to变量列入新的确认书电子邮件。

为了做到这一点,我们需要修改主计长,就像我们如何做登记主计长那样。 现在我们需要修改<代码>create方法。 该盒子的出路是,在用户上使用一个称为send_confirmation_instructions的类别方法。 我们希望重新制定这一方法,以便我们能够通过return_to。 可变。

class ConfirmationsController < Devise::ConfirmationsController
  def create
    self.resource = resource_class.send_confirmation_instructions(params[resource_name],session[:user_return_to])

    if resource.errors.empty?
      set_flash_message(:notice, :send_instructions) if is_navigational_format?
      respond_with resource, :location => after_resending_confirmation_instructions_path_for(resource_name)
    else
      respond_with_navigational(resource){ render_with_scope :new }
    end
  end
end

与Devise相比,唯一不同的是,第1行<代码>create,我们在其中通过两个变量。 现在我们需要重新制定这一方法:

class User < ActiveRecord::Base
  def self.send_confirmation_instructions(attributes={},redirect=nil)
    confirmable = find_or_initialize_with_errors(confirmation_keys, attributes, :not_found)
    confirmable.return_to = redirect if confirmable.persisted?
    confirmable.resend_confirmation_token if confirmable.persisted?
    confirmable
  end
end

confirmable becomes an instance of User (the current user based on email). So we just need to set return_to.

这就是说。

问题回答

看看存储方式——地点——已在校准/开发/控制器/助手实施。

def stored_location_for(resource_or_scope)
   scope = Devise::Mapping.find_scope!(resource_or_scope)
   session.delete("#{scope}_return_to")
end

It is possible to otherwise access it using session[ user_return_to ]. In your case, you would lose that session object because when the user clicks on the link from the confirmation mail, it might be a new session that is spawned.

You can implement whatever you have suggested as a before filter:

class Users::ConfirmationsController < Devise::ConfirmationsController 
     before_filter :set_redirect_location, :only => :show

     def set_redirect_location
         session["user_return_to"] = params[:redirect_to] if params[:redirect_to]
     end
end

In addition to this, you will have to modify the route to make devise call your controller instead of its own confirmation controller.

devise_for :users,
           :controllers => { :confirmations =>  users/confirmations }

希望:

注:代码表不完整,仅载列相关细节。

根据我从设计来源法典中的评论中可以看到的情况,你需要做的就是在你的工作中执行以下内容。 登记——控制员。

def after_inactive_sign_up_path_for(resource_or_scope)
  session["user_return_to"]
end




相关问题
Remove ActiveRecord in Rails 3

Now that Rails 3 beta is out, I thought I d have a look at rewriting an app I have just started work on in Rails 3 beta, both to get a feel for it and get a bit of a head-start. The app uses MongoDB ...

When will you upgrade your app to Rails 3? [closed]

Now that the Rails 3 beta is here, let s take a little straw poll. Please tell us briefly what your application does and when you will upgrade it to Rails 3. Or, if you re not planning on upgrading ...

Bundler isn t loading gems

I have been having a problem with using Bundler and being able to access my gems without having to require them somewhere, as config.gem used to do that for me (as far as I know). In my Rails 3 app, I ...

bypass attr_accessible/protected in rails

I have a model that, when it instantiates an object, also creates another object with the same user id. class Foo > ActiveRecord::Base after_create: create_bar private def create_bar Bar....

concat two fields activerecord

I m so used to oracle where you can simply concat(field1, , field2) but if I m using activerecord to find the field1 and field2, and I need a space in between, how do I accomplish this? Cheers ...

热门标签