简体   繁体   中英

Rails. Is the way to access to params hash from model concern?

I need to access to params inside my concern

module UrlGenerator
  extend ActiveSupport::Concern
  def test
    params[:slug]
  end 
end

How can i do? Thanks

I need to access to params inside my [model] concern

No. This is fundamentally wrong and you must not do it.

Only the controller, nothing else, knows your params (ie, the form/CGI parameters). Maybe, if you are careful, you can allow your views (HTML templates) to know about params . The rest of your code (models, DB stuff, etc.) does not need those params. They need their own arguments, and it is the controller's job to provide them by translating the params to whatever arguments your other methods take.

Do it like below and call test method from the model with parameters :

module UrlGenerator
  extend ActiveSupport::Concern
  included do
    def test(params)
      #define method
    end
  end
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM