lml / commontator

A Rails engine for comments
MIT License
353 stars 99 forks source link

Using helper method in comments/_body.html.erb #152

Closed 1dolinski closed 4 years ago

1dolinski commented 4 years ago

I recently added an autolink gem (https://github.com/vmg/rinku)

On create a comment properly auto links.

On edit it errors out with an undefined method:

ActionView::Template::Error (undefined method `rinku_link' for #<#<Class:0x00007fb1e56eec60>:0x00007fb1e71511c0>
Did you mean?  rinku_auto_link):
     5: <%= simple_format comment.is_deleted? ? \
     6:       t('commontator.comment.status.deleted_by',
     7:         deleter_name: Commontator.commontator_name(comment.editor)) : \
     8:         rinku_link(comment.body) %>

Btw, rinku_link is the correct helper method name

Perhaps this user was having a similar issue - https://github.com/lml/commontator/issues/55

Dantemss commented 4 years ago

Is this a helper method defined in app/helpers? I couldn't find any mention of it in the rinku repo. If that's the case, I believe you will need to include your helper file (maybe include ::ApplicationHelper) in the controller that is serving this view. If it's really in Rinku, you might have to include ActionView::Helpers::TextHelper in that controller probably...

Dantemss commented 4 years ago

And/or actually you might have to do this include in Commontator::ApplicationController

1dolinski commented 4 years ago

Yes, defined in app/helpers

require 'rails_rinku'
include ActionView::Helpers::TextHelper

module TextHelper
    def rinku_link(text)
        auto_link(text, :html => { :target => '_blank' })
    end
end
Dantemss commented 4 years ago

Yeah you'll need something like Commontator::ApplicationController.include TextHelper and probably also include this module in the controller that renders the view where the thread itself appears.

It is a little odd that you have include ActionView::Helpers::TextHelper outside of the context of a module though. It's like you are including that module in main?

1dolinski commented 4 years ago

Good catch, moved the include into the module. It seems like there might be a reason why the TextHelper is recognized on create but not on edit. I understand that the helper module is already autoloaded by rails, so it wouldn't need to be re-loaded in the controller.

Adding Commontator::ApplicationController.include ActionView::Helpers::TextHelper to the bottom of the initializer doesn't seem to help

Dantemss commented 4 years ago

Hmm I think the problem is that the method is being defined in ::TextHelper, not ActionView::Helpers::TextHelper so try Commontator::ApplicationController.include ::TextHelper instead. Or Commontator::ApplicationController.helper ::TextHelper. Or change your module TextHelper to module ActionView::Helpers::TextHelper (no need to include it in that case).

Dantemss commented 4 years ago

I think Rails may keep gem controllers and helpers separate from the application's by default. Getting gem controllers to use app helpers has always been a little weird to me.

1dolinski commented 4 years ago

For sure and great, one of your solutions worked out -- thanks! I added the following in my Commontator initializer Commontator::ApplicationController.helper ::TextHelper

and then also stripped out the include in the helper

require 'rails_rinku'

module TextHelper
    def rinku_link(text)
        auto_link(text, :html => { :target => '_blank' })
    end
end