我正在尝试为音乐家创建一个设置列表应用程序。用户可以上传他们的歌曲曲目并从这些歌曲列表中挑选歌曲以填充特定表演的设置列表。
见图片:setlist_screenshot。单击左侧歌曲上的箭头图标应将歌曲添加到右侧的可排序列表中。
有问题的型号:
class Setlist < ActiveRecord::Base
belongs_to :organization
# join between Set Lists & Arrangements(songs)
has_many :items, -> { order(position: :asc) }
has_many :arrangements, through: :items
end
-
class Item < ActiveRecord::Base
# join between Set Lists & Arrangements(songs)
belongs_to :arrangement
belongs_to :setlist
acts_as_list scope: :setlist
end
-
class Arrangement < ActiveRecord::Base
belongs_to :song
belongs_to :organization
has_many :attachments
# join between Set Lists & Arrangements(songs)
has_many :items
has_many :setlists, through: :items
end
基本上,一个设置列表通过项目(连接表)有许多安排(歌曲),并且安排(歌曲)当然可以在许多设置列表中。
我在视图中添加歌曲的按钮的代码是:
<%= link_to(organization_setlists_path(:arrangement_id => song.arrangements.first.id, setlist_id: @new_set.id, remote: true), method: "post", class: "secondary-content" )
这是对 setlists 控制器的 create 操作的发布请求(包括在新操作中创建的歌曲的排列 id 和 Setlist.new 的 id 的参数),如下所示:
class SetlistsController < ApplicationController
before_action :authenticate_user!
def new
if @new_set.nil?
@new_set = Setlist.create
@new_item = Item.new
end
end
def create
@arrangement = Arrangement.find(params[:arrangement_id])
@new_item = Item.new
@new_item.setlist_id = Setlist.find(params[:setlist_id])
@new_item.arrangement_id = @arrangement.id
if @new_item.save
flash[:message] = "Song Sucessfully Added!"
else
flash[:message] = "Something Went Wrong"
end
end
理想情况下,我希望在每次单击添加/箭头按钮后(远程)为同一个设置列表创建一个新项目,直到用户完成创建该设置列表。为了在右侧显示,我有:
<% if @new_set && @new_set.items.count >=1 %>
<ul>
<% @new_set.items.each do |item| %>
<li class="center-align"><%= item.arrangement.title %></li>
<% end %>
</ul>
<% else %>
<p class="center-align">Add Songs From The List On The Left</p>
<% end %>
最终,用户应该能够完成它并继续创建另一个集合列表,如果他们愿意的话。
除了我在这里所做的事情之外,我真的不知道该怎么做,这是行不通的。问题是:
一方面,创建操作实际上是创建一个项目并为新项目分配外键以进行安排,但由于某种原因而不是设置列表,即使设置列表 ID 是从 link_to 参数正确传递的。
此外,每次刷新页面时,都会根据我在新操作中的内容生成一个新的集合列表,因此添加的每个项目都会进入一个集合列表,然后生成一个空白集合列表,因此右侧不会出现任何内容.
最后,这看起来很混乱。有人有更好的设计策略来实现这一点吗?任何帮助将不胜感激!!!