Rails association help

2019-09-15 06:46发布

问题:

Hey I made a scaffold called pictures, but now I need to it to have many tags. I want it to be so that you can do something like click add tag then fill in a tag, then click add tag ..., this would be in pictures/new and edit, and maybe show. I don't have a clue how to do this and I am not that great in rails, so be very clear if you can. (maybe there is something in rails like a button that if you click it you execute a function, even then I'm not sure how to go about this) I am using rails 3.

回答1:

This is something you need.:

  1. http://railscasts.com/episodes/196-nested-model-form-part-1

  2. http://railscasts.com/episodes/197-nested-model-form-part-2



回答2:

Kevin,

I think task number one is to read up on Rails ActiveRecord associations here:

http://guides.rubyonrails.org/association_basics.html

Once you have your associations right, I would make tags a sub-resource of pictures in your routes file. This would allow you to do a POST to :tags with a route like /pictures/{id}/tags