Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6908413
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:33:37+00:00 2026-05-27T08:33:37+00:00

I have some users that can have many posts , and each of those

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T08:33:38+00:00Added an answer on May 27, 2026 at 8:33 am

    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)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Lets suppose that I have some pages some.web/articles/details/5 some.web/users/info/bob some.web/foo/bar/7 that can call a
We have some files on our website that users of our software can download.
I have an app that can open up some other forms at the user's
Let's say I have a subroutine/method that a user can call to test some
I have a sql database that stores some documents. A user can sign into
I have to ship some groovy code to some users that have only java
I am currently working with some users that do not have the .NET Framework
I have some input elements that the users are request special behavior on pageup
I have some script in my default page that redirects users to language specific
I have some pages that I don't want users to be able to access

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.