似乎不能让act - as - taggable与Rails 4一起工作(字段不会更新)

时间:2022-01-21 00:16:17

Seeing as the documentation for this gem is absolutely useless for beginners like myself (they the docs it doesn't say which code needs to go in which file), I figured I might get some better help here for my Rails 4 app instead of the official docs.

因为这个gem的文档对于像我这样的初学者来说是毫无用处的(它们是文档,它没有说明需要将哪个代码放入哪个文件中),所以我认为我可以从Rails 4应用程序而不是官方文档中得到一些更好的帮助。

I'm not sure if acts-as-taggable-on is the best solution for my problem, but here's what I'm trying to do: I'm creating a business directory that works with tags instead of categories so that a bar/lounge can belong to both a bar, and a lounge. Perhaps someone knows a better solution instead of using acts-as-taggable-on?

我不确定acts-as- taggabon是否是解决我的问题的最佳解决方案,但我想做的是:我正在创建一个使用标签而不是类别的业务目录,这样一个bar/lounge就可以同时属于bar和lounge。也许有人知道一个更好的解决方案,而不是使用act -as- taggabon ?

There are no errors when I try to create or view the tags, but the problem is that the tags arent' being saved. When I try to view the business which has tags, it shows up empty. Same thing when I try to edit it.

当我试图创建或查看标记时,没有错误,但问题是标签没有被保存。当我试图查看有标记的业务时,它显示为空。当我试着编辑它的时候也是一样。

This is what my model looks like:

我的模型是这样的:

class Business < ActiveRecord::Base
  validates :name, uniqueness: true
  acts_as_taggable
  acts_as_taggable_on :tag_list

end

The form:

形式:

<%= form_for(@business) do |f| %> 
  ...

  <div class="field">
    <%= f.label :tag_list, "Tags (seperated by commas)" %><br>
   <%= f.text_field :tag_list %>
  </div>

  ...
<% end %>

The view:

视图:

  <p>
      <strong>Tags:</strong>
      <%= @business.tag_list %>
  </p>

Anyone know what is preventing the tags from being viewed/saved? Perhaps there's better documentation out there that someone could provide.

有人知道是什么阻止标签被查看/保存吗?也许有人能提供更好的文档。

update: I'm also using Active_Admin, which seems to be causing some problem according to Matt Boldt. After following his tutorial my problem remains unsolved.

更新:我还使用了Active_Admin,这似乎是根据Matt Boldt造成的一些问题。在学习了他的教程之后,我的问题仍然没有解决。

3 个解决方案

#1


2  

possible it is an array issue? not sure. what version of acts_as_taggable are you using?

可能是数组问题?不确定。您正在使用什么版本的acts_as_taggable ?

try adding to your strong params to ask for an array.

尝试添加到强参数以请求数组。

  def business_params
      params.require(:business).permit(:xxx, :xxx, :tag_list => [])
  end

#2


1  

I had the same problem and changing :tag_list => [] with :tag_list fixed it

我遇到了同样的问题,改变了:tag_list => [] with:tag_list修复了这个问题

I guess it depends by the type of input you use in your form, in my case was a text input

我猜这取决于你在表单中使用的输入类型,在我的例子中是文本输入

 def business_params
  params.require(:business).permit(:xxx, :xxx, :tag_list)
 end

#3


0  

I was having the same problem and checked Terminal to see where the error had occurred, I got this Unpermitted parameters: tag_list

我遇到了同样的问题,检查了终端,看看哪里发生了错误,我得到了这个不允许的参数:tag_list

I had forgotten to add :tag_list to the strong params accepted attributes in the controller for the model I was using tags with. Try that!

我忘记添加:tag_list到我使用标签的模型的控制器中接受属性的强params中。试一试!

#1


2  

possible it is an array issue? not sure. what version of acts_as_taggable are you using?

可能是数组问题?不确定。您正在使用什么版本的acts_as_taggable ?

try adding to your strong params to ask for an array.

尝试添加到强参数以请求数组。

  def business_params
      params.require(:business).permit(:xxx, :xxx, :tag_list => [])
  end

#2


1  

I had the same problem and changing :tag_list => [] with :tag_list fixed it

我遇到了同样的问题,改变了:tag_list => [] with:tag_list修复了这个问题

I guess it depends by the type of input you use in your form, in my case was a text input

我猜这取决于你在表单中使用的输入类型,在我的例子中是文本输入

 def business_params
  params.require(:business).permit(:xxx, :xxx, :tag_list)
 end

#3


0  

I was having the same problem and checked Terminal to see where the error had occurred, I got this Unpermitted parameters: tag_list

我遇到了同样的问题,检查了终端,看看哪里发生了错误,我得到了这个不允许的参数:tag_list

I had forgotten to add :tag_list to the strong params accepted attributes in the controller for the model I was using tags with. Try that!

我忘记添加:tag_list到我使用标签的模型的控制器中接受属性的强params中。试一试!