Last active
August 29, 2015 14:00
-
-
Save thash/89e6ce0b83d8bda8a16f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class AddNameAndIconToUsers < ActiveRecord::Migration | |
def change | |
add_column :users, :name, :string | |
add_column :users, :icon, :string | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ sqlite3 db/development.sqlite3 | |
sqlite> delete from posts; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
gem 'sorcery' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ bundle exec rails generate controller users |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
<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 %> | |
... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ bundle exec rake db:migrate |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class User < ActiveRecord::Base | |
authenticates_with_sorcery! | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%= 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 %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ bundle exec rails g migration AddNameAndIconToUsers |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PostsController < ApplicationController | |
#... | |
def post_params | |
params.require(:post).permit(:body).merge(user_id: current_user.id) | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.... | |
<% @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 %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<% if logged_in? %> | |
<%= render 'form' %> | |
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PostsController < ApplicationController | |
# ... | |
before_action :require_login, only: [:create, :destroy] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Rwitter::Application.routes.draw do | |
# ... | |
resources :users, only: [:new, :create] do | |
collection do | |
post :signin | |
delete :signout | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def signin | |
if login(params[:email], params[:password]) | |
redirect_to root_path, notice: 'ログインしました' | |
else | |
redirect_to root_path, alert: 'ログインに失敗しました' | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def signout | |
if logout | |
redirect_to root_path, notice: 'ログアウトしました' | |
else | |
redirect_to root_path, alert: 'ログアウトに失敗しました' | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
> 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..."> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# $ bundle exec rails g migration AddUserIdToPosts で作成 | |
class AddUserIdToPosts < ActiveRecord::Migration | |
def change | |
add_column :posts, :user_id, :integer | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ 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"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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