Rails:无法使用acts_as_taggable_on 的文本字段设置或更新tag_list

发布于 2024-10-09 04:31:28 字数 631 浏览 6 评论 0原文

大家好,我正在尝试将标签添加到我正在开发的 Rails 照片库系统中。它可以从后端运行,但是如果我尝试在表单视图中设置或更改它,它就不起作用。

我将 actions_as_taggable 添加到照片模型中并进行了迁移。我的图库生成器被编程为自动为其创建的每张照片添加一个标签。这工作得很好,就像为控制台设置一样。但是,我似乎无法在照片表单中使用 text_field 设置标签。

这是我添加到照片表单中的代码:

  <p>
    <%= f.label :tag_list %><br />
    <%= f.text_field :tag_list %>
  </p>

现在,这非常简单,因为 :tag_list 支持单字符串逗号分隔赋值(例如 tag_list = "this, that, the other" #=> ['this', '那','另一个']),我不明白为什么使用文本字段不起作用。更没有意义的是,如果已经填充了标签列表,则在编辑照片时该列表仍会显示在文本字段中。我似乎无法对列表进行任何更改。他们的 github 页面上的文档似乎没有提供有关如何从视图设置这些值的任何信息。有什么想法吗?

哦,我正在使用 Rails 3 gem 版本。

Hey everyone, I'm trying to add tagging to a rails photo gallery system I'm working on. It works from the back-end, but if I try to set or change it in the form view, it doesn't work.

I added acts_as_taggable to the photo model and did the migrations. My gallery builder is programmed to add one tag automatically to each photo it creates. This works fine, just as if it were setting it for the console. However, I can't seem to set tags using a text_field in the photo form.

Here's the code I added to my photo form:

  <p>
    <%= f.label :tag_list %><br />
    <%= f.text_field :tag_list %>
  </p>

Now, that's pretty trivial, and since :tag_list supports single-string comma-separated assignment (e.g. tag_list = "this, that, the other" #=> ['this', 'that', 'the other']), I don't see why using a text field doesn't work. And to make even less sense, if a tag list has already been populated, the list will still show up in the text field when editing the photo. I just can't seem to commit any changes to the list. The documentation on their github page doesn't appear to give any information on how to set these values from the view. Any ideas?

Oh, and I'm using the Rails 3 gem version.

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

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

发布评论

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

评论(2

紫﹏色ふ单纯 2024-10-16 04:31:28

我想通了。事实证明,acts_as_taggable 不会自动为您提供 tag_list 的访问器。我在视图中的代码是正确的,但我需要添加

attr_accessor :tag_list

照片模型。就这么简单。

I figured it out. Turns out that acts_as_taggable doesn't give you accessors for tag_list automatically. My code in the view was right, but I needed to add

attr_accessor :tag_list

do the photo model. Simple as that.

小情绪 2024-10-16 04:31:28

如果您在 Rails 7 / acts_as_taggable 5 天内再次遇到此问题,可能是您忘记将控制器中的 :tag_list 参数列入白名单。不再需要手动创建 attr_accessor

    # Only allow a list of trusted parameters through.
    def foo_params
      params.require(:foo).permit(:subject, :markdown, :tag_list)
    end

我似乎无法对列表进行任何更改

就我而言,一旦添加 :tag_list 我就能够使用我的视图来更新我的模型内容,正如我通常期望的那样。

如果您要向已创建的控制器添加标记,则很容易忘记。

If you run into this again in the Rails 7 / acts_as_taggable 5 days, it's probably that you forgot to whitelist the :tag_list parameter in your controller. No need anymore for manually creating the attr_accessor.

    # Only allow a list of trusted parameters through.
    def foo_params
      params.require(:foo).permit(:subject, :markdown, :tag_list)
    end

I just can't seem to commit any changes to the list

In my case, once I added :tag_list I was able to use my view to update my models content as I would normally expect.

Easy to forget if you're adding tagging to an controller that you had already created.

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