尝试在rails应用程序中显示数据时为nil



我在尝试显示<%= @schedule.title %>时遇到此错误,但当我对<%= @lesson.title %>执行相同操作时,它对@lesson正常工作。

[nil的未定义方法"title":NilClass]1

流量是这样的

-用户注册并创建诊所,诊所属于用户
-诊所有很多从业者,他们属于诊所
-一个从业者有很多课程和时间表,它们属于从业者

当我在课程展示页面上时,会有一个到预订页面的链接。错误发生在这个预订页面上。

我知道它说的是nil,但课程和时间表都是为特定的从业者制定的。

有人知道为什么会发生这种事吗?我不明白为什么我可以访问@lesson而不能访问@schedule。任何帮助都将不胜感激。

路线.rb

resources :clinics do 
resources :practitioners do 
resources :lessons, :lesson_payments, :schedules do
resources :bookings do 
end
end
end
end

schedules_controller.rb

class SchedulesController < ApplicationController
before_action :set_schedule, only: [:show, :edit, :update, :destroy]
# GET /schedules
# GET /schedules.json
def index
@schedules = Schedule.all
end
# GET /schedules/1
# GET /schedules/1.json
def show
end
# GET /schedules/new
def new
@schedule = Schedule.new
end
# GET /schedules/1/edit
def edit
end
# POST /schedules
# POST /schedules.json
def create
@schedule = Schedule.new(schedule_params)
respond_to do |format|
if @schedule.save
format.html { redirect_to clinic_practitioner_schedule_path(id: @schedule.id), notice: 'Schedule was successfully created.' }
format.json { render :show, status: :created, location: @schedule }
else
format.html { render :new }
format.json { render json: @schedule.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /schedules/1
# PATCH/PUT /schedules/1.json
def update
respond_to do |format|
if @schedule.update(schedule_params)
format.html { redirect_to clinic_practitioner_schedule_path(@schedule), notice: 'Schedule was successfully updated.' }
format.json { render :show, status: :ok, location: @schedule }
else
format.html { render :edit }
format.json { render json: @schedule.errors, status: :unprocessable_entity }
end
end
end
# DELETE /schedules/1
# DELETE /schedules/1.json
def destroy
@schedule.destroy
respond_to do |format|
format.html { redirect_to clinic_practitioner_schedules_url, notice: 'Schedule was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_schedule
@schedule = Schedule.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def schedule_params
params.require(:schedule).permit(:title, :start, :end, :practitioner_id, :account_id)
end
end

bookings_controller.rb

class BookingsController < ApplicationController
before_action :set_lesson, only: [:new]
def new
@account = Account.new
@user = User.new 
@patient = Patient.new
@booking = Booking.new
@lesson_payment = LessonPayment.new
@schedule = Schedule.find_by_id(params[:id])
end
def create
create_patient_charge
create_patient_account  
@user = User.new(user_params)
@user.account_id = @account.id
respond_to do |format|
if @user.save
create_patient_profile
create_patient_booking
create_patient_lesson_payment
auto_login(@user)
UserMailer.new_signup_booking_admin(@user, @booking).deliver_later
UserMailer.new_signup_booking_client(@user, @booking).deliver_later
format.html { redirect_to dashboard_url, notice: 'Your account was successfully created.' }
format.json { render :show, status: :created, location: @user }
else
format.html { redirect_back fallback_location: root_path, alert: 'An error occurred while sending this request.' }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
end
private
def set_lesson
@lesson = Lesson.find(params[:lesson_id])
end

def user_params
params.require(:user).permit(:email, :password, :time_zone)
end
def create_patient_account
@account = Account.new()
@account.status = 'active'
@account.account = 'prefix-' + SecureRandom.hex(4)
@account.account_type = 'client'
@account.save
end
def create_patient_profile
@patient = Patient.new()
@patient.firstname = params[:user][:patient][:patient_first_name]
@patient.lastname = params[:user][:patient][:patient_last_name]
@patient.phone = params[:user][:patient][:patient_phone]
@patient.user_id = @user.id
@patient.account_id = @user.account_id
@patient.save
end
def create_patient_booking
@lesson = Lesson.find(params[:user][:booking][:lesson_id])
@booking = Booking.new()
@booking.lesson_id = params[:user][:booking][:lesson_id]
@booking.schedule_id = params[:user][:booking][:schedule_id]
@booking.patient_id = @patient.id
@booking.account_id = @user.account_id
@booking.title = @lesson.title
@booking.cost = @lesson.cost
@booking.status = 'Booked'
@booking.save
@schedule = Schedule.find(params[:user][:booking][:schedule_id])
@booking.practitioner_id = @schedule.practitioner_id
@booking.start = @schedule.start
@booking.refunded = 0
@booking.save
@schedule.title = 'Booked'
@schedule.save
end
def create_patient_lesson_payment
@lesson_payment = LessonPayment.new()
@lesson_payment.status = 'Paid'
@lesson_payment.date = Date.today
@lesson_payment.cost = @lesson.cost
@lesson_payment.service = @lesson.title
@lesson_payment.booking_id = @booking.id
@lesson_payment.account_id = @user.account_id
@lesson_payment.save
end
end

这是我链接到预订页面的地方
show.html.erb

<p id="notice"><%= notice %></p>
<%= link_to new_clinic_practitioner_lesson_booking_path(:lesson_id => @lesson.id), class: "course-btn" do %><i class="fa fa-calendar-plus-o"></i> Book This Lesson<% end %>
<p>
<strong>Image:</strong>
<%= @lesson.image %>
</p>
<p>
<strong>Title:</strong>
<%= @lesson.title %>
</p>
<p>
<strong>Duration:</strong>
<%= @lesson.duration %>
</p>
<p>
<strong>Cost:</strong>
<%= @lesson.cost %>
</p>
<p>
<strong>Category:</strong>
<%= @lesson.category %>
</p>
<p>
<strong>Language:</strong>
<%= @lesson.language %>
</p>
<p>
<strong>Level:</strong>
<%= @lesson.level %>
</p>
<p>
<strong>Description:</strong>
<%= @lesson.description %>
</p>
<p>
<strong>Practitioner:</strong>
<%= @lesson.practitioner_id %>
</p>
<p>
<strong>Account:</strong>
<%= @lesson.account_id %>
</p>
<%= link_to 'Edit', edit_clinic_practitioner_lesson_path(@lesson) %> |
<%= link_to 'Back', clinic_practitioner_lessons_path %>

new.html.erb

<h1>New Booking</h1>
<%= render 'form', booking: @booking %>
<%= link_to 'Back', clinic_practitioner_lesson_bookings_path %>

_form.html.erb

<table class="table table-bordered">
<thead>
<tr>
<th>Slots In The User's Time Zone</th>
<th>Price</th>
<th>Service Provider</th>
<th>Booking Button</th>
</tr>
</thead>
<tbody>
<tr>
<td><%= @lesson.title %></td>
<td><%= @schedule.title %></td>
</tr>
</tbody>
</table>

我注意到,在您的bookings_controllernew方法中,您正在初始化@schedule,如下所示:

@schedule = Schedule.find_by_id(params[:id])

但是,您的参数中没有任何params[:id]

{'clinic_id'=>'50','practitioner_id'=>'27','lesson_id'=>'15'}

这就是为什么你的@schedulenil

假设practitionerschedule之间有一个has_many关联,并且您想显示从业者的第一个schedule的标题,您可以这样做:

@schedule = Schedule.find_by_practitioner_id(params[:practitioner_id])

最新更新