Created
August 8, 2015 21:11
-
-
Save Yama-to/376c772f65ff75c912cb to your computer and use it in GitHub Desktop.
あなたがRails触る人なら見ておきたい「体系的な」豆知識 ref: http://qiita.com/Yama-to/items/93cd8b3a8be67b09e353
This file contains hidden or 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
/* | |
*= require_tree . | |
*= require_self | |
*/ |
This file contains hidden or 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
<!DOCTYPE html> | |
<html lang="ja"> | |
<head> | |
<!-- 中略 --> | |
<%= stylesheet_link_tag "application", media: "all", "data-turbolinks-track" => true %> | |
<%= javascript_include_tag "application", "data-turbolinks-track" => true %> | |
</head> |
This file contains hidden or 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
//= require jquery | |
//= require jquery_ujs | |
//= require turbolinks | |
//= require_tree . |
This file contains hidden or 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
$ Book.where('title = ? AND author = ?', params[:title], params[:author]) | |
Book Load (0.3ms) SELECT `books`.* FROM `books` WHERE (title = '白鯨' AND author = 'メルヴィル') |
This file contains hidden or 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
$ Book.where('title = :title AND author = :author', title: params[:title], author: params[:author]) |
This file contains hidden or 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 change | |
create_table :books do |t| | |
t.string :title, limit: 9, null: false | |
t.decimal :price, precision: 5, scale: 0 | |
t.string :publisher, default: 'HBS' | |
t.timestamps | |
end | |
end |
This file contains hidden or 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
$ rails g migration CreateJoinTableAuthorsAndBooks authors books | |
# invoke active_record | |
# create db/migrate/20150728175239_create_join_table_authors_and_books.rb |
This file contains hidden or 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 CreateJoinTableAuthorsAndBooks < ActiveRecord::Migration | |
def change | |
create_join_table :authors, :books do |t| | |
# t.index [:author_id, :book_id] | |
# t.index [:book_id, :author_id] | |
end | |
end | |
end |
This file contains hidden or 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
<%= ... -%> || <% ... -%> |
This file contains hidden or 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 false %> | |
<%= この行はコメントアウトになります %> | |
<% end %> | |
⑤ | |
<% | |
=begin | |
%> | |
<%= この間の部分は全てコメントになります %> | |
<% | |
=end | |
%> |
This file contains hidden or 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
# @text = | |
# 本日の天気 | |
# | |
# 関東地区の天気をお知らせします。 | |
# 明日は全体的に猛暑日となる見込みです。 | |
# 以上です。 | |
<%= simple_format(@text) %> | |
# => 出力結果 | |
<p>本日の天気</p> | |
<p> | |
関東地区の天気をお知らせします。<br /> | |
明日は全体的に猛暑日となる見込みです。<br /> | |
以上です。 | |
</p> |
This file contains hidden or 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_if @user.present, "MY PAGE", user_path(@user) %> | |
<%= link_to_unless_current "HOME", root_path %> |
This file contains hidden or 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 '削除', book, method: :delete, data: {confirm: '本当によろしいですか?'} %> |
This file contains hidden or 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
<%= f.text_filed :comment, required %> |
This file contains hidden or 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 Comment < ActiveRecord::Base | |
validates :text, presence: true | |
end |
This file contains hidden or 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
$ Book.where.not(title: "白鯨") | |
Book Load (0.3ms) SELECT `books`.* FROM `books` WHERE (`books`.`title` != '白鯨') |
This file contains hidden or 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 ReviewsController < ApplicationController | |
def create | |
sleep 3 | |
Review.create(review_params) | |
end | |
#- (中略) -# | |
end |
This file contains hidden or 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
<%= form_for @review do |f| %> | |
<!-- (中略) --> | |
<%= f.submit "投稿する", data: {disable_with: "処理中..."} %> | |
<% end %> |
This file contains hidden or 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 BooksController < ApplicationController | |
before_action :start_log | |
after_action :end_log | |
def create | |
Book.create(book_params) | |
end | |
private | |
def start_log | |
logger.debug("[START: #{Time.now.to_s}]") | |
end | |
def end_log | |
logger.debug("[END: #{Time.now.to_s}]") | |
end | |
end |
This file contains hidden or 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 BooksController < ApplicationController | |
around_action :output_log | |
def create | |
Book.create(book_params) | |
end | |
private | |
def output_log | |
logger.debug("[START: #{Time.now.to_s}]") | |
yield #アクションを実行 | |
logger.debug("[END: #{Time.now.to_s}]") | |
end | |
end |
This file contains hidden or 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 BooksController < ApplicationController | |
#--(中略)--# | |
private | |
def set_ip | |
@ip = request.remote_ip | |
end | |
end |
This file contains hidden or 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 BooksController < ApplicationController | |
def show | |
@book = Book.find(params[:id]) | |
if @book.status == '1' | |
render file: Rails.root.join('public/404.html'), status: :not_found and return | |
end | |
render 'top/show' | |
end | |
end |
This file contains hidden or 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 Application < Rails::Application | |
# Do not share helper methods | |
config.action_controller.include_all_helpers = false | |
end |
This file contains hidden or 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 ApplicationController < ActionController::Base | |
before_action :digest_auth | |
private | |
def digest_auth | |
members = { '<ユーザー名>' => '<パスワード>' } | |
authenticate_or_request_with_http_digest('<アプリケーション名>') do |name| | |
members[name] | |
end | |
end | |
end |
This file contains hidden or 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
$ Book.all.select(:id, :title) | |
Book Load (0.3ms) SELECT `books`.`id`,`books`.`title` FROM `books` | |
#=> [#<Book:0x007fb5ea28a4b8 id: 2, title: "白鯨">, | |
#<Book:0x007fb5ea28a1c0 id: 3, title: "そして">] |
This file contains hidden or 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
$ rake assets:precompile |
This file contains hidden or 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
$ Book.all.select(:id, :title).pluck(:title) | |
Book Load (0.3ms) SELECT `books`.`title` FROM `books` | |
#=> ["白鯨", "そして"] |
This file contains hidden or 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
# Set Time.zone default to the specified zone and make Active Record auto-convert to this zone. | |
# Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC. | |
# config.time_zone = 'Central Time (US & Canada)' | |
#=> | |
config.time_zone = 'Tokyo' |
This file contains hidden or 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
$ rails c | |
Loading development environment (Rails 4.2.1) | |
[1] pry(main)> |
This file contains hidden or 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
$ rails c --sandbox | |
Loading development environment in sandbox (Rails 4.2.1) | |
Any modifications you make will be rolled back on exit | |
[1] pry(main)> |
This file contains hidden or 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 Users < ActiveRecord::Base | |
# TODO: set sign-in validations | |
# FIXME: fix class name as singular | |
end |
This file contains hidden or 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
$ rake notes | |
#=> | |
app/models/user.rb: | |
* [2] [TODO] set sign-in validations | |
* [3] [FIXME] fix class name as singular | |
$ rake notes:fixme | |
#=> | |
app/models/user.rb: | |
* [3] [FIXME] fix class name as singular |
This file contains hidden or 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 :books do | |
get :newest, on: :collection | |
get :archive, on: :member | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment