3

如果尝试从另一个模型访问回形针图像以通过 sql 查询在其视图中显示,它不会显示图像。

我从一个类别控制器尝试了这样的事情,它通过选择框从表单中获取参数,在索引页面中。

category controller

def show

@category = Category.find_by_sql ["select distinct l.* from listings l , categories c, categories_listings cl where c.id = cl.category_id and l.id = cl.listing_id and c.id in (?,?)" ,  params[:c][:id1] , params[:c][:id2]]

end

在显示页面中,我无法从类别控制器访问列表模型中的回形针属性。

category show page      

<% @category.each do |c| %>

<%= c.place %>
<%= image_tag c.placeholder.url(:thumb) %>

<% end %>

Listing和category有habtm关系

4

1 回答 1

0

这是一篇旧帖子,但我遇到了同样的问题,并且使用正确的查询和您已经拥有的相同循环很容易解决,但我的问题出在 Rails 4 中。

对于将来遇到此问题的人,请尝试使用

Category.where(:id => params[:id])
于 2016-01-14T16:20:19.553 回答