Skip to content

Instantly share code, notes, and snippets.

@Yama-to
Created August 8, 2015 21:11
Show Gist options
  • Save Yama-to/376c772f65ff75c912cb to your computer and use it in GitHub Desktop.
Save Yama-to/376c772f65ff75c912cb to your computer and use it in GitHub Desktop.
あなたがRails触る人なら見ておきたい「体系的な」豆知識 ref: http://qiita.com/Yama-to/items/93cd8b3a8be67b09e353
/*
*= require_tree .
*= require_self
*/
<!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>
//= require jquery
//= require jquery_ujs
//= require turbolinks
//= require_tree .
$ Book.where('title = ? AND author = ?', params[:title], params[:author])
Book Load (0.3ms) SELECT `books`.* FROM `books` WHERE (title = '白鯨' AND author = 'メルヴィル')
$ Book.where('title = :title AND author = :author', title: params[:title], author: params[:author])
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
$ rails g migration CreateJoinTableAuthorsAndBooks authors books
# invoke active_record
# create db/migrate/20150728175239_create_join_table_authors_and_books.rb
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
<%= ... -%> || <% ... -%>
<%# 一般的なコメントアウトです %>
<%#= これもコメントアウトです
この行もコメントアウトです
複数行に渡りコメントアウトできます %>
<% これはコメントではありません
  # この行がコメントになります %>
<% if false %>
<%= この行はコメントアウトになります %>
<% end %>
<%
=begin
%>
<%= この間の部分は全てコメントになります %>
<%
=end
%>
# @text =
# 本日の天気
#
# 関東地区の天気をお知らせします。
# 明日は全体的に猛暑日となる見込みです。
# 以上です。
<%= simple_format(@text) %>
# => 出力結果
<p>本日の天気</p>
<p>
関東地区の天気をお知らせします。<br />
明日は全体的に猛暑日となる見込みです。<br />
以上です。
</p>
<%= link_to_if @user.present, "MY PAGE", user_path(@user) %>
<%= link_to_unless_current "HOME", root_path %>
<%= link_to '削除', book, method: :delete, data: {confirm: '本当によろしいですか?'} %>
<%= f.text_filed :comment, required %>
class Comment < ActiveRecord::Base
validates :text, presence: true
end
$ Book.where.not(title: "白鯨")
Book Load (0.3ms) SELECT `books`.* FROM `books` WHERE (`books`.`title` != '白鯨')
class ReviewsController < ApplicationController
def create
sleep 3
Review.create(review_params)
end
#- (中略) -#
end
<%= form_for @review do |f| %>
<!-- (中略) -->
<%= f.submit "投稿する", data: {disable_with: "処理中..."} %>
<% end %>
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
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
class BooksController < ApplicationController
#--(中略)--#
private
def set_ip
@ip = request.remote_ip
end
end
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
class Application < Rails::Application
# Do not share helper methods
config.action_controller.include_all_helpers = false
end
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
$ 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: "そして">]
$ rake assets:precompile
$ Book.all.select(:id, :title).pluck(:title)
Book Load (0.3ms) SELECT `books`.`title` FROM `books`
#=> ["白鯨", "そして"]
# 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'
$ rails c
Loading development environment (Rails 4.2.1)
[1] pry(main)>
$ 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)>
class Users < ActiveRecord::Base
# TODO: set sign-in validations
# FIXME: fix class name as singular
end
$ 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
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