-
Notifications
You must be signed in to change notification settings - Fork 0
#1 ユーザー登録の実装 #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
#1 ユーザー登録の実装 #6
Changes from all commits
aca287f
f226c93
d77ec73
423f7d7
8907c17
80950b8
09c4b91
1aacdf0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| # Place all the behaviors and hooks related to the matching controller here. | ||
| # All this logic will automatically be available in application.js. | ||
| # You can use CoffeeScript in this file: http://coffeescript.org/ |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| // Place all the styles related to the Users controller here. | ||
| // They will automatically be included in application.css. | ||
| // You can use Sass (SCSS) here: http://sass-lang.com/ |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| class UsersController < ApplicationController | ||
| def new | ||
| @user = User.new | ||
| end | ||
|
|
||
| def create | ||
| @user = User.new(user_params) | ||
| if @user.save | ||
| #登録成功時のアクション | ||
| flash[:success] = "ユーザー登録が完了しました!" | ||
| redirect_to @user | ||
| else | ||
| #登録失敗時のアクション | ||
| render "new" | ||
| end | ||
| end | ||
|
|
||
| def show | ||
| @user = User.find(params[:id]) | ||
| end | ||
|
|
||
| private | ||
|
|
||
| #ストロングパラメーター | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 自分用かもしれませんが、上のコメントもそうですが、コメントも保守の対象なので、何を残すかは精査したいところですね(修正不要です) |
||
| def user_params | ||
| params.require(:user).permit(:user_name, :email, :password, | ||
| :password_confirmation) | ||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| module UsersHelper | ||
|
|
||
| #ユーザーからGravatarを返す | ||
| def gravatar_for(user, size: 80) | ||
| gravatar_id = Digest::MD5::hexdigest(user.email.downcase) | ||
| gravatar_url = "https://secure.gravatar.com/avatar/#{gravatar_id}?s=#{size}" | ||
| image_tag(gravatar_url, alt: user.user_name, class: "gravatar") | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| class User < ApplicationRecord | ||
| before_save { email.downcase! } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| VALID_USER_NAME_REGEX = /\A(\w+)\z/i | ||
| VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z\d\-]+)*\.[a-z]+\z/i | ||
| validates :user_name, presence: true, | ||
| length: { maximum: 30 }, | ||
| uniqueness: { case_sensitive: false}, | ||
| format: { with: VALID_USER_NAME_REGEX } | ||
| validates :email, presence: true, | ||
| length: { maximum: 255 }, | ||
| uniqueness: { case_sensitive: false}, | ||
| format: { with: VALID_EMAIL_REGEX } | ||
| has_secure_password | ||
| validates :password, presence: true, length: { minimum: 6 } | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| <% if @user.errors.any? %> | ||
| <div class="error_explanation"> | ||
| <div class="alert alert-danger"> | ||
| <%= "入力エラー:#{@user.errors.count}項目" %> | ||
| </div> | ||
| <ul> | ||
| <% @user.errors.full_messages.each do |msg| %> | ||
| <li><%= msg %></li> | ||
| <% end %> | ||
| </ul> | ||
| </div> | ||
| <% end %> | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 修正するまでもないですが、パーシャルから、アサインされた変数を参照する場合、直接インスタンス変数を呼ばずに、 |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| <div class="center jumbotron"> | ||
| <h1>Knowledge Stocker</h1> | ||
| <h2>効率的かつ意欲的な学習体験を。</h2> | ||
| <%= link_to "新規登録", "#", class: "btn btn-lg btn-primary" %> | ||
| <%= link_to "新規登録", signup_path, class: "btn btn-lg btn-primary" %> | ||
| </div> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| <%= provide(:title, "新規登録") %> | ||
| <h1>新規登録</h1> | ||
|
|
||
| <div class="row"> | ||
| <div class="col-md-6 col-md-offset-3"> | ||
| <%= form_for(@user, url: signup_path) do |f| %> | ||
| <%= render "shared/error_messages" %> | ||
|
|
||
| <%= f.label :user_name, "ユーザー名" %> | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i18nを設定すれば、 |
||
| <%= f.text_field :user_name, class: "form_control" %> | ||
|
|
||
| <%= f.label :email, "メールアドレス" %> | ||
| <%= f.email_field :email, class: "form_control" %> | ||
|
|
||
| <%= f.label :password, "パスワード" %> | ||
| <%= f.password_field :password, class: "form_control" %> | ||
|
|
||
| <%= f.label :password_confirmation, "パスワード(確認)" %> | ||
| <%= f.password_field :password_confirmation, class: "form_control" %> | ||
|
|
||
| <%= f.submit "登録する", class: "btn btn-primary" %> | ||
| <% end %> | ||
| </div> | ||
| </div> | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| <% provide(:title, @user.user_name) %> | ||
| <h1>ユーザー情報</h1> | ||
| <div class="row"> | ||
| <aside class="col-md-4"> | ||
| <section class="user_info"> | ||
| <%= gravatar_for @user, size: 180 %> | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| <div class="user_name"><%= @user.user_name %></div> | ||
| </section> | ||
| </aside> | ||
| </div> | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,4 +2,7 @@ | |
| root 'static_pages#home' | ||
| get '/about', to: 'static_pages#about' | ||
| get '/contact', to: 'static_pages#contact' | ||
| get '/signup' , to: 'users#new' | ||
| post '/signup', to: 'users#create' | ||
| resources :users | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 修正しなくてもよいのですが、routingは、controllerが実装されてはじめて使えるようになるので、コミット順序からしたら、コントローラの実装の後でよいかと思います。 |
||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| class CreateUsers < ActiveRecord::Migration[5.1] | ||
| def change | ||
| create_table :users do |t| | ||
| t.string :user_name | ||
| t.string :email | ||
| t.string :password_digest | ||
|
|
||
| t.timestamps | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| end | ||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| class AddIndexToUsers < ActiveRecord::Migration[5.1] | ||
| def change | ||
| add_index :users, :user_name, unique: true | ||
| add_index :users, :email, unique: true | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| # This file is auto-generated from the current state of the database. Instead | ||
| # of editing this file, please use the migrations feature of Active Record to | ||
| # incrementally modify your database, and then regenerate this schema definition. | ||
| # | ||
| # Note that this schema.rb definition is the authoritative source for your | ||
| # database schema. If you need to create the application database on another | ||
| # system, you should be using db:schema:load, not running all the migrations | ||
| # from scratch. The latter is a flawed and unsustainable approach (the more migrations | ||
| # you'll amass, the slower it'll run and the greater likelihood for issues). | ||
| # | ||
| # It's strongly recommended that you check this file into your version control system. | ||
|
|
||
| ActiveRecord::Schema.define(version: 20170706211140) do | ||
|
|
||
| create_table "users", force: :cascade do |t| | ||
| t.string "user_name" | ||
| t.string "email" | ||
| t.string "password_digest" | ||
| t.datetime "created_at", null: false | ||
| t.datetime "updated_at", null: false | ||
| t.index ["email"], name: "index_users_on_email", unique: true | ||
| t.index ["user_name"], name: "index_users_on_user_name", unique: true | ||
| end | ||
|
|
||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| require 'test_helper' | ||
|
|
||
| class UsersControllerTest < ActionDispatch::IntegrationTest | ||
| test "should get new" do | ||
| get signup_path | ||
| assert_response :success | ||
| end | ||
|
|
||
| end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
修正不要ですが、このオプション指定は下記との勘違い?(あ、でもjrubyがないか)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
あーすみませんデフォルトですね。失礼。