Reputation: 443
I want show a daycare details on show page but I got this error
NoMethodError : undefined method `find' for nil:NilClass
from daycare controller file and i'm not get any idea. I have mentioned below that error line.
This is my Controller file
class DayCaresController < ApplicationController
before_filter :authenticate_user!
before_action :set_day_care, only: [:show, :edit, :update, :destroy]
# GET /day_cares
# GET /day_cares.json
def index
@day_cares = DayCare.all
end
# GET /day_cares/1
# GET /day_cares/1.json
def show
end
# GET /day_cares/new
def new
@day_care = DayCare.new
end
# GET /day_cares/1/edit
def edit
end
# POST /day_cares
# POST /day_cares.json
def create
@day_care = current_user.build_day_care(day_care_params)
respond_to do |format|
if @day_care.save
UserMailer.welcome_email(@user).deliver
format.html { redirect_to @day_care, :gflash => { :success => 'Day care was successfully created.'} }
format.json { render :show, status: :created, location: @day_care }
else
format.html { render :new }
format.json { render json: @day_care.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /day_cares/1
# PATCH/PUT /day_cares/1.json
def update
respond_to do |format|
if @day_care.update(day_care_params)
format.html { redirect_to @day_care, :gflash => { :success => 'Day care was successfully updated.'} }
format.json { render :show, status: :ok, location: @day_care }
else
format.html { render :edit }
format.json { render json: @day_care.errors, status: :unprocessable_entity }
end
end
end
# DELETE /day_cares/1
# DELETE /day_cares/1.json
def destroy
@day_care.destroy
respond_to do |format|
format.html { redirect_to day_cares_url, :gflash => { :success => 'Day care was successfully destroyed.'} }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions
def set_day_care
@day_care = current_user.day_care.find(params[:id]) # => **I got error this line**
end
# Never trust parameters from the scary internet, only allow the white list through.
def day_care_params
params.require(:day_care).permit(:name, :address, :office_phone, :cell_phone, :logo, :website, :user_id)
end
def dashboard
end
def profile
end
end
Upvotes: 3
Views: 7584
Reputation: 10825
If user has_many: day_cares
then use this name instead of day_care
:
@day_care = current_user.day_cares.where(id: params[:id]).take
or probably as you wrote:
@day_care = current_user.day_cares.find(params[:id])
But with arrays instead of single instance (day_cares
).
Also you can use just:
@day_care = DayCare.find(params[:id])
If you search by id. Or if you need to check that it's users day_care
:
@day_care = DayCare.where(id: params[:id], user: current_user).take
Upvotes: 3
Reputation: 1036
current_user.day_care.find
is not available, because you can only perform queries on plural associations. So given that the model associations are setup correctly as:
class User < ActiveRecord:Base
has_many :day_cares
...
end
the solution is probably just to resolve the spelling error from
`current_user.day_care.find` #wrong!
to
`current_user.day_cares.find` #right!
Upvotes: 0