Html 我得到了什么未定义的方法'each';对于nil:Rails中的NilClass?
我在做搜索。但是我在搜索视图中的Html 我得到了什么未定义的方法'each';对于nil:Rails中的NilClass?,html,ruby-on-rails,Html,Ruby On Rails,我在做搜索。但是我在搜索视图中的每个都会出错 视图/housing/index.html.erb的每个都工作正常 但是下面是views/search/search\u housing.html.erbwehre我得到了每个: <tbody> <% @housings.each do |housing| %> <tr> <td><%=link_to "#{housing.title}", housing_path(housi
每个都会出错
视图/housing/index.html.erb的每个
都工作正常
但是下面是views/search/search\u housing.html.erb
wehre我得到了每个:
<tbody>
<% @housings.each do |housing| %>
<tr>
<td><%=link_to "#{housing.title}", housing_path(housing.slug) %></td>
<td><%= housing.category.name %></td>
下面是我的房管员
class HousingsController < ApplicationController
before_action :set_housing, only: [:show, :edit, :update, :destroy]
# GET /housings
# GET /housings.json
def index
@housings = Housing.all.order(created_at: :desc).paginate(page: params[:page], per_page: 10)
end
# GET /housings/1
# GET /housings/1.json
def show
end
# GET /housings/new
def new
@housing = Housing.new
end
# GET /housings/1/edit
def edit
if not @housing.user_email == current_user.email || current_user.email == "codeinflash@gmail.com"
redirect_to @housing
end
end
# POST /housings
# POST /housings.json
def create
@housing = Housing.new(housing_params)
@housing.user_email = current_user.email
respond_to do |format|
if @housing.save
format.html { redirect_to @housing }
flash[:success] = "Housing was successfully created."
else
format.html { render :new }
format.json { render json: @housing.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /housings/1
# PATCH/PUT /housings/1.json
def update
respond_to do |format|
if @housing.update(housing_params)
format.html { redirect_to @housing }
format.json { render :show, status: :ok, location: @housing }
flash[:success] = "Housing was successfully updated."
else
format.html { render :edit }
format.json { render json: @housing.errors, status: :unprocessable_entity }
end
end
end
# DELETE /housings/1
# DELETE /housings/1.json
def destroy
@housing.destroy
respond_to do |format|
format.html { redirect_to housings_url }
format.json { head :no_content }
flash[:alert] = "Housing was successfully destroyed."
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_housing
@housing = Housing.friendly.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def housing_params
params.require(:housing).permit(:title, :type, :description, :location, :user_email, :created_at, :category_id, :slug)
end
end
class SearchController < ApplicationController
def search_housing
@housings = Housing.search((params[:search].present? ? params[:search] : '*')).records.order(created_at: :desc)
# if params[:search].nil?
# @housings = Housing.all.order(created_at: :desc)
# else
# @housings = Housing.search params[:search]
# end
end
end
类外壳控制器
下面是我的搜索控制器
class HousingsController < ApplicationController
before_action :set_housing, only: [:show, :edit, :update, :destroy]
# GET /housings
# GET /housings.json
def index
@housings = Housing.all.order(created_at: :desc).paginate(page: params[:page], per_page: 10)
end
# GET /housings/1
# GET /housings/1.json
def show
end
# GET /housings/new
def new
@housing = Housing.new
end
# GET /housings/1/edit
def edit
if not @housing.user_email == current_user.email || current_user.email == "codeinflash@gmail.com"
redirect_to @housing
end
end
# POST /housings
# POST /housings.json
def create
@housing = Housing.new(housing_params)
@housing.user_email = current_user.email
respond_to do |format|
if @housing.save
format.html { redirect_to @housing }
flash[:success] = "Housing was successfully created."
else
format.html { render :new }
format.json { render json: @housing.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /housings/1
# PATCH/PUT /housings/1.json
def update
respond_to do |format|
if @housing.update(housing_params)
format.html { redirect_to @housing }
format.json { render :show, status: :ok, location: @housing }
flash[:success] = "Housing was successfully updated."
else
format.html { render :edit }
format.json { render json: @housing.errors, status: :unprocessable_entity }
end
end
end
# DELETE /housings/1
# DELETE /housings/1.json
def destroy
@housing.destroy
respond_to do |format|
format.html { redirect_to housings_url }
format.json { head :no_content }
flash[:alert] = "Housing was successfully destroyed."
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_housing
@housing = Housing.friendly.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def housing_params
params.require(:housing).permit(:title, :type, :description, :location, :user_email, :created_at, :category_id, :slug)
end
end
class SearchController < ApplicationController
def search_housing
@housings = Housing.search((params[:search].present? ? params[:search] : '*')).records.order(created_at: :desc)
# if params[:search].nil?
# @housings = Housing.all.order(created_at: :desc)
# else
# @housings = Housing.search params[:search]
# end
end
end
class SearchController
此错误来自您的视图:
<% @housings.each do |housing| %>
当您试图访问@housing时,它是零
试着把
在控制器中引发exception或binding.pry调试器,并检查查询结果
class SearchController
我想您对住房的查询。search返回零
要安装binding.pry调试器,请检查此链接
干杯。哦,上帝啊,我找到了路。。
我只是忘了在搜索控制器的末尾加一个end
end
谢谢大家 在您的搜索控制器中,当您单独注释掉“@houses=Housing.all.order(created_at::desc)”行时,它是否起作用?如果是这样的话,您会得到一个SyntaxError,因此它肯定是其他东西。很高兴你把它修好了!