Ruby on rails Rails在视图中返回奇怪的无效信息,但在控制台中不返回
我知道一个事实(通过控制台检查)输出应该是三首歌,但出于某种原因,我一直在每个页面添加第四首歌。这首歌在每一个场合都是不同的,我不明白为什么会发生这种情况。非常感谢您的帮助 在我的控制器中Ruby on rails Rails在视图中返回奇怪的无效信息,但在控制台中不返回,ruby-on-rails,ruby,activerecord,Ruby On Rails,Ruby,Activerecord,我知道一个事实(通过控制台检查)输出应该是三首歌,但出于某种原因,我一直在每个页面添加第四首歌。这首歌在每一个场合都是不同的,我不明白为什么会发生这种情况。非常感谢您的帮助 在我的控制器中 def edit @songs = Song.all(order: 'title') @song = Song.find(params[:id]) @setlist = Setlist.find(params[:id]) @allocations = @setlist.al
def edit
@songs = Song.all(order: 'title')
@song = Song.find(params[:id])
@setlist = Setlist.find(params[:id])
@allocations = @setlist.allocations
end
完整视图代码(根据要求):
首先是实际编辑页面的视图:
<h1>Edit a Setlist</h1>
<div class="row">
<div class="span8">
<%=form_for(@setlist) do|f|%>
<%=f.label :date, "Set a date" %>
<span><%=f.date_select :date%><span>
<%=f.label :morning, "Morning" %>
<%=f.radio_button :morning, true %>
<%=f.label :morning, "Evening" %>
<%=f.radio_button :morning, false %>
<h2>Library</h2>
<div>
<%= render 'library' %>
<%= render 'currentSet' %>
</div>
<%=f.submit "Save Setlist", class: "btn btn-large btn-primary" %>
<% end %>
</div>
</div>
编辑集合列表
图书馆
当前集合部分(忽略库部分,因为它是尚未添加任何内容的嵌套表单):
当前设置列表
标题
艺术家
根键
去除
没有歌曲
和分配:
<tr>
<td><%= allocation.song.title %></td>
<td><%= allocation.song.artist %></td>
<td><%= allocation.song.key %></td>
<td><%= link_to "Remove Song", allocation, method: :delete %></td>
</tr>
我不确定这是否相关,但我还添加了实际呈现的html供检查:
<tr>
<td>S</td>
<td>A</td>
<td>K</td>
<td><a href="/allocations/7" data-method="delete" rel="nofollow">Remove Song</a>...
</tr><tr>
<td>S</td>
<td>A</td>
<td>K</td>
<td><a href="/allocations" data-method="delete" rel="nofollow">Remove Song</a></td>
</tr>
s
A.
K
...
s
A.
K
我发现wierd的一部分是,最后一个条目与所有其他条目的不同之处在于,url没有分配id,即最后第二个条目中的/allocations/9与最后一个条目中的/allocations。仅关闭此条目即可:
def edit
@songs = Song.all(order: 'title')
@song = Song.find(params[:id])
@setlist = Setlist.find(params[:id])
@allocations = @setlist.allocations
end
在您的歌曲和设置列表查找器中使用适当的:id(即,不同的查找器)。只需关闭此查找器,然后:
def edit
@songs = Song.all(order: 'title')
@song = Song.find(params[:id])
@setlist = Setlist.find(params[:id])
@allocations = @setlist.allocations
end
在您的歌曲和设置列表查找器中使用适当的:id(即,不是相同的)。您在哪里告诉它显示三首歌曲而不是四首?我没有看到任何循环。数据库中是否只有三首歌曲?那你怎么能得到第四首呢?我告诉它显示所有分配到setlist的歌曲。但是,我知道数据库中每个集合列表只分配了三首歌曲(我已经使用控制台确认了这一点)。
s.allocations.count(0.2ms)从“allocations”中的“allocations”选择count(*),其中“allocations”。“setlist_id”=1=>3
您能给我们完整的视图代码吗?这可能没有关系,但是为什么在finder中使用相同的:id作为歌曲,而在控制器方法中使用Setlist?您在哪里告诉它显示三首歌曲而不是四首?我没有看到任何循环。数据库中是否只有三首歌曲?那你怎么能得到第四首呢?我告诉它显示所有分配到setlist的歌曲。但是,我知道数据库中每个集合列表只分配了三首歌曲(我已经使用控制台确认了这一点)。s.allocations.count(0.2ms)从“allocations”中的“allocations”选择count(*),其中“allocations”。“setlist_id”=1=>3
您能给我们完整的视图代码吗?这可能没有关系,但为什么在控制器方法中的歌曲查找器和设置列表中使用相同的:id?