teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

2

説明漏れがあったので修正

2021/04/22 06:24

投稿

Homaresan
Homaresan

スコア15

title CHANGED
File without changes
body CHANGED
@@ -52,15 +52,33 @@
52
52
  end
53
53
  ```
54
54
  ### 試したこと
55
+ ```
55
- 上記、前提・実現したいことに記載のfruit_decorator.rbですが、Fruitはclassではなくmoduleなので当然full_nameメソッドは呼ばれませんでした。
56
+ app/decorators/concerns/fruit_decorator.rb
56
57
 
58
+ module FruitDecorator
59
+ extend ActiveSupport::Concern
60
+
61
+ def full_name
57
- もしやるなら下記のようにファイルを作成し、それぞれのファイル内でfull_nameメソッドを書けば動くと思いますが、かえってファイル数が増えあまりベストな方法では無いと感じました。
62
+ "#{first_name} #{last_name}"
63
+ end
64
+ end
58
65
  ```
59
- app/decorators/apple_decorator.rb
60
- app/decorators/grape_decorator.rb
61
- app/decorators/banana_decorator.rb
66
+ 上記、前提・実現したいことに記載のようにfruit_decorator.rbを作成しfruit.rbからfruit_decorator.rbにfull_nameメソッドを移しました。
67
+ その後、下記のようにviewでfull_nameメソッドを使用しましたが、エラーが出てしまいdecorator層に定義したメソッドを呼び出すことができませんでした。
62
68
  ```
69
+ app/views/apples/index.html.erb
63
70
 
71
+ <% @apples.each do |apple| %>
72
+ <%= apple.full_name %><br>
73
+ <% end %>
74
+ ```
75
+ ```
76
+ エラー文
77
+
78
+ ActionView::Template::Error - undefined method `full_name'
79
+ ```
80
+
81
+
64
82
  ### 補足情報(FW/ツールのバージョンなど)
65
83
 
66
84
  * Rails 6.0.3.5

1

誤字

2021/04/22 06:24

投稿

Homaresan
Homaresan

スコア15

title CHANGED
File without changes
body CHANGED
@@ -34,21 +34,21 @@
34
34
  app/models/apple.rb
35
35
 
36
36
  class Apple < ApplicationRecord
37
- include fruit
37
+ include Fruit
38
38
  end
39
39
  ```
40
40
  ```
41
41
  app/models/grape.rb
42
42
 
43
43
  class Grape < ApplicationRecord
44
- include fruit
44
+ include Fruit
45
45
  end
46
46
  ```
47
47
  ```
48
48
  app/models/banana.rb
49
49
 
50
50
  class Banana < ApplicationRecord
51
- include fruit
51
+ include Fruit
52
52
  end
53
53
  ```
54
54
  ### 試したこと