Only Admin can create Users

1,302 views
Skip to first unread message

MoisesZaragoza

unread,
May 31, 2012, 11:56:19 AM5/31/12
to Devise
I am trying to write a application on Ruby on Rails using Devise
I Already have a user created as a Admin but now i want to be able to
create other uses. and disable the ability for the general public to
sing up.

I Create a Users Controller that looks like
app/controllers/users_controller.rb

class UsersController < ApplicationController
before_filter :authenticate_user!

def index
@user = User.all
end
def show
@user = User.find(params[:id])
end

def new
@user = User.new
end

def create
@accounts = Account.all.map { |a| [a.name, a.id] }
@user = User.new(params[:user])
if @user.save
flash[:success] = "User Created"
redirect_to @user
else
render 'new'
end
end


______________________________________________________________________

my Model
app/models/user.rb

class User < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable


attr_accessible :email, :password, :password_confirmation, :remember_me, :username, :first_name, :last_name, :account_id
# attr_accessible :title, :body
belongs_to :account
--
validates_uniqueness_of :username, presence: true, :case_sensitive =>
false

validates :password, presence: true
validates :password_confirmation, presence: true
end

____________________________________________________________________________


Now when i go to my view in the create
app/views/users/new.html.erb

<%= form_for(@user) do |f| %>
<table class="simple-table responsive-table">
<tbody>
<tr>
<td scope="row">
<%= f.label :email %>
</td>
<td><%= f.email_field :email %></td>
</tr>
<tr>
<td scope="row">
<%= f.label :username %>
</td>
<td><%= f.text_field :username %></td>
</tr>
<tr>
<td scope="row">
<%= f.label :password %>
</td>
<td><%= f.password_field :password %></td>
</tr>
<tr>
<td scope="row">
<%= f.label :password_confirmation %>
</td>
<td><%= f.password_field :password_confirmation %></td>
</tr>
<tr>
<td scope="row">
<%= f.label :first_name %>
</td>
<td><%= f.text_field :first_name %></td>
</tr>
<tr>
<td scope="row">
<%= f.label :last_name %>
</td>
<td><%= f.text_field :last_name %></td>
</tr>
<tr>
<td colspan=2><%= f.submit "Create User" , :class =>
"button" %> &nbsp;&nbsp; <%= link_to "Cansel", users_path, :class =>
"button" %></td>
</tr>
</tbody>
</table>
<% end %>

-------------------------------------------------------------------------------
the HTML FORM
<form accept-charset="UTF-8" action="/users" class="new_user"
id="new_user" method="post">



----------------------------------------------------------


rake routes

users GET /users(.:format) users#index
users_new GET /users_new(.:format)
users#new
new_user_session GET /users/sign_in(.:format) devise/
sessions#new
user_session POST /users/sign_in(.:format) devise/
sessions#create
destroy_user_session DELETE /users/sign_out(.:format) devise/
sessions#destroy
user_password POST /users/password(.:format) devise/
passwords#create
new_user_password GET /users/password/new(.:format) devise/
passwords#new
edit_user_password GET /users/password/edit(.:format) devise/
passwords#edit
PUT /users/password(.:format) devise/
passwords#update
cancel_user_registration GET /users/cancel(.:format) devise/
registrations#cancel
user_registration POST /users(.:format) devise/
registrations#create
new_user_registration GET /users/sign_up(.:format) devise/
registrations#new
edit_user_registration GET /users/edit(.:format) devise/
registrations#edit
PUT /users(.:format) devise/
registrations#update
DELETE /users(.:format) devise/
registrations#destroy

i am using the users_new_path to get to that page

But for some reason it takes me to the hope page and does not create a
new use

Moises Zaragoza

unread,
Jun 19, 2012, 10:14:14 AM6/19/12
to plataforma...@googlegroups.com
Yes you have to actually move the dive controller into your application.
you can copy the controller from https://github.com/plataformatec/devise

from there
go to
app/controllers/ devise/registrations_controller.rb
and remove
prepend_before_filter :require_no_authentication, :only => [ :new, :create, :cancel ]

Then you probably want also remove
sign_in(resource_name, resource)


Let me know if you still have problems

Thanks
Moises Zargoza
mZar...@circleTechFl.com
http://www.circleTechFl.com
(954)592-2500
Send 'Moises' To 90210



On Tue, Jun 19, 2012 at 12:59 AM, Najera <jorge....@solcomla.com> wrote:
I got the same problem!!

Did you find a solution???



--




Reply all
Reply to author
Forward
0 new messages