Rails 中带有 text_field 的逗号分隔数组 [英] Comma separated array with a text_field in Rails

查看:38
本文介绍了Rails 中带有 text_field 的逗号分隔数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些用户可以有很多帖子,并且每个帖子都可以有很多标签.我已经使用帖子和标签之间的 has_and_belongs_to_many 关系实现了这一点.

创建新帖子时,用户可以使用逗号分隔的值列表对其进行标记(很像在 SO 上发布新问题时).如果任何标签尚不存在,则应自动创建.这是帖子的 _fields.html.erb 部分:

<%= form_for @post do |f|%><%= 渲染 'shared/error_messages', :object =>f.对象%><h1 class="post"><%= t(:new_post_message) %></h1><div class="field"><%= f.label t(:title) %><br/><%= f.text_field :title %>

<div class="field"><%= f.label t(:tag).pluralize %><br/><%= f.text_field :tags %>

<div class="field"><%= f.label t(:text) %><br/><%= f.text_area :content %>

<div class="actions"><%= f.submit t(:post_verb) %>

<%结束%>

现在使用 f.text_field :tags 会导致输入元素带有 [] 文本.

我还没有在 posts_controller.rb 中使用标签,因为我不确定应该如何从参数中获取和拆分字符串值:

def 创建@post = current_user.posts.build(params[:post])如果@post.save重定向到 root_path别的@流 = []呈现页面/主页"结尾结尾

以前有人解决过这个问题吗?非常感谢.

解决方案

我更喜欢在 post.model 上创建一个属性来读取标签.例如

app/models/post.rb

def tag_listself.tags.map { |t|t.name }.join(", ")结尾def tag_list=(new_value)tag_names = new_value.split(/,\s+/)self.tags = tag_names.map { |name|Tag.where('name = ?', name).first 或 Tag.create(:name => name) }结尾

然后在你看来你可以这样做:

<%= f.text_field :tag_list %>

代替:tags

帖子模型将接受标签列表,拆分为标签名称,如果存在则查找该标签,如果不存在则创建它.无需控制器逻辑.

EDIT 这段代码当然依赖于你的标签模型有一个名为 name 的属性(如果不只是替换你存储标签name"的任何属性),并且它在数据库(即您在标签模型中使用了类似 validates_uniqueness_of :name 的东西)

I have some users that can have many posts, and each of those posts can have many tags. I've implemented that using a has_and_belongs_to_many relation between the posts and the tags.

When creating a new post, the user can tag it using a comma separated list of values (much like when posting a new question on SO). If any of the tags does not exist already, it should be automatically created. This is the _fields.html.erb partial for the post:

<%= form_for @post do |f| %>
  <%= render 'shared/error_messages', :object => f.object %>

  <h1 class="post"><%= t(:new_post_message) %></h1>

  <div class="field">
    <%= f.label t(:title) %>
    <br />
    <%= f.text_field :title %>
  </div>

  <div class="field">
    <%= f.label t(:tag).pluralize %>
    <br />
    <%= f.text_field :tags %>
  </div>

  <div class="field">
    <%= f.label t(:text) %>
    <br />
    <%= f.text_area :content %>
  </div>

  <div class="actions">
    <%= f.submit t(:post_verb) %>
  </div>
<% end %>

Using f.text_field :tags right now results in an input element with [] for text.

I don't use tags in posts_controller.rb yet, because I'm not sure how I should get and split the string value from the parameters:

def create
  @post = current_user.posts.build(params[:post])
  if @post.save
    redirect_to root_path
  else
    @stream = []
    render 'pages/home'
  end
end

Has anyone tackled this problem before? Thanks a lot.

解决方案

My preference would be to create an attribute on the post.model to read in the tags. e.g.

app/models/post.rb

def tag_list
  self.tags.map { |t| t.name }.join(", ")
end

def tag_list=(new_value)
  tag_names = new_value.split(/,\s+/)
  self.tags = tag_names.map { |name| Tag.where('name = ?', name).first or Tag.create(:name => name) }
end

Then in your view you can do:

<%= f.text_field :tag_list %>

instead of :tags

The post model will accept the tag list, split into tag names, find the tag if it exists, and create it if it doesn't. No controller logic required.

EDIT This code of course relies on your tag model having an attribute called name (if not just substitute whatever attribute you're storing the tags 'name' in), and that it's unique in the database (i.e. you're using something like validates_uniqueness_of :name in your tags model)

这篇关于Rails 中带有 text_field 的逗号分隔数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆