How would I go about letting a user add and associate a comma separated list of categories while add

Posted by Throlkim on Stack Overflow See other posts from Stack Overflow or by Throlkim
Published on 2010-05-06T11:19:14Z Indexed on 2010/05/06 11:48 UTC
Read the original article Hit count: 205

Filed under:
|
|
|
|

I've been struggling for quite a while to get this feature working:

I want my user to be able to select categories when uploading a photograph, but additionally be able to specify a comma-separated list of categories to create/find and associate with the photograph. I've had this working by using an attr_accessor :new_categories on the photographs model, but having that there without the column existing breaks both Paperclip and Exifr. Obviously, image upload and EXIF data retrieval are pretty important for a photography website, but not being able to add categories while uploading a photograph is a pain in the arse.

Methods I've tried so far:

  • Using attr_accessor to add a field for new_categories. Breaks gems.
  • Using an Ajax sub-form to add categories. Formtastic can't handle it.
  • Adding a column for new_categories to the photograph model. It works, but it's horrific.

I haven't tried using a nested form, but I'd need to intercept it and stop it from processing it as normal.

Here's an example of what I'm trying to accomplish: http://imgur.com/rD0PC.png

And the function I use to associate the categories:

def process_new_categories
    unless self.new_categories.nil?
        for title in self.new_categories.split(",")
            self.categories << Category.find_or_create_by_title(title.strip.capitalize)
        end
    end
end

Has anyone got any ideas as to how to do this?

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about rails