Auto_link 与rails_autolink gem - 未定义的方法

发布于 2025-01-02 10:30:04 字数 734 浏览 2 评论 0原文

我正在尝试使用rails_autolink gem。 像往常一样,我将 gem 声明添加到我的 gemfile 中:

gem 'rails_autolink'

然后运行“bundle install”,重新启动我的 Rails 本地服务器并 然后,我在类声明之前将 require 添加到 posts_controller 中

require 'rails_autolink'
class PostsController < ApplicationController
     ...

,并在创建操作中使用 auto_link 方法。

    def create
        @new_post = Post.new(params[:post])

        if @new_post.content == ""
          redirect_to posts_url
        else
         @new_post.content = auto_link(@new_post.content)

         ... #respond_to and save methods
        end
    end

end #end of my post controller

事情是,当我尝试创建帖子时,我在 auto_link 方法上有一个未定义的方法,知道为什么吗?是宝石特有的还是其他原因?

I'm trying to use the rails_autolink gem.
As usual, I added the gem declaration to my gemfile:

gem 'rails_autolink'

Then ran "bundle install", restarted my rails local server and
Then I added to my posts_controller the require before the class declaration

require 'rails_autolink'
class PostsController < ApplicationController
     ...

and used the auto_link method inside my create action

    def create
        @new_post = Post.new(params[:post])

        if @new_post.content == ""
          redirect_to posts_url
        else
         @new_post.content = auto_link(@new_post.content)

         ... #respond_to and save methods
        end
    end

end #end of my post controller

The thing is that when I try to create a post I have a undefined method on the auto_link method, any idea why ? Is it specific to the gem or is it because something else ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

独闯女儿国 2025-01-09 10:30:04

这是您尝试在控制器中使用的视图助手

@new_post.content = view_context.auto_link(@new_post.content)

This is a view helper which you're trying to use in a controller

@new_post.content = view_context.auto_link(@new_post.content)
无悔心 2025-01-09 10:30:04

在任何其他地方您可以使用: ActionController::Base.helpers.auto_link(text)

In any other places you can use: ActionController::Base.helpers.auto_link(text)

你的往事 2025-01-09 10:30:04

我不相信你需要命名空间。只需使用以下命令再试一次:

auto_link(@new_post.content)

I don't believe you need the namespacing. Try it again simply using:

auto_link(@new_post.content)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文