2012-05-17 25 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