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
#app/models/product.rb | |
#必須入力チェック | |
validates :description, :image_url, :price, :title , presence: true | |
#数値属性チェック | |
validates :price , numericality: {greater_than_of_equal_to: 0.01} | |
#重複チェック | |
validates :title, uniqueness: true |
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
# encoding: utf-8 | |
# ファイルの先頭に上記のコメントを追加する。 |
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 'test_helper' | |
class ProductTest < ActiveSupport::TestCase | |
test "product attributes must not be empty" do | |
product = Product.new | |
assert product.invalid? | |
assert product.errors[:title].any? | |
assert product.errors[:description].any? | |
assert product.errors[:price].any? | |
assert product.errors[:image_url].any? |
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
RewriteEngine on | |
RewriteCond %{QUERY_STRING} PARAM=03 | |
RewriteRule ^/abc/(.*)$ /xyz1/$1 [R=301,L] | |
RewriteCond %{QUERY_STRING} PARAM=02 [OR] | |
RewriteCond %{QUERY_STRING} PARAM=04 | |
RewriteRule ^/abc/(.*)$ /xyz2/$1 [R=301,L] |
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
#HTMLのスタイルを摘要する事ができる。 | |
<%= sanitize(product.description) %> |
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
sprintf("$%0.02f",product.price) |
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
<%= button_to 'カートに入れる', line_items_path(product_id: product) %> | |
#HTML 展開イメージ | |
#<pre><code class="html"> | |
#<form action="/line_items?product_id=1" class="button_to" method="post"> | |
# <div><input type="submit" value="カートに入れる"> | |
# <input name="authenticity_token" type="hidden" value="4yQOh3nVUm6w27g/Ej1DiyWNhtVcdcQOvjDuyZeztGk="> | |
# </div> | |
#</form> | |
#</code></pre> |
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
migrationの後ろに、追加すれば項目の追加、削除ができる。 | |
add_XXX_to_TABLE | |
remove_XXX_TO_TABE | |
※XXXXの部分は無視されるらしいよ | |
#実行例 | |
#rails g migration add_quantiy_to_line_items quantity:integer | |
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 'date' | |
def each_fridays | |
friday = Date.new(2013,1,4) | |
loop do | |
yield friday | |
friday += 7 | |
end | |
end | |
fridays = enum_for(:each_fridays) |
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 count_word_from_file(path: "~/default.txt", word:"default") | |
file = File.read(path) | |
return file.split.select{|w| w==word}.size | |
end |