Skip to content

Instantly share code, notes, and snippets.

@thash
Last active August 29, 2015 14:00
Show Gist options
  • Save thash/89e6ce0b83d8bda8a16f to your computer and use it in GitHub Desktop.
Save thash/89e6ce0b83d8bda8a16f to your computer and use it in GitHub Desktop.
class AddNameAndIconToUsers < ActiveRecord::Migration
def change
add_column :users, :name, :string
add_column :users, :icon, :string
end
end
$ sqlite3 db/development.sqlite3
sqlite> delete from posts;
gem 'sorcery'
$ bundle exec rails generate controller users
$ bundle exec rails generate sorcery:install
create config/initializers/sorcery.rb
generate model User --skip-migration
invoke active_record
create app/models/user.rb
invoke test_unit
create test/models/user_test.rb
create test/fixtures/users.yml
insert app/models/user.rb
insert app/models/user.rb
create db/migrate/20140423163942_sorcery_core.rb
<body>
<div id="sub">
<% if logged_in? %>
<%= image_tag current_user.icon, width: 50, height: 50 %>
<%= current_user.name %> でログイン中
<% else %>
<div id="login_form">
<%= form_tag signin_users_path, class: 'user_form' do %>
<label for="email">email:</label><%= text_field_tag :email %><br />
<label for="password">password:</label><%= password_field_tag :password %><br />
<div class="actions"><%= submit_tag 'ログイン' %></div>
<% end %>
<br />
<div style="clear: both;"></div>
もしくは
<%= link_to '新規登録', new_user_path %>
</div>
<% end %>
</div>
<div id="main">
<%= yield %>
</div>
</body>
...
<div id="sub">
<% if logged_in? %>
<%= image_tag current_user.icon, width: 50, height: 50 %>
<%= current_user.name %> でログイン中
<br /><br />
<%= link_to 'ログアウト', signout_users_path, method: :delete %>
<% else %>
...
$ bundle exec rake db:migrate
class User < ActiveRecord::Base
authenticates_with_sorcery!
end
<%= link_to '投稿一覧へ', posts_path %><br /><br />
<%= form_for @user, class: 'user_form' do |f| %>
<label for="name">name:</label> <%= f.text_field :name %><br />
<label for="email">email:</label> <%= f.text_field :email %><br />
<label for="password">password:</label> <%= f.password_field :password %><br />
<label for="icon">icon:</label> <%= f.text_field :icon %><br />
<div class="actions"><%= f.submit 'ユーザ登録' %></div>
<% end %>
$ bundle exec rails g migration AddNameAndIconToUsers
class PostsController < ApplicationController
#...
def post_params
params.require(:post).permit(:body).merge(user_id: current_user.id)
end
....
<% @posts.each do |post| %>
<tr>
<td class="icon"><%= image_tag post.user.icon, width: 20, height: 20 %></td>
<td class="name"><%= post.user.name %></td>
<td class="body">「<%= post.body %>」</td>
<td class="created_at"><%= link_to post.created_at, post %></td>
<% if logged_in? && current_user == post.user %>
<td class="destroy"><%= link_to '×', post, method: :delete, data: { confirm: 'Are you sure?' } %></td>
<% end %>
</tr>
<% end %>
# app/models/user.rb
class User < ActiveRecord::Base
authenticates_with_sorcery!
has_many :posts
end
# app/models/post.rb
class Post < ActiveRecord::Base
belongs_to :user
end
<% if logged_in? %>
<%= render 'form' %>
<% end %>
class PostsController < ApplicationController
# ...
before_action :require_login, only: [:create, :destroy]
Rwitter::Application.routes.draw do
# ...
resources :users, only: [:new, :create] do
collection do
post :signin
delete :signout
end
end
$ bundle exec rake routes | grep users
signin_users POST /users/signin(.:format) users#signin
signout_users DELETE /users/signout(.:format) users#signout
users POST /users(.:format) users#create
new_user GET /users/new(.:format) users#new
def signin
if login(params[:email], params[:password])
redirect_to root_path, notice: 'ログインしました'
else
redirect_to root_path, alert: 'ログインに失敗しました'
end
end
def signout
if logout
redirect_to root_path, notice: 'ログアウトしました'
else
redirect_to root_path, alert: 'ログアウトに失敗しました'
end
end
class SorceryCore < ActiveRecord::Migration
def change
create_table :users do |t|
t.string :email, :null => false
t.string :crypted_password, :null => false
t.string :salt, :null => false
t.timestamps
end
add_index :users, :email, unique: true
end
end
> User.first
User Load (0.2ms) SELECT "users".* FROM "users" ORDER BY "users"."id" ASC LIMIT 1
=> #<User id: 1, email: "hash@example.com", crypted_password: "$2a$10$qjnOIDTS8PwyJCnrFO3GIeY1/B57D.djWsTTJg9CRRUv...", salt: "No7XZBLgYRswjszxmjR3", created_at: "2014-04-27 11:36:17", updated_at: "2014-04-27 11:36:17", name: "Hash", icon: "http://www.gravatar.com/avatar/0c4becc8e557c4876120...">
# $ bundle exec rails g migration AddUserIdToPosts で作成
class AddUserIdToPosts < ActiveRecord::Migration
def change
add_column :posts, :user_id, :integer
end
end
$ sqlite3 db/development.sqlite3
sqlite> .schema users
CREATE TABLE "users" (
"id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
"email" varchar(255) NOT NULL,
"crypted_password" varchar(255) NOT NULL,
"salt" varchar(255) NOT NULL,
"created_at" datetime,
"updated_at" datetime,
"name" varchar(255),
"icon" varchar(255));
CREATE UNIQUE INDEX "index_users_on_email" ON "users" ("email");
class UsersController < ApplicationController
def new
@user = User.new
end
def create
user = User.new(user_params)
if user.save
redirect_to root_path, notice: 'ユーザを作成しました。左のフォームからログインしてください'
else
render new_user_path, alert: 'ユーザ登録に失敗しました'
end
end
def signin
# TODO
end
def signout
# TODO
end
private
def user_params
params.require(:user).permit(:name, :email, :password, :icon)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment