簡體   English   中英

使用Devise身份驗證和Rails登錄后丟失的用戶帖子?

[英]User post lost after login with Devise authentication and Rails?

在我的Rails應用程序中(具有devise身份驗證),任何人都可以開始創建帖子,但是如果他們在單擊保存/創建/發布時未登錄,則他們首先必須創建用戶名或login = perfect。

我的代碼:

 before_filter :authenticate_user!, :except => [:show, :index, :new, :edit]

可悲的是,一旦他們登錄,要么重定向到索引頁面,要么回到帖子,但是一切都消失了,隨后用戶不得不重新開始。

如何更改重定向以及如何防止帖子在再次登錄后丟失?

我知道與以下內容有關:

  def after_sign_up_path_for(resource)
  new_user_confirmation_path
  end

HTTP是無狀態的 -發布參數(以及所有其他參數)僅對一個請求有效。 基本上,http無法知道重定向之前傳輸了哪些參數。

這是會話可能派上用場的地方-您可以在控制器中執行類似的操作來解決這兩個問題。

before_filter :authenticate_user!, :except => [:show, :index, :new, :edit, :create]
# include create action inside except hash

def create
  if !user_signed_in? # check whether user is logged in manually
    session[:post] = params[:post] # assign post data to session if not
    redirect_to new_user_session_path
  else
    # ...
  end
end

在該會話中,http變為“有狀態”,因為您的會話哈希在多個請求中都可用(直到銷毀它)。

然后,當用戶登錄后,使用表單內的會話來預先填充表單字段。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM