komposable / komponent

An opinionated way of organizing front-end code in Ruby on Rails, based on components

Home Page:http://komponent.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Component helper methods aren't included in remote renders

devonwalshe opened this issue · comments

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.

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

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

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

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

EDIT: it has been documented in the README as well