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

回答編集履歴

5

タイトルを付けました。

2020/07/25 07:08

投稿

DelphiumG
DelphiumG

スコア3

answer CHANGED
@@ -1,4 +1,4 @@
1
- ###一部修正しました
1
+ ###一部修正しました(2)
2
2
 
3
3
  今回のようにnamespace使用時にcontroller_nameでは"top"のみが返るのでnamespaceまで取得できるようにcontroller_pathに変更しました。
4
4
  この場合underscoreも不要になりますので削除いたしました。

4

underscoreを削除

2020/07/25 07:08

投稿

DelphiumG
DelphiumG

スコア3

answer CHANGED
@@ -1,6 +1,7 @@
1
1
  ###一部修正しました
2
2
 
3
3
  今回のようにnamespace使用時にcontroller_nameでは"top"のみが返るのでnamespaceまで取得できるようにcontroller_pathに変更しました。
4
+ この場合underscoreも不要になりますので削除いたしました。
4
5
 
5
6
  **(原因)**
6
7
   コントローラの名前はcontroller_name で取得できますが namespace までは取得できないので、Staff::TopController の場合も Admin::TopController の場合も同じ top が返ってくる。
@@ -12,7 +13,7 @@
12
13
  layout :set_layout
13
14
  private
14
15
  def set_layout
15
- if controller_path.underscore.match(%r{\A(staff|admin|customer)/})#訂正箇所
16
+ if controller_path.match(%r{\A(staff|admin|customer)/})#訂正箇所
16
17
  Regexp.last_match[1]
17
18
  else
18
19
  "customer"

3

byebugを削除

2020/07/25 07:04

投稿

DelphiumG
DelphiumG

スコア3

answer CHANGED
@@ -12,13 +12,10 @@
12
12
  layout :set_layout
13
13
  private
14
14
  def set_layout
15
- byebug
16
15
  if controller_path.underscore.match(%r{\A(staff|admin|customer)/})#訂正箇所
17
16
  Regexp.last_match[1]
18
- byebug
19
17
  else
20
18
  "customer"
21
- byebug
22
19
  end
23
20
  end
24
21
  end

2

(訂正)今回のようにnamespace使用時にcontroller_nameでは"top"のみが返るのでcontroller_pathに変更

2020/07/25 03:18

投稿

DelphiumG
DelphiumG

スコア3

answer CHANGED
@@ -1,1 +1,25 @@
1
+ ###一部修正しました
2
+
3
+ 今回のようにnamespace使用時にcontroller_nameでは"top"のみが返るのでnamespaceまで取得できるようにcontroller_pathに変更しました。
4
+
5
+ **(原因)**
6
+  コントローラの名前はcontroller_name で取得できますが namespace までは取得できないので、Staff::TopController の場合も Admin::TopController の場合も同じ top が返ってくる。
7
+
8
+ **(訂正後)**
9
+ ```
10
+ class ApplicationController < ActionController::Base
11
+
12
+ layout :set_layout
13
+ private
14
+ def set_layout
15
+ byebug
16
+ if controller_path.underscore.match(%r{\A(staff|admin|customer)/})#訂正箇所
1
- ※解決内容を2重投稿してしましましたのでこちらは削除しました。
17
+ Regexp.last_match[1]
18
+ byebug
19
+ else
20
+ "customer"
21
+ byebug
22
+ end
23
+ end
24
+ end
25
+ ```

1

同じ内容を2回 投稿してしまったため、こちらの内容を削除

2020/07/25 03:14

投稿

DelphiumG
DelphiumG

スコア3

answer CHANGED
@@ -1,21 +1,1 @@
1
- unhappychoice様に提案いただいたコードそのままだと下記のエラーが発生いたしましたので
2
-
3
- ![イメージ説明](ed53e414ef3597078cc441de59c5aa23.jpeg)
4
-
5
- 下記のように
6
- controller → controller_name
7
- に変更したろエラーが完全に解消いたしました。
1
+ ※解決内容を2重投稿てしましましのでちらは削除しました。
8
- ```
9
- class ApplicationController < ActionController::Base
10
-
11
- layout :set_layout
12
- private
13
- def set_layout
14
- if controller_name.underscore.match(%r{\A(staff|admin|customer)/})
15
- Regexp.last_match[1]
16
- else
17
- "customer"
18
- end
19
- end
20
- end
21
- ```