4

having those example models:

class Post < ActiveRecord::Base
  belongs_to :category
end

class Category < ActiveRecord::Base
  has_many :posts
end

Imagine I have a form to create new posts, by default I get a select with a list of available categories, but what If I want to create new categories from the "new/edit post" form?

Would be perfect to have a button with the select box that popup a windows to create a new category.

Is better than creating a new category and then going to create a new post.

What I always saw is creating has_many associations, but not belongs_to one.

Thank you

4

1 回答 1

1

I think you're kind of asking a couple questions here. I'm going to say the more important one is how to create the association in the opposite direction from how it's usually built (because that's the one I know how to answer). I'll focus on that.

For simplicity, I'll just define a simple text field with label you can fill in as part of your form.

= label_tag :new_category_name
= text_field_tag :new_category_name

In your controller, you can then build the new category like so.

@post.build_category(name: params[:new_category_name])

If you're saving your @post the conventional way, then the category will be created in the same transaction as the post, so if that fails it won't create the category. If you want it to save the category no matter what you can call @post.create_category instead.

Here's the documentation: http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html

In the table Singular associations (one-to-one), replace other with your model name.

The other question I think is how to make it look good in the view. You certainly have options for how to make it look good. You can create a popup as you suggested. You could also use AJAX to send a small xhr request. I've seen fancy combination select / text boxes too.

于 2012-12-04T23:13:16.963 回答