Created
March 13, 2012 20:35
-
-
Save Mikke/2031392 to your computer and use it in GitHub Desktop.
Rails 3 has_many use checkboxes and multiple select in the form
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
rails g scaffold Book title:string description:text | |
rails g scaffold Pubhouse title:string address:text | |
rails g model BookPubhouse book_id:integer pubhouse_id:integer | |
rake db:migrate |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 'app/models/book.rb' | |
class Book < ActiveRecord::Base | |
has_many :book_pubhouses, :dependent => :destroy # уничтожать вместе с основным экземпляром | |
has_many :pubhouses, :through => :book_pubhouses # through указывает Rails приложению зависимость двух моделей через третью, теперь мы можем использовать Book.pubhouses напрямую | |
# валидация | |
validates :title, :presence => true | |
validates :title, :uniqueness => true | |
validates :title, :length => { :maximum => 150 } | |
validates :description, :length => { :maximum => 2000 } | |
end | |
# 'app/models/pubhouse.rb' | |
class Pubhouse < ActiveRecord::Base | |
has_many :book_pubhouses, :dependent => :destroy | |
has_many :shops, :through => :book_pubhouses | |
validates :title, :presence => true | |
validates :title, :uniqueness => true | |
validates :title, :length => { :maximum => 150 } | |
validates :address, :length => { :maximum => 2000 } | |
end | |
# 'app/models/book_pubhouse.rb' | |
class BookPubhouse < ActiveRecord::Base | |
belongs_to :book | |
belongs_to :pubhouse | |
validates :book_id, :presence => true | |
validates :pubhouse_id, :presence => true | |
validates :pubhouse_id, :uniqueness => {:scope => :book_id} | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%= form_for(@book) do |f| %> | |
... | |
<div class="param"> | |
<%= f.label :pubhouses %> | |
<span class="ul"> | |
<% Pubhouse.all.each do |pubhouse| %> | |
<label class="li"><%= check_box_tag :pubhouse_ids, pubhouse.id, @book.pubhouses.include?(pubhouse), :name => 'book[pubhouse_ids][]' %> <u><%= pubhouse.title %></u></label><br /> | |
<% end %> | |
</span> | |
</div> | |
... | |
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%= form_for(@book) do |f| %> | |
... | |
<div class="param"> | |
<%= f.label :pubhouses %> | |
<%= select_tag("book[pubhouse_ids][]", options_for_select(Pubhouse.all.collect { |pubhouse| [pubhouse.title, pubhouse.id] }, @book.pubhouses.collect { |pubhouse| pubhouse.id}), {:multiple=>true, :size=>5}) %> | |
</div> | |
... | |
<% end %> |
Could you please post your controller? Thanks. ;)
Use accepts_nested_attributres_for in model, see http://stackoverflow.com/questions/17585215/how-to-update-attributes-in-many-to-many-relation-in-rails
For rails 4, in the controller:
def create
...
if @book.save
@book.pubhouses.build
...
end
def update
...
if @book.save
@book.pubhouses.build
...
end
def book_params
params.require(:book).permit(pubhouse_ids: [])
end
This is very helpful thank you!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
i want know about the controller for edit and delete