[英]Rails / Devise: Gracefully ask user to sign in on signed-out create
[英]Rails, Devise: If user signed in create with user id, else without
我有一個流控制器,正在使用Devise。 用戶可以創建具有(而不是在db中具有該用戶的ID的流)用戶ID的流,也可以不具有(創建其賓客流的)流帳戶的流。 目前,我有以下創建方法:
def create
@stream = current_user.streams.build(params[:name])
if @stream.valid?
@stream.save
redirect_to c_path(@stream.name)
flash[:notice] = "blabla"
else
redirect_to root_path
end
end
但這不適用於客人,因為我使用current_user。 現在,我的計划是創建一個if else(如果使用current_user登錄,則不是)。
我該怎么做?
謝謝!
很簡單,您可以使用user_signed_in?
幫手:
if user_signed_in?
@stream = current_user.streams.build(params[:name])
else
@stream = Stream.new(params[:name])
end
BTW確保在構建對象時傳遞有效的參數。
設計有一個user_signed_in嗎? 方法。 因此,您可以這樣做:
if user_signed_in?
@stream = current_user.streams.build(params[:name])
else
@stream = Stream.new(params[:name])
end
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.