Forum: Ruby on Rails Help with habtm models in one form

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
4629eb532ec7772781fd2a7b9c263896?d=identicon&s=25 Fatih Genç (Guest)
on 2008-12-04 18:39
(Received via mailing list)
Hi I have a post and a category model
class Post < ActiveRecord::Base
has_and_belongs_to_many :categories
end
class Category < ActiveRecord::Base
has_and_belongs_to_many :posts
 end
I created join table migration files

Now I wanna create category while creating post I mean user can choose
category or add a new category to this post how can i imlement this?

--
74a56286718bc003d6c7438f7efa1e3a?d=identicon&s=25 Navjeet Chabbewal (jeet)
on 2008-12-04 19:03
I did something like this in the past in a similar situation.

I have a drop down of categories that has all the valid categories and
an additional category injected as 'New Category'. When you user selects
the 'New Category' from the drop down, I display another textbox (I did
this with ajax call) for the user to provide new category name. In the
controller if this new_category param is detected than a new category is
created first before creating the post associated with that new
category.

-Navjeet

Fatih Genç wrote:
> Hi I have a post and a category model
> class Post < ActiveRecord::Base
> has_and_belongs_to_many :categories
> end
> class Category < ActiveRecord::Base
> has_and_belongs_to_many :posts
>  end
> I created join table migration files
>
> Now I wanna create category while creating post I mean user can choose
> category or add a new category to this post how can i imlement this?
>
> --
This topic is locked and can not be replied to.