投稿データ削除時の確認ダイアログ
アクセス時にページの一番下まで自動スクロール
プロフィール画像を丸形に変更
バリデーション
Flashメッセージ
ルーティングのルート制限
特定のルートに対し認証済みユーザーのみアクセスを許す
cd talkapp
app/views/layouts/application.html.erb次のコード追加
削除ボタンを押すと確認ダイヤログが現れます
confirm button before running deleteing routine from website
http://stackoverflow.com/questions/19022176/confirm-button-before-running-deleting-routine-from-website
app/views/layouts/application.html.erbのscriptの箇所に次のコード追加
...
windoew.scrollto(0,document.body.scrollHeight);
...
多くの投稿が存在している場合、一番下までスクロールした状態で表示
Scroll Automatically to the Buttiom of the Page
http://stackoverflow.com/questions/11715646/scroll-automatically-to-the-bottom-of-the-page
app/views/layout/_navbar.html.erbで次のコードのようにnavbar-fixed-topを追加
...
app/assets/stylesheets/mystyle.cssのbodyに次のコードを追加
...
padding-top: 70px;
...
一番下までスクロールされてもNavbarは上部に存在し続ける
app/views/posts/index.html.erbの次のコードの箇所を次のようにimg-circleを追加
...
<img class="media-object img-circle" scr="<%=
...
プロフィール画像が丸形に変わる
app/models/usr.rbに次のコードを追加
createとupdateのバリデーションの違いはcreateの場合はすべてのフィールドを必須にしているのに対し、
updateの場合は全て必須にしていない。
...
validation :name,presence: true, uniqeness: { case_senseitive: false }, on:
...
app/models/post.rbに次のコードを追加
...
validates :message, presence: true
...
app/controllers/users_controller.rbのnewを次のコードのように変更
バリデーションのエラーメッセージを保存するために必要
...
def new
#xxxx
@user = User.new
end
...
app/controllers/posts_controller.rbに次のコードに変更
バリデーションのエラーメッセージを保存するために必要
def new
#xxxx
@post = Post.new
end
app/views/users/new.html.erbとedit.html.erbに次のコード追加
...
<%= render "layout/error_message", object: @user %>
...
...
<%= render "layout/error_message", object: @user %>
...
app/views/posts/new.html.erbで次のコードのようにnavbar-fixed-topを追加
...
<%= render "layout/error_message", object: @post %>
...
app/views/layoutにerror_message.html.erbを作成し、次のコードを追加
<% if object.errors.any? %>
Error
- <%= msg %>
<% objevt.error.full message.each do |msg| %>
<% end %>
<% end %>
ブラウザ上の表示
何も入力せず登録ボタンを押すとエラー
app/controllers/sessions_controller.rbに次のコード追加
Flashメッセージ設定
...
falsh[:success] = "You have successfully logged in"
...
flash[:falure] = "There was a problem with your email or password"
...
app/viewslayoutに_message.html.erbを作成し、次のコードを追加
<% flash.each do |key,value| %>
<% if key == "success" %>
Success:<%= value %>
<% elseif key == "falure" %>
Failure<%= value %>
<% end %>
<% end %>
app/views/layouts/application.html.erbに次のコード追加
...
<%= render "layouts/message" %>
...
ブラウザ上の表示
ログインに成功すると上部にメッセージが表示されます
config/routes.rbの次のコードを次に変更
リソースベースのルート定義に対し、使用するアクションを限定しています。
...
resources :users, only: [:new, :create, :edit, :update]
resources :post, only: [:index, :new, :create]
...
rake routesの結果
限定されたアクションだけルートに登録されています。
ルーティングの制限
app/helpers/sessions_helper.rbに次のコードに追加
ユーザーがログインしていない場合はログイン画面に転送するヘルパーメソッドを追加します。
logged_in_userヘルパーメソッド
def logged_in_user
unless logged_in?
redirect_to login_path
end
end
app/cotrollers/users_controller.rbに次のコードに追加
editとupdateは認証済みユーザーのみがアクセス許可
ログインしていないユーザが保護されたページにアクセスしようとした際にログインページに転送する方法
...
before_action :logged_in_user, only: [:edit, :update]
...
app/contorllers/posts_controller.rbに次のコードに変更に次のコードを追加
PostControllerの全アクションは認証済みユーザーのみがアクセス許可
...
before_action :logged_in_user
...
ログインしていない状態でhttp://localhost:3000/posts
ログイン画面に転送