Rails 5 个标签数组转成字符串
Rails 5 tags array turning into string
BLUF:我的 posts 标签数组可以很好地创建和读取标签,但是当我编辑 post 时,表单字段会去除标签数组中的逗号;用户必须手动将逗号重新输入到标签字段中,即使他们没有更新它,否则标签会在一个长字符串中连接在一起。
详情:使用rails5和acts_as_taggable_ongem。
我的Post模特
class Post < ApplicationRecord
acts_as_taggable
在我的控制器中:
private
def post_params
params.require(:post).permit(:post_type, :title, :content, :picture, :body_parts,
:duration, :equipment, :calories, :protein,
:fat, :carbs, :ingredients, tag_list:[] )
end
在我的表单视图中:
<%= form_for(@post, html: { multipart: true }) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<span class="glyphicon glyphicon-tags"></span>
<%= f.text_field :tag_list, multiple: true, class: 'form-control' %>
<%= f.submit "Save changes", class: "btn btn-primary" %>
<%= link_to "Cancel", post_path(@post), class: "btn btn-default" %>
<% end %>
在 post 显示视图中:
<%= raw post.tag_list.map { |t| link_to t, tag_path(t) }.join(', ') %>
我不知道还有什么相关的,但就像我说的,一切正常,直到我尝试编辑 post,然后 text_field 去掉逗号
提前感谢您的任何建议,
通过 value
参数向 text_field
添加标签,并将它们转换为逗号分隔符字符串。
<%= f.text_field :tag_list, value: f.object.tag_list.to_s, multiple: true, class: 'form-control' %>
BLUF:我的 posts 标签数组可以很好地创建和读取标签,但是当我编辑 post 时,表单字段会去除标签数组中的逗号;用户必须手动将逗号重新输入到标签字段中,即使他们没有更新它,否则标签会在一个长字符串中连接在一起。
详情:使用rails5和acts_as_taggable_ongem。
我的Post模特
class Post < ApplicationRecord
acts_as_taggable
在我的控制器中:
private
def post_params
params.require(:post).permit(:post_type, :title, :content, :picture, :body_parts,
:duration, :equipment, :calories, :protein,
:fat, :carbs, :ingredients, tag_list:[] )
end
在我的表单视图中:
<%= form_for(@post, html: { multipart: true }) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<span class="glyphicon glyphicon-tags"></span>
<%= f.text_field :tag_list, multiple: true, class: 'form-control' %>
<%= f.submit "Save changes", class: "btn btn-primary" %>
<%= link_to "Cancel", post_path(@post), class: "btn btn-default" %>
<% end %>
在 post 显示视图中:
<%= raw post.tag_list.map { |t| link_to t, tag_path(t) }.join(', ') %>
我不知道还有什么相关的,但就像我说的,一切正常,直到我尝试编辑 post,然后 text_field 去掉逗号
提前感谢您的任何建议,
通过 value
参数向 text_field
添加标签,并将它们转换为逗号分隔符字符串。
<%= f.text_field :tag_list, value: f.object.tag_list.to_s, multiple: true, class: 'form-control' %>