views:

43

answers:

2

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?

+1  A: 

It would be easier to help if I could see your form code, roughly. I don't know anything about Formtastic, but this is very easy and very common in basic Rails.

Simply add a text field to your form:

<%= text_field_tag :new_categories %>

In your controller:

#changing your method to take a parameter here
#and moving method to the model object
model_obj.process_new_categories(params[:new_categories]) 
MattMcKnight
Ah ha, I got halfway to trying that last night but I got distracted by something and veered off on another path. I think it could work if I ensure it runs after the model has been updated, and also pass the model object through to the function; unless I've forgotten a reason why I didn't do that last night. I'll get back to you once I've checked! :)
Throlkim
Good thought, I moved the method onto the model itself, so it can have the right context to make updates.
MattMcKnight
Just ran a test and it works great - no problems from Exifr, which is a relief. Thanks very much :)
Throlkim
+1  A: 

Check out this screen-cast http://railscasts.com/episodes/167-more-on-virtual-attributes it shows how to create a tags class which is similar to the categories class that you're trying to re-create.

ThinkBohemian
That's almost exactly what I'm trying to achieve - I'll have to check to see whether it works with Paperclip and Exifr. Thanks for that, I somehow missed that Railscast.
Throlkim
no problem, though it was written awhile ago, i believe in newer versions of rails there is a better way of describing the has_and_belongs_to_many relationship
ThinkBohemian