0

Here is my controller

before_action :seo_url_product,only: :product

def seo_url_product
    if params[:id]
         @posts = Post.friendly.where(sub_category_id: params[:id]).paginate(page:   params[:page], per_page: 2).to_a
     end
end

Here is my view

<% category.sub_categories.each do |sub_category| %>
    <li><%= link_to sub_category.name, controller: :posts, action: :product, id: sub_category %></li>
<% end %>

I can see friendly url when i click sub_categories but i could not see any result.

4

1 回答 1

1

我认为问题在于您尝试使用slug非真实身份进行查询。如您所知,friendly_id 使用 slug。所以你应该先找到真实的ID然后尝试查询。

params[:id]在查询中插入,where您可以使用以下内容。

sub_category_id = Sub_category.friendly.find(params[:id]) # This find id insted of slug
@posts = Post.friendly.where(sub_category_id: sub_category_id).paginate(page:   params[:page], per_page: 2).to_a
于 2014-06-11T06:32:45.607 回答