I am doing search. But I get error at each
in search view
each
for views/housing/index.html.erb is working fine.
But below is views/search/search_housing.html.erb
wehre I am getting each
:
<tbody>
<% @housings.each do |housing| %>
<tr>
<td><%=link_to "#{housing.title}", housing_path(housing.slug) %></td>
<td><%= housing.category.name %></td>
below is my Housing controller
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
Below is my search controller
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
This error comes from your view:
<% @housings.each do |housing| %>
while you are trying to access to @housing which is nil.
Try to put
raise exception or binding.pry debugger in your controller and check what is the result of your query.
class SearchController < ApplicationController
def search_housing
@housings = Housing.search((params[:search].present? ? params[:search] : '*')).records.order(created_at: :desc)
binding.pry
end
end
I suppose your query for Housing.search returns nil.
To install binding.pry debugger check this link https://github.com/rweng/pry-rails
Cheers.
OH MAY GOD, I found way.. I just forgot to put an end
end of the search controller...
Thanks guys!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.