2012-05-17 28 views
0

我在當前項目中使用Devise gem進行身份驗證。我遇到了一個問題,在我的自定義窗體中使用設計註冊和登錄窗體,我不知道如何在我的項目中實現它。在Rails自定義表單中使用設計窗體

此自定義表單有20-25個字段,如果當前用戶沒有登錄,我想在同一視圖中包含設備登錄和註冊表單。所以當用戶點擊表單的保存按鈕時,控制器會先認證或註冊用戶,然後再保存表單。

class BookController < ApplicationController 
    def new 
     @book = Shop::Book.new 
    end 

    def create 
     @book = Book.new(params[:book]) 

     # TODO:: 
     # validate/register the user if not currently logged-in 

     if @book.save 
      redirect_to :action => 'list' 
     else 
      @subjects = Subject.find(:all) 
      render :action => 'new' 
     end 
    end 
end 
+0

你可以在一個視圖中顯示兩種形式,然後使用登錄/註冊表單Ajax請求,所以用戶不需要離開頁面登錄。我能想到的唯一方法是在設計和自定義表單數據之間建立關係,並使用form_for。 – digicazter

+0

看看這是否有幫助。 http://pupeno.com/2010/08/29/show-a-devise-log-in-form-in-another-page/ – digicazter

+0

我曾考慮過首先使用form_remote_tag進行登錄和註冊,但它有一些可用性問題。這個表單將只有一個按鈕「保存」,它將首先登錄/註冊用戶,然後繼續保存模型。 –

回答

1

查看Digi_Cazter的鏈接,然後在檢查signed_in後添加表單?

在你創建方法像這應該工作:

@user = User.new(:email => '[email protected]', :password => 'password', #values from params 
    :password_confirmation => 'password') 
    @user.save 
    sign_in @user #if you want to do this 

     if @book.save 
      redirect_to :action => 'list' 
     else 
      @subjects = Subject.find(:all) 
      render :action => 'new' 
     end