レイルに乗ってみた〜 目次
書籍『Rails of Ruby on Rails ~Case of LOCUSANDWONDERS.COM~』で勉強してみた(P134〜)
商品一覧が面を作成 コントローラ作成
G5:~/work/locus username$ script/generate controller products index show
exists app/controllers/
exists app/helpers/
create app/views/products
exists test/functional/
create app/controllers/products_controller.rb
create test/functional/products_controller_test.rb
create app/helpers/products_helper.rb
create app/views/products/index.html.erb
create app/views/products/show.html.erb
Loaded suite script/generate
Started
Finished in 0.000564 seconds.
0 tests, 0 assertions, 0 failures, 0 errors[書籍に間違い発見]書籍P134の下の画像が間違ってる?上と同じだし。
G5:~/work/locus username$ script/generate controller genre show
exists app/controllers/
exists app/helpers/
create app/views/genre
exists test/functional/
create app/controllers/genre_controller.rb
create test/functional/genre_controller_test.rb
create app/helpers/genre_helper.rb
create app/views/genre/show.html.erb
Loaded suite script/generate
Started
Finished in 0.015155 seconds.
0 tests, 0 assertions, 0 failures, 0 errors
コントローラ修正
書籍と同じなので略。
ルート定義を追加する
書籍と同じなので略。
レイアウトを修正する
書籍に書いてある /work/locus/app/views/layouts/products.html.erb が新しくできてなかったので新規作成。
なんでかな?
コードは書籍と同じなので略。
それから /work/locus/app/views/layouts/entries.html.erb を書籍の通り修正。
ビューを修正
書籍と同じなので略。
ブラウザで確認
http://localhost:3000/

エラーになったので
/work/locus/config/routes.rbに
map.resources :products
を追加してみた。読み飛ばした?
それから下のほうに
# You can have the root of your site routed with map.root -- just remember to delete public/index.html. # map.root :controller => "welcome"
と書いてあったのでpublic/index.htmlを削除してみた。
そしてまたブラウザで確認 http://localhost:3000/

今度は表示された。
商品詳細画面の作成 コントローラ修正
/work/locus/app/controllers/products_controller.rbを修正。
def show @product = Product.find(params[:id]) end
ビューを追加
/work/locus/app/views/products/show.html.erbを修正。
<div id="content">
<%= image_tag(url_for_file_column(
@product, :cover_image)) if @product.cover_image %>
<div id="item">
<h1><%=h @product.title %></h1>
<%= image_tag(
url_for_file_column(@product, :image, 'middle')) %>
<div id="description">
<%= simple_format(h(@product.description)) %>
</div>
<div class="detail">
<p>価格: <%= h @product.price %>(税込)</p>
<%= link_to 'カートに入れる', '#' %>
</div>
</div>
</div>
ブラウザで確認
http://localhost:3000/
商品をクリックで

商品名や写真を適当にいれたので見難くなっちゃった(||゚Д゚)
メモ
- 書籍の間違いなのか、自分の見落としなのかよく分らなくなってきた(´・ω・`)