2014-02-11 76 views
0

儘管檢查了代碼,但我仍然收到語法錯誤,意外的輸入結束,甚至在刪除代碼並運行它之後,問題似乎無法解決。是代碼:獲取語法錯誤,意外的輸入結束

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 

回答

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

謝謝!!它起作用了,我認爲「除非」將成爲「if和else」聲明的一部分,並且不需要單獨結束。 – user3150377

2

虛假update方法;您錯過了current_user支票的end

重定向確實不是停止執行代碼,您需要返回。

1

實際錯誤 - 錯過endunless條件。

你甚至可以重構你的代碼是這樣

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 
相關問題