Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
CRUD Devise Example
<%= 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 %>
<p><%= f.label :username %><br />
<%= f.text_field :username %></p>
<p><%= f.label :first_name %><br />
<%= f.text_field :first_name %></p>
<p><%= f.label :last_name %><br />
<%= f.text_field :last_name %></p>
<p><%= f.label :email %><br />
<%= f.email_field :email %></p>
<% if action_name == "new" %>
<p><%= f.label :password, "New password" %><br />
<%= f.password_field :password %></p>
<p><%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation %></p>
<% end %>
<p>
<%= f.label :role %><br />
<%= f.collection_select :role, User::ROLES, :to_s, :humanize %>
</p>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
<%= 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 %>
<p><%= f.label :password, "New password" %><br />
<%= f.password_field :password %></p>
<p><%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation %></p>
<div class="actions">
<%= f.submit "Change password" %>
</div>
<% end %>
<h2>Edit user</h2>
<%= render 'form' %>
<p></p>
<%= render 'passwords' %>
<%= link_to 'All users', users_path %>
<h1>All Users</h1>
<table id="users">
<thead>
<tr>
<th>Username</th>
<th>First Name</th>
<th>Last Name</th>
<th>Email</th>
<th>Login Count</th>
<th>Last Sign In</th>
<th>Role</th>
<th>Created At</th>
<th>Last Edited</th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<% @users.each do |user| %>
<tr>
<th><%= user.username %></th>
<th><%= user.first_name %></th>
<th><%= user.last_name %></th>
<th><%= user.email %></th>
<th><%= user.sign_in_count %></th>
<th><%if user.last_sign_in_at.blank? %>Never logged in<% else %><%= time_ago_in_words(user.last_sign_in_at.to_datetime)+' ago' %><% end %></th>
<th><%= user.role %></th>
<th><%= user.created_at.to_s(:datetime).downcase %></th>
<th><%= user.updated_at.to_s(:datetime).downcase %></th>
<th><%= link_to "Edit", edit_user_path(user) %></th>
<th><%= link_to "Delete", user, :method => :delete, :confirm => "Are you sure?" %></th>
</tr>
<% end %>
</tbody>
</table>
<p><%= link_to "New User", new_user_path %></p>
<h2>Sign up</h2>
<%= render 'form' %>
<%= link_to 'All users', users_path %>
<p><%= @user.username %></p>
<p><%= @user.first_name %></p>
<p><%= @user.last_name %></p>
<p><%= @user.email %></p>
class UsersController < ApplicationController
# GET /users
# GET /users.json
def index
@users = User.all
respond_to do |format|
format.html # index.html.erb
format.json { render :json => @users }
end
end
# GET /users/1
# GET /users/1.json
def show
@user = User.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render :json => @user }
end
end
# GET /users/new
# GET /users/new.json
def new
@user = User.new
respond_to do |format|
format.html # new.html.erb
format.json { render :json => @user }
end
end
# GET /users/1/edit
def edit
@user = User.find(params[:id])
end
# POST /users
# POST /users.json
def create
@user = User.new(params[:user])
respond_to do |format|
if @user.save
format.html { redirect_to @user, :notice => 'User was successfully created.' }
format.json { render :json => @user, :status => :created, :location => @user }
else
format.html { render :action => "new" }
format.json { render :json => @user.errors, :status => :unprocessable_entity }
end
end
end
# PUT /users/1
# PUT /users/1.json
def update
@user = User.find(params[:id])
respond_to do |format|
if @user.update_attributes(params[:user])
format.html { redirect_to @user, :notice => 'User was successfully updated.' }
format.json { head :ok }
else
format.html { render :action => "edit" }
format.json { render :json => @user.errors, :status => :unprocessable_entity }
end
end
end
# DELETE /users/1
# DELETE /users/1.json
def destroy
@user = User.find(params[:id])
@user.destroy
respond_to do |format|
format.html { redirect_to users_url }
format.json { head :ok }
end
end
end
@doememeier

This comment has been minimized.

Copy link

@doememeier doememeier commented Apr 26, 2013

thx for the example helped me a lot!

@toebbel

This comment has been minimized.

Copy link

@toebbel toebbel commented Jun 3, 2015

thanks 👍

@vixChoo

This comment has been minimized.

Copy link

@vixChoo vixChoo commented Aug 25, 2018

thank you so much!!!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.