0

我在当前项目中使用 Devise gem 进行身份验证。我遇到了在我的自定义表单中使用设计注册和登录表单的问题,我不确定如何在我的项目中实现它。

此自定义表单有 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
4

1 回答 1

2

查看 Digi_Cazter 的链接,然后在检查 signed_in 后添加表单?

在您的 create 方法中,这样的东西应该可以工作:

   @user = User.new(:email => 'test@example.com', :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 
于 2012-05-17T20:31:05.667 回答