Ruby on rails 如何填充下拉列表[rails4]
所以,我有一个应用程序,可以让用户上传和评论歌曲。不过,我想添加一个流派类别。当用户上传歌曲时,他们可以选择歌曲的类型。这将有助于我添加搜索 有关代码的快速概述,请参见:www.github.com/apane/leap 我猜我会在db中添加一个流派表,并将其与歌曲关联,例如: 体裁属于歌曲,一首歌曲有很多体裁Ruby on rails 如何填充下拉列表[rails4],ruby-on-rails,ruby-on-rails-4,Ruby On Rails,Ruby On Rails 4,所以,我有一个应用程序,可以让用户上传和评论歌曲。不过,我想添加一个流派类别。当用户上传歌曲时,他们可以选择歌曲的类型。这将有助于我添加搜索 有关代码的快速概述,请参见:www.github.com/apane/leap 我猜我会在db中添加一个流派表,并将其与歌曲关联,例如: 体裁属于歌曲,一首歌曲有很多体裁 但在那之后,我被难住了。如何填充“类型”下拉列表 将流派表添加到数据库后,可以执行以下操作: <%= collection_select(:song, :genre_id, Gen
但在那之后,我被难住了。如何填充“类型”下拉列表 将流派表添加到数据库后,可以执行以下操作:
<%= collection_select(:song, :genre_id, Genre.all, :id, :name) %>
其中:歌曲是指您上传歌曲的表单
rails g model genre name
rails g model genre_song genre:belongs_to song:belongs_to
rake db:migrate
模型/流派.rb
has_many :genre_songs
has_many :songs, through: :genre_song
has_many :genre_songs
has_many :genres, through: :genre_song
def self.tagged_with(name)
Genre.find_by_name!(name).songs
end
def tag_list
genres.map(&:name).join(", ")
end
def tag_list=(names)
self.genres = names.split(",").map do |n|
Genre.where(name: n.strip).first_or_create!
end
end
Genres: <%= raw song.genres.map(&:name).map { |t| link_to t, genre_path(t) }.join(', ') %>
<div class="field">
<%= f.label :tag_list, "Genres (separated by commas)" %><br />
<%= f.text_field :tag_list %>
</div>
型号/歌曲.rb
has_many :genre_songs
has_many :songs, through: :genre_song
has_many :genre_songs
has_many :genres, through: :genre_song
def self.tagged_with(name)
Genre.find_by_name!(name).songs
end
def tag_list
genres.map(&:name).join(", ")
end
def tag_list=(names)
self.genres = names.split(",").map do |n|
Genre.where(name: n.strip).first_or_create!
end
end
Genres: <%= raw song.genres.map(&:name).map { |t| link_to t, genre_path(t) }.join(', ') %>
<div class="field">
<%= f.label :tag_list, "Genres (separated by commas)" %><br />
<%= f.text_field :tag_list %>
</div>
歌曲/index.html.erb
has_many :genre_songs
has_many :songs, through: :genre_song
has_many :genre_songs
has_many :genres, through: :genre_song
def self.tagged_with(name)
Genre.find_by_name!(name).songs
end
def tag_list
genres.map(&:name).join(", ")
end
def tag_list=(names)
self.genres = names.split(",").map do |n|
Genre.where(name: n.strip).first_or_create!
end
end
Genres: <%= raw song.genres.map(&:name).map { |t| link_to t, genre_path(t) }.join(', ') %>
<div class="field">
<%= f.label :tag_list, "Genres (separated by commas)" %><br />
<%= f.text_field :tag_list %>
</div>
类型:
歌曲/\u form.html.erb
has_many :genre_songs
has_many :songs, through: :genre_song
has_many :genre_songs
has_many :genres, through: :genre_song
def self.tagged_with(name)
Genre.find_by_name!(name).songs
end
def tag_list
genres.map(&:name).join(", ")
end
def tag_list=(names)
self.genres = names.split(",").map do |n|
Genre.where(name: n.strip).first_or_create!
end
end
Genres: <%= raw song.genres.map(&:name).map { |t| link_to t, genre_path(t) }.join(', ') %>
<div class="field">
<%= f.label :tag_list, "Genres (separated by commas)" %><br />
<%= f.text_field :tag_list %>
</div>
歌曲\u控制器.rb
has_many :genre_songs
has_many :songs, through: :genre_song
has_many :genre_songs
has_many :genres, through: :genre_song
def self.tagged_with(name)
Genre.find_by_name!(name).songs
end
def tag_list
genres.map(&:name).join(", ")
end
def tag_list=(names)
self.genres = names.split(",").map do |n|
Genre.where(name: n.strip).first_or_create!
end
end
Genres: <%= raw song.genres.map(&:name).map { |t| link_to t, genre_path(t) }.join(', ') %>
<div class="field">
<%= f.label :tag_list, "Genres (separated by commas)" %><br />
<%= f.text_field :tag_list %>
</div>
添加
标签列表
到许可参数您试过什么了吗?快速搜索显示