我不断收到语法错误,意外的输入结束,尽管检查了代码,甚至在删除代码并运行它之后,问题似乎无法解决。代码是:
class BookingsController < ApplicationController
before_filter :authenticate_user!
def new
@booking = Booking.new
end
def create
@booking = current_user.booking.create(booking_params)
if @booking.save
flash[:alert] = "You have now booked a date"
redirect_to root_path
else
flash[:alert] = "Error:booking did not save"
redirect_to root_path
render 'new'
end
end
def show
@booking = Booking.find(params[:id])
end
def edit
@booking = Booking.find(params[:id])
unless @booking.user == current_user
redirect_to root_path
end
end
def update
@booking = Booking.find(params[:id])
unless @booking.user == current_user
redirect_to root_path
if @booking.update_attributes(booking_params)
redirect_to root_path
flash[:notice] = "You have edited your post"
else
render 'edit'
flash[:alert] = "something went wrong"
end
end
def destroy
@booking = Booking.find(params[:id])
@booking.destroy
redirect_to root_path
end
private
def booking_params
params.require(:booking).permit(:content)
end
end
答案 0 :(得分:2)
您错过了end
关键字
def update
@booking = Booking.find(params[:id])
unless @booking.user == current_user
redirect_to root_path
end # <~~~ missed in your code
if @booking.update_attributes(booking_params)
redirect_to root_path
flash[:notice] = "You have edited your post"
else
render 'edit'
flash[:alert] = "something went wrong"
end
end
答案 1 :(得分:2)
Bogus update
方法;您错过了end
支票的current_user
。
同样重定向不停止执行代码,您需要返回。
答案 2 :(得分:1)
实际错误 - end
条件后错过unless
。
您甚至可以像这样重构代码
def update
@booking = Booking.find(params[:id])
redirect_to root_path unless @booking.user == current_user
if @booking.update_attributes(booking_params)
redirect_to root_path
flash[:notice] = "You have edited your post"
else
render 'edit'
flash[:alert] = "something went wrong"
end
end