rails类别未在视图中显示

时间:2017-05-28 02:29:30

标签: ruby-on-rails

大家好我试图为每个创建的音符制作类别。我在导航栏中显示所有类别但由于某些原因,当我点击类别时,它只显示所有帖子而不是该类别中的帖子。有人可以帮忙吗?感谢

notes_controller:

class NotesController < ApplicationController
  before_action :find_note, only: [:show, :edit, :update, :destroy, :upvote,  :downvote]
  before_action :authenticate_user!, except: [:index, :show]
  before_action :load_activities, only: [:index, :show, :new, :edit]

  def index
    if params[:category].blank?
      @notes = Note.all.order("created_at DESC")
    else 
      @category_id = Category.find_by(name: params[:category]).id
      @notes=Note.where(category_id:@category_id).order("created_at DESC")
    end

    if params[:search]
      @notes = Note.search(params[:search]).order("created_at DESC")
    else
      @notes = Note.all.order("created_at DESC")
    end
  end

  def show
    @comments = Comment.where(note_id: @note)
    @random_post = Note.where.not(id:@note).order("RANDOM()").first
  end

  def new
    @note = current_user.notes.build
  end

  def create
    @note = current_user.notes.build(note_params)
    if @note.save
      redirect_to @note
    else
      render 'new'
    end
  end

  def edit
  end

  def update
    if @note.update(note_params)
      flash[:notice] = "Your message was successfully sent!"
      redirect_to @note
    else
      render 'edit'
    end
  end

  def destroy
    @note.destroy
    redirect_to notes_path
  end

  def upvote
    @note.upvote_by current_user
    respond_to do |format|
      format.html { redirect_to :back }
      format.js{ render 'upvote' }
    end
  end

  def downvote
    @note.downvote_from current_user
    respond_to do |format|
      format.html { redirect_to :back }
      format.js{ render 'downvote' }
    end
  end

  private

  def find_note
    @note = Note.find(params[:id])
  end

  def note_params
    params.require(:note).permit(:title, :content, :category_id, :avatar)
  end

  def load_activities
    @activities = PublicActivity::Activity.order('created_at DESC').limit(20)
  end
end

视图/笔记/ index.html.erb:

<% if user_signed_in? %>
  <p id="intro">
    Hello
    <%= current_user.name %>
    <h1>  
      <%=@category_id%> 
    </h1>
    <br/>
    <span>
      Share your inspiratikkkon and see what's inspiring others.
    </span>
  </p>
<% else %>
  <p id="intro">
    What's your muse?
    <br/>
    <span>
      Share your inspiration and see what's inspiring others.
    </span>
  </p>
<% end %>

<div id="posts">
<% unless @notes.blank? %>
  <% @notes.each do |note| %>
    <div class="post">
      <div class="post_image">
        <%= image_tag note.avatar.url(:medium)%>
      </div>
      <div class="post_content">
        <div class="title">
          <h2>
            <%= link_to note.title, note %>
          </h2>
        </div>
        <div class="data clearfix">
          <p class="username" style="font-size:20px;">
            <%= link_to note.user.name, note.user %>
            <span class="user-menu_profile-pic"><%= image_tag note.user.avatar.url(:thumb) %></span>
          </p>
          <p class="buttons">
            <span>
              <i class="fa fa-comments-o" style="font-size:30px;"></i>
            </span>
            <%= link_to like_note_path(note), method: :put, class: "btn btn-default btn-sm" do %>
              <span class="glyphicon glyphicon-chevron-up"></span>
              yeah your life sucks!
              <%= (note.get_upvotes.size)%>
            <% end %>
            <%= link_to dislike_note_path(note), method: :put, class: "btn btn-default btn-sm" do %>
              <span class="glyphicon glyphicon-chevron-down"></span>
              You deserve it!
              <%= (note.get_downvotes.size) %>
            <% end %>
          </p>
        </div>
      </div>
    </div>
  <% end %>
</div>
<% else %>
  <h2>Add a Note</h2>
  asdfasdfasdf
  ;ldldldldldl
  <p>
    It appears you haven't created any stories yet... 
    Lets fix that. Why don't you go ahead and create your story.
  </p>
  <button>
    <%= link_to "New Note", new_note_path %>
  </button>
<% end %>

layout.html.erb:

<li>
  <a href="#">it1d</a>
</li>
<
<% Category.all.each do |category| %>
  <li 'divider-vertical'>
    <%= link_to category.name, notes_path(category: category.name) %>
  </li>
<%end%>

routes.rb中:

authenticated :user do
  root "notes#index", as: "authenticated_root"
end
resources :notes do
  member do
    put "like", to: "notes#upvote"
    put "dislike", to: "notes#downvote"
  end
  resources :comments
end

1 个答案:

答案 0 :(得分:0)

问题在于您的index操作,您执行了两次if评估,一次评估params[:category],另一项评估params[:search],并设置@notes:< / p>

def index
  if params[:category].blank?
    @notes = Note.all.order("created_at DESC")
  else 
    @category_id = Category.find_by(name: params[:category]).id
    @notes=Note.where(category_id:@category_id).order("created_at DESC")
  end

  if params[:search]
    @notes = Note.search(params[:search]).order("created_at DESC")
  else
    @notes = Note.all.order("created_at DESC")
  end
end

@notes将始终设置为@notes = Note.all.order("created_at DESC"),因为生成的链接未提供search参数。

即使提供了search参数,@notes仍然会被@notes = Note.search(params[:search]).order("created_at DESC")覆盖。

因此您需要更改控制器中的逻辑,否则@notes将永远不会设置为Note.where(category_id:@category_id).order("created_at DESC")

与您的问题无关,但值得一提:

您可以在 form.html.erb 中更改此行:

<%= link_to category.name, notes_path(category: category.id) %>

然后,在你的index行动中,你将会这样做:

if params[:category].blank?
  @notes = Note.all.order("created_at DESC")
else 
  @notes=Note.where(category_id: params[:category]).order("created_at DESC")
end

并为DB保存额外的查询以设置@category_id