如何强制用户登录某个动作然后在以后执行。

| 我试图强迫用户在我的文章控制器中调用此更新操作时登录(我正在尝试逐步参与),但是一旦他们登录,我仍要调用此操作而不是暂停。
 def update
    @article.attributes = params[:article]
    @article.save

    #store this article as a session variable
    session[:pending_article] = @article.body

    respond_with(@article, :location => article_url(@article))
  end
现在,我正在使用before_filter进行需要用户登录的操作
 def require_user
    unless current_user
      store_location
      flash[:notice] = \"You must be logged in to access this page\"
      redirect_to login_url
      return false
    end
  end
但是,我了解到,过滤器一旦重定向便会停止原始操作,因此永远不会调用update。基本上,我希望用户登录以保存文章,但是我想保存他们的工作,因此我将文章正文存储在稍后获取的会话变量中。是否有更好的方法要求用户登录才能执行操作,但之后仍要调用它?     
已邀请:
在您的require_user方法中,您可以执行以下操作:
session[:article] = params[:article]
然后在您的登录方法(/ sessions / create?)中执行以下操作:
# this should take you back /articles/new,
# you may have to move your call to store_location 
# or manually set session[:return_to]
redirect_back_or_default 
然后在ArticlesController#new中
def new
  @article = Article.new(session[:article] || {})
end
然后,会话中保存的文章参数仍然存在,因此表单已预先填写。 但是请小心在会话中存储太多内容。在Rails中,默认会话存储是cookie,并且cookie仅保存约4k的数据。您可能需要更改您的会话存储以实现此目的。     

要回复问题请先登录注册