I have a page that lets an admin user register a new user, by entering details into a form.
<%= stylesheet_link_tag "signup" %>
<%= form_for(@user) do |f| %>
<% if @user.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@user.errors.count, "error") %> prohibited this user from being saved:</h2>
<ul>
<% @user.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="signup">
<div class = "field">Username:
<%= f.text_field :username, :class => "username" %></div>
</br>
<div class = "field">Email:
<%= f.email_field :email, :class => "email" %></div>
</br>
<div class = "field">First Name:
<%= f.text_field :firstname, :class => "firstname" %></div>
</br>
<div class = "field">Last Name:
<%= f.text_field :lastname, :class => "lastname" %></div>
</br>
<div class="field">
<%= f.label :admin %><br />
<%= f.text_field :admin, :class => "admin" %>
</div>
<div class = "field">
<div class="actions">
<%= f.submit "Register New User", :class => "button" %>
</div>
</div>
</div>
<% end %>
here is the new and create actions from the admin_controller:
def create
@user = User.new(params[:user])
if @user.save
respond_to do |format|
format.html { redirect_to @user, notice: 'User was successfully created.' }
format.json { render json: @user, status: :created, location: @user }
end
else
respond_to do |format|
format.html { render action: "new" }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
end
def new
@user = User.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: @user }
end
end
Once the form is filled in, and I hit register new user, I get brought back to the home page, and the data does not go into the database.
EDIT: full log after clicking register new user
I can see that the data in the logs:
Started POST "/users" for 127.0.0.1 at 2012-09-17 12:27:49 +0100
Processing by Devise::RegistrationsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"Bhh2ELWIFW9bENXeQ3Yta+bJqD7Dx8os9zdvnN0m5sM=", "user"=>{"username"=>"test", "email"=>"test@test.com", "firstname"=>"test", "lastname"=>"test", "admin"=>"false"}, "commit"=>"Register New User"}
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
Redirected to http://localhost:3000/
Filter chain halted as :require_no_authentication rendered or redirected
Completed 302 Found in 2ms (ActiveRecord: 0.2ms)
Started GET "/" for 127.0.0.1 at 2012-09-17 12:27:49 +0100
Processing by ProjectsController#index as HTML
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
Project Load (0.2ms) SELECT "projects".* FROM "projects"
Rendered projects/index.html.erb within layouts/application (1.0ms)
Completed 200 OK in 5ms (Views: 4.0ms | ActiveRecord: 0.4ms)
I am new to rails so please remember this when trying to help. Thanks in advance
You are simply not saving the user to the DB and are calling the wrong controller action.
First: The way this works in Rails is that usually
newaction renders the form to the user.Once submitted the
createaction will be hit.In
createyou then have to create a new user object containing the properties the user entered in the form. This happens throughUser.new(params[:user])(whereparams[:user]is the data the user entered into the form) and then call.saveon the object to persist it in the database.Here is how the
createaction code should ideally look like: