I'm having an issue with a Rails form for user registration. When the button is clicked, nothing happens. Nothing is logged in the console, nothing is created and the page remains the same. Here's the view users/new.html.erb:
<section>
<div class="container">
<div class="row">
<%= form_for(@user) do |f| %>
<%= f.text_field :username, :placeholder => "User name" %>
<%= f.email_field :email, :placeholder => "Email" %>
<%= f.password_field :password, :placeholder => "Password" %>
<%= f.submit "Create an account" %>
<% end %>
</div>
</div>
</section>
And here is the controller.
def new
@user = User.new
end
def create
@user = User.new(user_params)
if @user.save
session[:user_id] = @user.id
redirect_to '/'
else
redirect_to '/register'
end
end
private
def user_params
params.require(:user).permit(:username, :email, :password)
end
I've got the following route set up:
resources :users
get 'register' => 'users#new'
And finally my user migration (the model has has_secure_password)
class CreateUsers < ActiveRecord::Migration
def change
create_table :users do |t|
t.string :username
t.string :email
t.string :password_digest
t.timestamps null: false
end
end
end
You're not logging anything in the failure case (the else
in your create
action), and you're redirecting instead of rendering so you'll be getting a new @user
each time rather than displaying the existing @user
with any errors it may contain.
Change your else
to:
else
logger.debug @user.errors.full_messages
render :new
end
I'm not sure if you do have any error handling code in your new
view, but even if you don't the above will at least log the errors for you.
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.