Rails 4:从Model类中获取Controller类

时间:2016-09-27 11:04:25

标签: ruby-on-rails-4

有没有简单的方法从Model类中获取Controller类名? 例如:

Invoice => InvoicesController

尝试tabalize,但它会返回"发票"

Invoice.to_s.tabalize => "invoices"

1 个答案:

答案 0 :(得分:4)

s = Invoice
s.name.pluralize + 'Controller'
=> "InvoicesController"

获取控制器类本身......

"InvoicesController".constantize
=> InvoicesController