Rails 4 – 研究不显示

我有一个应用程序列出一些“Tuto”,我希望用户可以按类别或关键字搜索任何Tuto。 它就像关键字的魅力,但不适用于类别……

即使我选择一个类别,一切仍然显示…

类别是在控制台中创建的,如Category.create(name: "Ruby")等等……

这是我的观点索引

 .container .row h1.text-gray Tutorials .row.search_banner .col-xs-3 =form_tag tutos_path, :method => 'get' do =text_field_tag :search, params[:search], placeholder:"Search by keywords" =submit_tag "Search", class:'btn btn-xs btn-default btn-search' .col-xs-3 =form_tag tutos_path, :method => 'get' do =select_tag :filter, options_for_select(Category.all.map{|c| c.name}, params[:filter]) =submit_tag "Search", class:"btn btn-xs btn-default btn-search" .col-xs-3 -if user_signed_in? = link_to "Create a tuto", new_tuto_path, class:"btn btn-success" br br #tutos.transitions-enabled -@tutos.each do |tuto| .box.panel-default = link_to image_tag(image_by_category(tuto.category.try(:name))), tuto_path(tuto) h3 = link_to tuto.title, tuto_path(tuto), class:"title-link" h6 | Created by: span = tuto.user.full_name br span.glyphicon.glyphicon-heart span = tuto.get_upvotes.size 

模特托托

 class Tuto < ActiveRecord::Base acts_as_votable belongs_to :user belongs_to :category validates :category_id, presence: true def self.search(search) if search where(["title LIKE ?","%#{search}%"]) else all end end end 

编辑了索引动作,不确定elsif更合乎逻辑?

tutos控制器

 class TutosController < ApplicationController before_action :authenticate_user!, only: [:new, :create] before_action :set_tuto, only: [:show, :edit, :update, :destroy, :upvote] def index #binding.pry if params[:search] || params[:filter] @tutos = Tuto.search(params[:search]).includes(:user, :category) elsif params[:search].present? @tutos = Tuto.all.includes(:user, :category) elsif params[:filter].present? @categories = Category.search(params[:filter]) else @categories = Category.all end end def show @tuto = Tuto.find(params[:id]) @user = User.all end def new @tuto = Tuto.new end def edit end def create @tuto = Tuto.new(tuto_params) @tuto.user_id = current_user.id respond_to do |format| if @tuto.save flash[:success] = "Test" format.html { redirect_to @tuto, notice: 'Tuto was successfully created.' } format.json { render :show, status: :created, location: @tuto } else format.html { render :new } format.json { render json: @tuto.errors, status: :unprocessable_entity } end end end def update respond_to do |format| if @tuto.update(tuto_params) format.html { redirect_to @tuto, notice: 'Tuto was successfully updated.' } format.json { render :show, status: :ok, location: @tuto } else format.html { render :edit } format.json { render json: @tuto.errors, status: :unprocessable_entity } end end end def destroy @tuto.destroy respond_to do |format| format.html { redirect_to tutos_url, notice: 'Tuto was successfully destroyed.' } format.json { head :no_content } end end def upvote @tuto.upvote_by current_user redirect_to :back end private def set_tuto @tuto = Tuto.find(params[:id]) end def tuto_params params.require(:tuto).permit(:title, :content, :id, :user_id, :category_id) end end 

我不确定我使用binding.pry的技巧,但是当我模拟一个例如AWS-Amazon的类别时,我就有了这些。

 Started GET "/tutos?utf8=%E2%9C%93&filter=AWS-Amazon&commit=Search" for ::1 at 2016-09-28 03:30:19 +0200 Processing by TutosController#index as HTML Parameters: {"utf8"=>"✓", "filter"=>"AWS-Amazon", "commit"=>"Search"} From: Tutos/app/controllers/tutos_controller.rb @ line 7 TutosController#index: 6: def index => 7: binding.pry 8: if params[:search] || [:filter] 9: @tutos = Tuto.search(params[:search]).includes(:user, :category) 10: elsif 11: @tutos = Tuto.all.includes(:user, :category) 12: elsif 13: @categories = Category.search(params[:filter]) 14: else 15: @categories = Category.all 16: end 17: end [1] pry(#)> params => {"utf8"=>"✓", "filter"=>"AWS-Amazon", "commit"=>"Search", "controller"=>"tutos", "action"=>"index"} [2] pry(#)> 

当我按关键字搜索效果如此之好时,为什么我的类别filter没有提供任何内容? 希望得到你的帮助! 问候

我的架构可能有所帮助

 ActiveRecord::Schema.define(version: 20160920133801) do create_table "categories", force: :cascade do |t| t.string "name" t.text "description" t.string "image" t.datetime "created_at", null: false t.datetime "updated_at", null: false end create_table "tutos", force: :cascade do |t| t.datetime "created_at", null: false t.datetime "updated_at", null: false t.string "title" t.text "content" t.integer "user_id" t.integer "category_id" end add_index "tutos", ["user_id"], name: "index_tutos_on_user_id" create_table "users", force: :cascade do |t| t.string "email", default: "", null: false t.string "encrypted_password", default: "", null: false t.string "reset_password_token" t.datetime "reset_password_sent_at" t.datetime "remember_created_at" t.integer "sign_in_count", default: 0, null: false t.datetime "current_sign_in_at" t.datetime "last_sign_in_at" t.string "current_sign_in_ip" t.string "last_sign_in_ip" t.datetime "created_at", null: false t.datetime "updated_at", null: false t.string "first_name" t.string "last_name" t.boolean "admin" end add_index "users", ["email"], name: "index_users_on_email", unique: true add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true create_table "votes", force: :cascade do |t| t.integer "votable_id" t.string "votable_type" t.integer "voter_id" t.string "voter_type" t.boolean "vote_flag" t.string "vote_scope" t.integer "vote_weight" t.datetime "created_at" t.datetime "updated_at" end add_index "votes", ["votable_id", "votable_type", "vote_scope"], name: "index_votes_on_votable_id_and_votable_type_and_vote_scope" add_index "votes", ["voter_id", "voter_type", "vote_scope"], name: "index_votes_on_voter_id_and_voter_type_and_vote_scope" end