【问题标题】:Fail to load posts from a certain category无法加载某个类别的帖子
【发布时间】:2015-12-01 05:09:49
【问题描述】:

我正在建立一个像 www.reddit.com 这样的网站。我有可以属于八个类别的帖子。当用户点击某个类别时,属于该类别的帖子将显示出来。我能够创建允许用户执行此操作的功能,并且一切正常。然后我使用 ransack gem 制作了一个搜索栏,我的搜索功能工作得很好。但是现在我不能再点击类别来显示相应的帖子了。我不知道我是怎么搞砸的,我花了相当多的时间试图弄清楚。如果哪位能指出问题,不胜感激,谢谢!

帖子控制器:

  def index
    @search_posts = Post.search(params[:q])
    @search_results = @search_posts.result.paginate(:page => params[:page], :per_page => 10)

    if params[:category_id]
      @category = Category.find params[:category_id]
      @posts = @category.posts.paginate(:page => params[:page], :per_page => 10)
    else
      @posts = Post.paginate(:page => params[:page], :per_page => 10)
    end

  end

我觉得它会自动执行 else 块中的代码,但我不确定为什么,因为当我检查日志时会出现 params[:category_id]:

Started GET "/categories/7/posts" for ::1 at 2015-12-01 00:00:11 -0500
Processing by PostsController#index as HTML
  Parameters: {"category_id"=>"7"}
  Category Load (0.2ms)  SELECT  "categories".* FROM "categories" WHERE "categories"."id" = ? LIMIT 1  [["id", 7]]
   (0.3ms)  SELECT COUNT(*) FROM "posts"
  Post Load (0.3ms)  SELECT  "posts".* FROM "posts" LIMIT 10 OFFSET 0

帖子#索引视图:

<%= search_form_for @search_posts do |f| %>
  <%= f.text_field :title_cont %>
  <%= f.submit "Search" %>
<% end %>

<% if @search_results.any? %>

  <% @search_results.each do |post| %>

    <div id="each_post">
      <p id="post_title"><%= link_to post.title, strip_url(post.url) %></p>
      <p>Submitted <%= time_ago_in_words(post.created_at) %> ago by <%= link_to post.user.name, user_path(post.user) %></p>
    </div>

  <% end %>

  <%= will_paginate @search_results %>

<% else %>

  <% @posts.each do |post| %>

      <div id="each_post">
        <p id="post_title"><%= link_to post.title, strip_url(post.url) %></p>
        <p>Submitted <%= time_ago_in_words(post.created_at) %> ago by <%= link_to post.user.name, user_path(post.user) %></p>
      </div>

  <% end %>

  <%= will_paginate @posts %>

<% end %>

我确定这不是传递类别 ID 的正确方法,但我让它像这样工作:

<%= link_to 'Gadgets', category_posts_path(1) %>
<%= link_to 'Sports', category_posts_path(2) %>
<%= link_to 'Gaming', category_posts_path(3) %>
<%= link_to 'Pics', category_posts_path(4) %>
<%= link_to 'World News', category_posts_path(5) %>
<%= link_to 'Videos', category_posts_path(6) %>
<%= link_to 'Aww', category_posts_path(7) %>
<%= link_to 'Music', category_posts_path(8) %>

帖子嵌套在类别下:

  resources :categories do
    resources :posts
  end

【问题讨论】:

  • params[:q] 为零时,您是否仔细检查了@search_results 是否为空?
  • 感谢杨老师的回复。你给了我将 @search_results 设置为 nil 的想法,我让它工作了。谢谢!

标签: ruby-on-rails ruby-on-rails-3 ruby-on-rails-4


【解决方案1】:

我可以通过在控制器中将 '@search_results' 设置为 nil 来解决这个问题:

if params[:category_id]
  @search_results = nil
  @category = Category.find(params[:category_id])
  @posts = @category.posts.paginate(:page => params[:page], :per_page => 10)
else
  @posts = Post.paginate(:page => params[:page], :per_page => 10)
end

并将 if 条件语句更改为:

<% if @search_results != nil %>

希望这可以帮助遇到类似问题的人。

【讨论】:

    猜你喜欢
    • 2020-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多