Ruby 未定义的方法`build\u book\u reservation
嗨,我只是想知道为什么会话在刷新时找不到我的预订并在其他控制器中使用它 这是我的应用程序控制器Ruby 未定义的方法`build\u book\u reservation,ruby,ruby-on-rails-3,session,Ruby,Ruby On Rails 3,Session,嗨,我只是想知道为什么会话在刷新时找不到我的预订并在其他控制器中使用它 这是我的应用程序控制器 helper :all # include all helpers, all the time private def current_reservation @reservation ||= Reservation.find(session[:reservation_id]) if session[:reservation_id] #last assigned val
helper :all # include all helpers, all the time
private
def current_reservation
@reservation ||= Reservation.find(session[:reservation_id]) if session[:reservation_id]
#last assigned value will be returned as default.
end
def create_reservation_session(id)
session[:reservation_id] = id
end
def destroy_reservation_session
session[:reservation_id] = nil
end
def new
@book_reservation = BookReservation.new
end
def create
@reservation = Reservation.find(params[:reservation_id])
if @reservation
@book_reservation=@reservation.build_book_reservation(params[:book_reservation])
if @book_reservation.save
#If success set session
create_reservation_session(@reservation.id)
#redirect_to root_url, :notice => "Successfully created book reservation."
else
render :action => 'new'
end
else
render :action => 'new'
end
我想在这里告诉我们
def new
@book_reservation = BookReservation.new
end
def create
@reservation = current_reservation
@book_reservation=@reservation.build_book_reservation(params[:book_reservation])
if @book_reservation.save
#If success set session
create_reservation_session(@reservation.id)
#redirect_to root_url, :notice => "Successfully created book reservation."
else
render :action => 'new'
end
end
它为nil:NilClass`错误引发undefined方法
build\u book\u reservation'
型号/图书预订.rb
belongs_to :reservation
has_one :book_reservation
model/reservation.rb
belongs_to :reservation
has_one :book_reservation
您从未在会话中放入
:reservation\u id
这意味着
session[:reservation\u id]
为nil
和reservation.find(nil)
引发您看到的异常。错误原因是您从未设置会话,但尝试访问它。在应用程序控制器中,使用
private
def current_reservation
@reservation ||= Reservation.find(session[:reservation_id]) if session[:reservation_id]
#last assigned value will be returned as default.
end
def create_reservation_session(id)
session[:reservation_id] = id
end
def destroy_reservation_session
session[:reservation_id] = nil
end
在控制器中
helper :all # include all helpers, all the time
private
def current_reservation
@reservation ||= Reservation.find(session[:reservation_id]) if session[:reservation_id]
#last assigned value will be returned as default.
end
def create_reservation_session(id)
session[:reservation_id] = id
end
def destroy_reservation_session
session[:reservation_id] = nil
end
def new
@book_reservation = BookReservation.new
end
def create
@reservation = Reservation.find(params[:reservation_id])
if @reservation
@book_reservation=@reservation.build_book_reservation(params[:book_reservation])
if @book_reservation.save
#If success set session
create_reservation_session(@reservation.id)
#redirect_to root_url, :notice => "Successfully created book reservation."
else
render :action => 'new'
end
else
render :action => 'new'
end
然后,无论何时需要,您都可以使用当前预订。嗯,这就是您的意思吗<代码>预订=预订。查找(会话[:reservation\u id])为我的愚蠢感到抱歉,我正在学习是的,就是这个。我编辑了答案,试图让答案更清晰。谢谢,只是一个后续问题,我只是编辑了我的帖子,我做得对吗?这应该可以解决眼前的问题,但现在如果有人来到一个页面,因为参数[:reservation\u id]而没有发回预订id,你就会遇到问题可能也没有。hi尝试了你的建议,现在成功了。它为nil:NilClass'提出了不同的错误
未定义的方法
build\u book\u reservation`这可能是一个离题的问题,只是想知道为什么会发生这种情况,只需编辑我的帖子,提前谢谢:)在你的创建方法中添加put@reservation,并告诉我输出内容你得到了什么?什么样的木头?抱歉,我是ruby的新手:(是否将_重定向到注释行?我只是错过了NilClass:Class的最后一条注释未定义的方法
model_name'的输入`