简体   繁体   中英

Why isn't my rails 4 helper working?

I have a helper module for my home page with two methods that do the same thing:

module HomeHelper
    def parsed_text(tweet)
        auto_link (tweet).gsub(/(@\w+)/, %Q{<a href="http://twitter.com/\\1">\\1</a>})
    end
    def other_parsed_text
        self.auto_link.gsub(/(@\w+)/, %Q{<a href="http://twitter.com/\\1">\\1</a>})
    end
end

In my view this works:

<%= parsed_text(tweet.text) %>

But this doesn't:

<%= tweet.text.other_parsed_text %>

I get a NoMethodError at / undefined method other_parsed_text . Isn't self the caller of the method inside of my helper method?

What am I doing wrong? I want the second style of calling methods with a . notation to work too. How do I do that?

This does not work because you didnt extend the class that tweet.text is of. You can use ActiveSupport::Concern if you want to extend some class. What you are doing now is provding some methods that can be called with parameters.

// I posted an example here: https://stackoverflow.com/a/8504448/1001324

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