Railsチュートリアル7章

Railsチュートリアル7章大事なところメモっときます

テストコードはRspecで書いています。

 

画像   Gravatar

Gravatarは無料のサービスで、プロフィール写真をアップロードして、指定したメールアドレスと関連付けれる。

users/show.html.erb

<% provide(:title, @user.name) %>
<div class="row">
<aside class="col-md-4">
<section class="user_info">
<h1>
<%= gravatar_for @user %>
ここ!! これでhelperメソッドを呼び出しているので、helperに行く。
<%= @user.name %>
</h1>
</section>
</aside>
</div>

users_helper.rb

module UsersHelper
# 引数で与えられたユーザーのGravatar画像を返す
def gravatar_for(user,size: 80 )
 
gravatar_id = Digest::MD5::hexdigest(user.email.downcase)
# emailをハッシュ化している
gravatar_url = "https://secure.gravatar.com/avatar/#{gravatar_id}?s=#{size}"
これが画像
image_tag(gravatar_url, alt: user.name, class: "gravatar")
end
end

 

そもそもhelperとは・・・

モデルのメソッドと何が違うの??

 

モデルのメソッドはコントローラーで使う感じ。

helperは全ビューで共通なので、ビューでも使えるという感じ。

実際に今回はshowのビューで使っている。

今のところはこういう理解です。

---------------------------------------------------------------------------------------

plurialize

勝手に複数の時にsをつけてくれたりする

例)

>> helper.pluralize(1, "error")

=> "1 error"

>> helper.pluralize(5, "error")

=> "5 errors"

 

---------------------------------------------------------------------------------------

 

flash

ユーザー登録完了後にメッセージを表示する

 

コントローラー

def create
@user = User.new(user_params)
if @user.save
flash[:success] = "Welcome to the Sample App!"
ここ!!
redirect_to @user
else
render 'new'
end
end

 

ビュー

application.html.erb

<!DOCTYPE html>
<html>
<head>
<title><%= full_title(yield(:title)) %></title>
<%= render 'layouts/rails_default' %>
<%= render 'layouts/shim' %>
</head>

<body>
<%= render 'layouts/header' %>
<div class="container">
ここ!!
<% flash.each do |message_type, message| %>
コントローラーで引数渡されてる。message_typeがsucess, messageが"Welcome to the Sample App!"
<%= content_tag(:div, message, class: "alert alert-#{message_type}") %>
<% end %>
!!
 
** 元々はこの形
<% flash.each do |message_type, message| %>
<div class="alert alert-<%= message_type %>"><%= message %></div>
                          ↑ここまでクラス
<% end %>
**
 
<%= yield %>
<%= render 'layouts/footer' %>
<%= debug(params) if Rails.env.development? %>
</div>
</body>
</html>

 

---------------------------------------------------------------------------------------

 

テストコード(Rspec)

 

require 'rails_helper'


RSpec.describe "Users", type: :system do

it 'should get new' do
visit signup_path
expect(current_path).to eq (signup_path)
end
本章はここからーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー
context 'Signup' do
it "invalid signup information" do
visit signup_path
fill_in 'Name', with:""
fill_in 'Email', with:"111@111.com"
fill_in 'Password', with: "101010"
fill_in 'Confirmation', with: "101010"
expect do
find('input[name="commit"]').click
end.to change { User.count }.by(0)

expect(page).to have_content('Sign up')
# これは違う
# expect(current_path).to eq(new_user_path)
 
render'new'はnewアクションを呼び出しているわけではない。
表示されるviewだけを切り替えている。
そのためrender'new'後のpathはnew_pathというわけではない。
expect(has_css?('.alert-danger')).to be_truthy
 
end

it "valid signup information" do
visit signup_path
fill_in 'Name', with:"111"
fill_in 'Email', with:"111@111.com"
fill_in 'Password', with: "101010"
fill_in 'Confirmation', with: "101010"
expect do
find('input[name="commit"]').click
end.to change { User.count }.by(1)

expect(has_css?('.user_info')).to be_truthy
# showページにあるCSS これなくてもいいよ

expect(page).to have_content'Welcome to the Sample App!'
application.html.erbにあるからわかりずらいけどflashの中身
 

 
end

end

 



end

 

以上7章は終わりです!