【发布时间】:2016-06-19 13:42:24
【问题描述】:
我想根据用户属性展示产品。所以基本上我是根据用户属性从产品类中检索一个对象。我不知道如何在我的视图中为这些创建一个列表。如何按属性呈现它们?以下是我现在展示所有产品的视图:
<% @products.each do |product| %>
<%= render "product_row", product: product, order_item: @order_item %>
<% end %>
我的用户.rb
def products_for_gender
if gender == 'male'
Product.where("product_id: '9'")
elsif gender == 'female'
Product.where("product_id: '10'")
else
Product.where("product_id: '11'")
end
end
还有我的控制器:
def index
@products = Product.all
@products = current_user.products_for_gender
@order_item = current_order.order_items.new
end
现在我在尝试访问产品视图时遇到此错误:
ActiveRecord::StatementInvalid in Products#index
Showing /home/ubuntu/workspace/app/views/products/index.html.erb where line #31 raised:
SQLite3::SQLException: unrecognized token: ":": SELECT "products".* FROM "products" WHERE "products"."active" = ? AND (product_id: '9')
【问题讨论】:
标签: ruby-on-rails model-view-controller views