rails - Devise - 创建注册表单 [英] rails - Devise - Creating a Registration Form

查看:86
本文介绍了rails - Devise - 创建注册表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在撞墙,试图了解如何让Devise与客户注册一起工作。

I've been banging my head against the wall try to understand how to get Devise to work with customer registration....

所以在我的着陆页上想要显示一个注册表单,所以我把它添加到我的看法:

So On my landing page I want to show a registration form, so I added this to my view:

<%= render 'devise/registrations/new' %>

在该部分我看到一个表单标签,如下所示:

In that partial I have in the view a form tag like follows:

<%= form_for(user_registration_path, :url => user_registration_path) do |f| %>
.
.

在我的应用程序布局中,我有:

In my application layout I have:

<% flash.each do |key, value| %>
    <div class="flash <%= key %>"><%= value %></div>
<% end %>

我遇到的问题是当我提交一个新的无效参数的注册表单时,看到错误消息?

The issue I'm having is when I submit a new registration form with invalid params, I don't see the error message?

但是,如果我提交有效的信息,表单表示它的工作,我需要检查我的电子邮件的确认链接,这是好的。

But if I submit valid info the form does say it worked and that I need to check my email for the confirmation link, which is good.

你能帮我了解如何让这个工作结束2-end,以便我可以显示错误:

Can you help me understand how to get this working end-2-end so I can display the errors:

这是我的完整控制器:

  # GET /users/new
  # GET /users/new.xml                                            
  # GET /users/new.json                                    HTML AND AJAX
  #-------------------------------------------------------------------
  def new
    respond_to do |format|
      format.json { render :json => @user }   
      format.xml  { render :xml => @user }
      format.html
    end
  end

  # GET /users/1/edit                                                      
  # GET /users/1/edit.xml                                                      
  # GET /users/1/edit.json                                HTML AND AJAX
  #-------------------------------------------------------------------
  def edit
    respond_to do |format|
      format.json { render :json => @user }   
      format.xml  { render :xml => @user }
      format.html
    end

  rescue ActiveRecord::RecordNotFound
    respond_to_not_found(:json, :xml, :html)
  end

  # POST /users
  # POST /users.xml         
  # POST /users.json                                      HTML AND AJAX
  #-----------------------------------------------------------------
  def create
    @user = User.new(params[:user])

    if @user.save
      respond_to do |format|
        format.json { render :json => @user.to_json, :status => 200 }
        format.xml  { head :ok }
        format.html { redirect_to :action => :index }
      end
    else
      respond_to do |format|
        format.json { render :text => "Could not create user", :status => :unprocessable_entity } # placeholder
        format.xml  { head :ok }
        format.html { render :action => :new, :status => :unprocessable_entity }
      end
    end
  end

模型:

  validates :fname, :presence => true, :length => { :minimum => 2 }
  validates :lname, :presence => true, :length => { :minimum => 2 }
  validates :password, :presence => true, :length => { :minimum => 6 }
  validates :email, :presence => true, :length => { :minimum => 6 }


推荐答案

从github自述文件:
请记住,Devise使用Flash消息让用户知道登录是成功还是失败,Devise希望您的应用程序适当地调用flash [:notice]和flash [:alert]。

From the github readme: "Remember that Devise uses flash messages to let users know if sign in was successful or failed. Devise expects your application to call "flash[:notice]" and "flash[:alert]" as appropriate."

这篇关于rails - Devise - 创建注册表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆