当前位置: 动力学知识库 > 问答 > 编程问答 >

ruby on rails 3 - Do I need a model or controller for a join table?

问题描述:

I am having trouble with my first join-table for a many to many relationship.

class Category < ActiveRecord::Base

has_and_belongs_to_many :users

end

class User < ActiveRecord::Base

has_and_belongs_to_many :categories

end

And I have added a join table:

create_table "categories_users", :id => false, :force => true do |t|

t.integer "category_id", :null => false

t.integer "user_id", :null => false

end

Can someone point me to an example of the form that I would use to add a user to a category? Do I need a separate restful controller for 'categories_users'? I have an impulse to add method to my category controller called "add user", but I'm not sure if that is wise for an otherwise RESTful controller.

网友答案:

I'm using simple_form and I finally found in the documentation there what to do. So using simple_form, the form would look like this:

<%= simple_form_for [:admin, @festival, @category] do |c| %>
  <%= c.input :name %>
  <%= c.input :description, input_html: { cols: 100, rows: 3, class: "span6" } %>
  <%= c.input :takes_submissions %>
  <%= c.association :users, label_method: :full_name, label: "Choose curators" %>
  <%= c.button :submit %>
<% end %>

No need to add a controller or separate form. Yay!

分享给朋友:
您可能感兴趣的文章:
随机阅读: