简体   繁体   中英

rails - Devise - Creating a Registration Form

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.

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

Here's my full controller:

  # 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

The model:

  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]”。

Just insert this at the beginning of your form, end before the end of it:

  <%= devise_error_messages! %>

Devise is kinda tricky, I just finally managed to figure it out myself, and it's awesome. Btw, don't render the view as a partial, use templates, and then set your root to users#registrations_controller , given that you created custom controllers and views.

If you haven't, I wrote a blog post about it here

Where is your <%= error_messages_for %> ? That is how errors will be displayed.

Also, devise needs virtual attributes in the model for additional attributes so don't forget to add :lname , :fname etc... to the devise user model or whatever you are doing.


Update

<%= f.error_messages_for :model %>

Before Rails 3 this is how errors were formatted, but in R3 it's depircated and you need to install a plugin to access errors this way. See comments for the link to the plugin

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM