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_taggablein 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:[] ) endin 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' %>更多推荐
发布评论