Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comma separated array with a text_field in Rails

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.

like image 808
Vlad Sabev Avatar asked Dec 01 '11 17:12

Vlad Sabev


1 Answers

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)

like image 188
Chris Bailey Avatar answered Oct 31 '22 10:10

Chris Bailey