现在,我尝试使用group_path(@group)设置路由,但收到了错误消息,例如
ActiveRecord::RecordNotFound at /groups/%23%3CActiveRecord::Relation:0x007fd6cf362538%3E
Couldn't find Group with id=#<ActiveRecord::Relation:0x00fds6cf362538>
这是我的代码。我想设置一个指向groups / show /:id的链接。为什么会出现此错误?你能告诉我怎么解决这个问题吗?
☆index.html.erb
<% if @items.present? %>
<% @items.each do |i| %>
<% i_attr = i.get_element('ItemAttributes') %>
<tr>
<td> <%= link_to image_tag(i.get('SmallImage/URL'), {:style => 'border: none;'}), i_attr.get('DetailPageURL') %></td>
<td> <%= link_to i_attr.get('Title'), i_attr.get('DetailPageURL') %></td>
<td> <%= i_attr.get('Author') %></td>
<td> <%= i_attr.get('PublicationDate')%></td>
<td> <%= i_attr.get('Publisher') %></td>
<td> <%= i_attr.get('NumberOfPages')%></td>
<td>
<% if @existing_groups_isbns.include? i_attr.get('ISBN') %>
<% @existing_groups_isbns.each do |isbn| %>
<% if isbn == i_attr.get('ISBN') %>
<% @group = Group.where(:isbn =>isbn) %>
<%= link_to '既存ページへ' , group_path(@group) %>
<% end %>
<% end %>
<% else %><!-- if includes?==-->
<%= link_to '新規作成', {:controller => 'groups', :action => 'new', :name => i.get('ItemAttributes/Title'),:author => i.get('ItemAttributes/Author'), :publish => i.get('ItemAttributes/Publisher'), :published => i.get('ItemAttributes/PublicationDate'), :isbn => i.get('ItemAttributes/ISBN'), :page => i.get('ItemAttributes/NumberOfPages'), :imageurl=>i.get('MediumImage/URL')} ,class: "btn btn-midium btn-primary"%>
<% end %><!--if includes?-->
</td>
</tr>
<% end %><!-- @items.each do-->
<% else %><!--if @items.present?-->
見つかりませんでした。
<% end %><!-- if @items.present?-->
☆index_controller
def index
@keyword = params[:keyword]
if @keyword.present?
Amazon::Ecs.debug = true
res = Amazon::Ecs.item_search(params[:keyword],
:search_index => 'All', :response_group => 'Medium')
@items = res.items
search_isbns = @items.map{ |isbns| isbns.get('ItemAttributes/ISBN')}
search_asins = @items.map{ |asins| asins.get('ItemAttributes/ASIN')}
@existing_groups_isbns = Group.select(:isbn).where(:isbn => search_isbns).map(&:isbn)
@existing_groups_ids = Group.where(:isbn => search_isbns).map{|g| g.id}
end
答案 0 :(得分:1)
替换
<% @group = Group.where(:isbn =>isbn) %>
与
<% @group = Group.where(:isbn =>isbn).first %>
因为您需要一个具有ActiveRecord关系的对象