Last active
August 29, 2015 14:10
-
-
Save diskshima/ca5eedf7a7b2b83e88fe to your computer and use it in GitHub Desktop.
Rails Best Practices の警告をちゃんと考える ref: http://qiita.com/diskshima/items/a19273f14705bbdb3e14
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
<% unless @member.name.present? %> | |
名無しの権兵衛 | |
<% 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
<% unless @member.name? %> | |
名無しの権兵衛 | |
<% 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
<%= render 'member_names', members: @members %> |
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 MembersController < ApplicationController | |
: | |
def update_phone_number | |
@member = Member.find(params[:id]) | |
phone_number = phone_number_params[:phone_number] | |
@member.is_temporary = true | |
@member.phone_number = remove_non_digit(phone_number) | |
@member.save | |
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 Member < ActiveRecord::Base | |
: | |
# ロジックはモデルの中に書く | |
def update_phone_number(phone_number) | |
self.is_temporary = true | |
self.phone_number = remove_non_digit(phone_number) | |
save | |
end | |
: | |
end | |
class MembersController < ApplicationController | |
: | |
def update_phone_number | |
@member = Member.find(params[:id]) | |
phone_number = phone_number_params[:phone_number] | |
@member.update_phone_number(phone_number) | |
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
get 'posts/get_post' |
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
post 'posts/update_post' | |
get 'posts/get_post' |
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
resources :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
class CreateUsers < ActiveRecord::Migration | |
def change | |
create_table "computer" do |t| | |
t.string :name | |
t.integer :os_id | |
t.timestamps | |
end | |
add_index :users, :os_id # <= これですね | |
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
member.last_name.kana |
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 member | |
: | |
def last_name_kana | |
last_name.kana | |
end | |
: | |
end | |
member.last_name_kana |
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 show | |
@members = Member.all <%# インスタンス変数に値を設定 %> | |
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
<%= render partial: 'member_names' %> <%# ここで暗黙に @members を渡しているのと同じ %> |
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
<%# @members に依存しているけど、このファイルを開くまでは分からない %> | |
<%= @members.map { |m| m.name } %> |
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
<%= render patial: 'member_names', locals: { members: @members } %> <%# 明示的にローカル変数として渡している %> |
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
<%# ローカル変数を使う %> | |
<%= members.map { |m| m.name } %> |
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
<%= render partial: 'member_names', locals: { members: @members } %> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment