0

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"=>"[email protected]", "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

3
  • 1
    db entries are made in the create action Commented Sep 17, 2012 at 11:06
  • your post request ends up in a befopre_filter named: :require_no_authentication. What's its content? Commented Sep 17, 2012 at 11:36
  • before_filter :authenticate_user! #:except => [:show, :index] but this is in my projects controller. I can't seem to find the devise controller to check exactly what is going on. Commented Sep 17, 2012 at 11:41

1 Answer 1

1

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 new action renders the form to the user. Once submitted the create action will be hit.

In create you then have to create a new user object containing the properties the user entered in the form. This happens through User.new(params[:user]) (where params[:user] is the data the user entered into the form) and then call .save on the object to persist it in the database.

Here is how the create action code should ideally look like:

def create
    @user = User.new(params[:user])
    if @user.save

      respond_to do |format|
        format.html # new.html.erb
        format.json { render json: @user }
      end
    else
      # validation failed
      respond_to do |format|
        format.html { render 'new' } #this will re-display the new form with model errors
        format.json { #give some error in the JSON }
      end
    end
  end
Sign up to request clarification or add additional context in comments.

4 Comments

Sorry I forgot to include my create action. It still doesn't seem to work after editing it to look like your suggestion.
Are there validation errors that get fired? Is the right action hit? Your log entry does not reveal what the URL or the action where. I would bet you either hit the wrong controller action.
No validation errors flag up. I have two controllers, a projects controller and admin controller. I will add a full log to my question
Obviously the action is never hit but Devise redirects the user to the root_url due to some authentication problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.