Rails 5标签阵列变成了字符串(Rails 5 tags array turning into string)

编程入门 行业动态 更新时间:2024-10-28 04:17:19
Rails 5标签阵列变成了字符串(Rails 5 tags array turning into string)

BLUF:我的帖子标签数组适用于创建和读取标签,但是当我编辑帖子时,表单字段会剥离其逗号的标签数组; 即使用户不更新它,用户也必须手动将逗号重新输入到标签字段中,否则标签将以一个长字符串连接在一起。

详情:使用rails 5和acts_as_taggable_on gem。

我的博文模型

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 %>

在后期展示视图中:

<%= raw post.tag_list.map { |t| link_to t, tag_path(t) }.join(', ') %>

我不知道还有什么相关的,但正如我所说的,一切正常,直到我尝试编辑帖子,然后text_field去掉逗号

提前感谢您的任何建议,

BLUF: My tags array for posts works fine to create and read the tags, but when I edit the post, the form field strips the tags array of their commas; the users has to manually re-enter the commas into the tags field, even if they aren't updating it, or else the tags get joined to together in one long string.

Details: using rails 5 and the acts_as_taggable_on gem.

My Post model

class Post < ApplicationRecord acts_as_taggable

in my Controller:

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

in my form view:

<%= 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 %>

in the post show view:

<%= raw post.tag_list.map { |t| link_to t, tag_path(t) }.join(', ') %>

I don't' know what else is relevant, but like I said, everything works fine until i try and edit the post, and then the text_field strips out the commas

Thanks in advance for any advice,

最满意答案

通过value参数将标签添加到text_field并将它们转换为逗号分隔符字符串。

<%= f.text_field :tag_list, value: f.object.tag_list.to_s, multiple: true, class: 'form-control' %>

Add tags to text_field through value argument and convert them to comma delimiter string.

<%= f.text_field :tag_list, value: f.object.tag_list.to_s, multiple: true, class: 'form-control' %>

更多推荐

本文发布于:2023-07-14 20:47:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1107339.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:阵列   字符串   变成了   标签   Rails

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!