いつもお世話になっております。
Draperを使いモデルをデコレーションするのは出来ました。
次に関連するモデルのデコレーションを試みましたが
そのモデルを参照している箇所でNoMethodErrorが出ました。
以下のような関係の時、
部門(親)→department
課(子)→section
課モデルのshowビューで
部門名を出力したいです。
分かる方どうか宜しくお願い致します。
SectionsController
ruby
1class SectionsController < ApplicationController 2 3 def show 4 #@section = Section.find(params[:id]) 5 @section = SectionDecorator.decorate_collection(Section.find(params[:id])) 6 end 7 8end
SectionDecorator
Ruby
1class SectionDecorator < Draper::Decorator 2 delegate_all 3 4 decorates_association :department 5 6end 7 8
DepartmentDecorator
ruby
1class DepartmentDecorator < Draper::Decorator 2 def hoge 3 model.name + "テスト。" 4 end 5end
show.html.erb
ruby
1 2 3 <div class="container-fluid"> 4 <div class="row"> 5 <div class="col-xs-4 col-xs-offset-2" > 6 <h1>課の詳細</h1> 7 </div> 8 9 <div class="col-xs-8 col-xs-offset-2" > 10 <div class="box"> 11 12 <div class="table-responsive"> 13 <table class="table table-hover"> 14 15 <tr> 16 <th>部門</th> 17 <td><!-- この行でNoMethodError --><%= @section.department.hoge %></td> 18 </tr> 19 <tr> 20 <th>課</th> 21 <td><%= @section.sec_name %></td> 22 </tr> 23 <tr> 24 <th>使用中</th> 25 <td><%= @section.use %></td> 26 </tr> 27 <tr> 28 <th>表示順</th> 29 <td><%= @section.sec_order %></td> 30 </tr> 31 32 </table> 33 </div><!-- /div class="table-responsive" --> 34 35 36 37 <p><%= link_to "このデータを編集する", [:edit, @section] %></p> 38 </div> 39 40 41 42 43 </div> 44 </div> 45 </div><!-- /container --> 46 47 48 49
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/04/12 08:28
2017/04/12 09:02