Yêu cầu thg 6 20, 2019 10:10 SA 214 0 2
  • 214 0 2
0

Hỏi về kế thừa trong controller Rails

Chia sẻ
  • 214 0 2

E có sử dụng một Gem và gem đó yêu cầu kế thừa lại controller của nó

class NotificationsController < ActivityNotification::NotificationsController

trong khi e vẫn muốn kế thừa từ BaseController thì làm thế nào ạ.

class NotificationsController < BaseController

E xin cảm ơn!

2 CÂU TRẢ LỜI


Đã trả lời thg 6 21, 2019 4:24 SA
Đã được chấp nhận
+5

Class ActivityNotification::NotificationsController kế thừa ActivityNotification.config.parent_controller.constantize https://github.com/simukappu/activity_notification/blob/master/app/controllers/activity_notification/notifications_controller.rb#L3

ActivityNotification.config.parent_controller có giá trị default là ApplicationController https://github.com/simukappu/activity_notification/blob/master/lib/activity_notification/config.rb#L159

Nếu bạn muốn thay đổi parent class này thì bạn có thể config trong initializer mà gem này tạo ra

config/initializers/activity_notification.rb

ActivityNotification.configure do |config|
  ...
  config.parent_controller = 'BaseController'
  ...
end

Khi đó thì bạn sẽ có quan hệ

NotificationsController < ActivityNotification::NotificationsController < BaseController
Chia sẻ
Đã trả lời thg 6 20, 2019 3:45 CH
+4

bạn cứ dùng cách 1 nhé. vì bản thân controller của gem đó cũng đã kế thừa BaseController rồi

Chia sẻ
Avatar Nam @Black.Box
thg 6 21, 2019 3:19 SA
Viblo
Hãy đăng ký một tài khoản Viblo để nhận được nhiều bài viết thú vị hơn.
Đăng kí