Question

My goal is to find a way to add a new field in the submission form to gather what country the picture is from. I want to make the country searchable the same way any tag can lead to a page just with items containing that tag when using the acts-as-taggable-on gem. I made a few very, very misguided attempts with my early October attempts at https://github.com/harrisongill/whatyoutheat/commits/master.

Ideally, I want it to look like when you go to a page like http://community.whatyoutheat.com/pins/10, there are not only lines like description, author, and tags, but also one for country that works similar to how a tag works.

Any help would be appreciated. I'm open to something involving acts-as-taggable-on or anything else.

Was it helpful?

Solution

Easy peasy. And looking at your code, there's not much to do.

First of all, you need a virtual attribute for country since it's a field that doesn't exist in your database and as far as I can tell, doesn't need to. And because it's rails 3, you'll need to make it accessible. Next you'll merge the country name with the tags list before you submit.

That's it.

Step 1 - Add a virtual attribute in the model

attr_accessor :country

Step 2 - Make the attribute accessible in the model

attr_accessible :description, :image, :image_remote_url, :tag_list, :country

Step 3 - Append the country name to the tag list before submit in the controller#create

params[:pin][:tag_list] = params[:pin][:tag_list] + ", " + params[:pin][:country]
params.delete :country
@pin = current_user.pins.new(params[:pin])

Also remember to replace the tag list in Show to create links to the tags.

<p>Tags: <%= raw @pin.tag_list.map { |t| link_to t, tag_path(t) }.join(', ') %></p>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top