开发者

rails moving layout helpers in it's own module separate from application

开发者 https://www.devze.com 2023-03-18 21:58 出处:网络
My goal is to keep in application_helper only the methods that I use in the templates, otherwise my application_helper gets too long.

My goal is to keep in application_helper only the methods that I use in the templates, otherwise my application_helper gets too long.

My problem relies in the suffix Helper:

This works

# app/helpers
module ApplicationHelper
  include ApplicationLayout
end

# app/helpers/layouts
module ApplicationLayout
  def my_helper
    puts 'my_helper!'
  end
end

This doesn't

# app/helpers
module ApplicationHelper
  include ApplicationLayoutHelper
end

# app/helpers/layouts
module ApplicationLayoutHelper
  def my_helper
    puts 'my_helper!'
  end
end

The error is:

Expected app/helpers/layouts/application_layout_helper.rb to define Layouts::ApplicationLayoutHelper

So I nest it and I get:

Routing Error undefined method `sub' for nil:NilClass
开发者_如何转开发

Actually I'd like to implement (give your opinion) app/helpers/layouts/application_helper.rb but it gives the same error, so for simplicity I stated this case.

Any suggestion on how to extract helper methods for layouts? Is there any convention over configuration I could use?

Thank you


Well, I tried what you expect and it's pretty straight:

in helpers/layouts/application_layout_helper.rb

module Layouts
  module ApplicationLayoutHelper
    def my_helper
      'my_helper!' 
    end
  end
end

in helpers/application_helper.rb

module ApplicationHelper
  include Layouts::ApplicationLayoutHelper
end

Ad in my view I directly do:

<%= my_helper %>

I still don't really understand what you'll do with these methods.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号