我试图通过关键字搜索一些标题,我需要帮助设置我的实例变量tutos ....(似乎我需要两个实例@tutos ......但我不知道怎么办?< / p>
我试过了:
@tutos = Tuto.all.includes(:user, :category) || Tuto.search(params[:search])
但我有同样的错误......
即使我设置了实例变量,我也有undefined method each' for nil:NilClass
...感谢您的帮助
我的控制器
class TutosController < ApplicationController
before_action :authenticate_user!, only: [:new, :create]
before_action :set_tuto, only: [:show, :edit, :update, :destroy, :upvote]
def index
@tutos = Tuto.all.includes(:user, :category)
keyword_search
@categories = Category.all
end
def keyword_search
@tutos = Tuto.search(params[:search])
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
我的索引
.container
.row
.col-xs-12
h1.text-gray Tutorials
h4 Search by Title
=form_tag tutos_path, :method => 'get' do
=text_field_tag :search, params[:search]
=submit_tag "Search", class:'btn btn-default'
= form_tag '/tutos', method: 'get' do
select[name="category.id"]
- @categories.each do |category|
| <option value="
= category.id
| ">
= category.name
input[type="submit" value="Search", class="btn btn-default"]
.col-xs-12
-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
br
br
tuto模型
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
答案 0 :(得分:0)
像这样更新你的index
。你不需要keyword_search
。
def index
if params[:search]
@tutos = Tuto.search(params[:search]).includes(:user, :category)
else
@tutos = Tuto.all.includes(:user, :category)
end
@categories = Category.all
end
希望这有帮助。
答案 1 :(得分:0)
def index
@tutos = Tuto.all.includes(:user, :category)
keyword_search // **Here you are overriding @tutos... First have no meaning always**
@categories = Category.all
end
def keyword_search
@tutos = Tuto.search(params[:search])
end
2 - &gt;
def self.search(search)
if search
where(["title LIKE ?","%#{search}%"])
else
all
end
end
如果搜索到的数据不可用,它会给出nil,所以你应该处理这个...否则每个都会出现nil