komposable / komponent

An opinionated way of organizing front-end code in Ruby on Rails, based on components
http://komponent.io
MIT License
426 stars 31 forks source link

Component helper methods aren't included in remote renders #89

Closed devonwalshe closed 6 years ago

devonwalshe commented 6 years ago

A component that uses a helper method defined in its associated helper module returns a NoMethodError, when a controller action is called via ajax with a remote: true call.

# /components/foo_component/foo_component.html.haml 
=button_to foo_helper("foo"), foo_path, remote: true
# /components/foo_component/foo_component.rb
Module FooComponent
    extend ComponentHelper
    def foo_helper(text)
       return text
    end
end
# /app/controllers/foos_controller.rbs
...
def foo
    render partial: 'components/foo_component/foo_component'
end

If the helper method is included in the app/helpers/foo.rb it works just fine.

App is on komponent 1.1.4 & Rails 5.1.

Spone commented 6 years ago

Hi @devonwalshe can you try with version 2.0.0.pre.1 of Komponent? see below

florentferry commented 6 years ago

Hello @devonwalshe,

Nothing wrong here, you are using render partial from Rails, not component helper from Komponent. Only component helper load and inject component module into view context.

You can do what you want with:

def foo
  render html: helpers.component("foo")
end
devonwalshe commented 6 years ago

Cheers @florentferry, that indeed worked. Would agree it would be useful if this was documented. Many thanks!

Spone commented 6 years ago

I documented this in the cookbook: https://github.com/komposable/komponent/wiki#cookbook

EDIT: it has been documented in the README as well