我正在 Railssite 上做一个 Ruby...我在重定向到另一个表单时遇到了问题...我已经做了很多事情来修复它,但我无法找出代码中的错误..任何人都请帮我解决它..拜托
错误
←[36m2014-04-23T05:46:13.280376+00:00 app[web.1]:←[0m NoMethodError (undefined m
ethod `invitation_type' for nil:NilClass):
←[36m2014-04-23T05:46:13.280378+00:00 app[web.1]:←[0m app/controllers/vectors_controller.rb in invited_new
代码:
class VectorsController < ApplicationController
def create
ui = UserInvitation.where(hash_code: session[:hash_code]).first
@vector = Vector.new(vector_params.merge({user_id: current_user.id, vector_type: ui.invitation_type}))
respond_to do |format|
if @vector.save
ui.completed_registration = true
ui.save
session[:hash_code] = nil
flash[:notice] = "You're all set! Welcome to Availendar!"
format.html {redirect_to vector_path(@vector.id)}
else
flash[:alert] = "Oops, looks like there's been a problem. Please correct it and try again."
format.html {render action: :invited_new}
end
end
end
def invited_new
@user_invitation = UserInvitation.where(hash_code: session[:hash_code]).first
#raise ArgumentError "Cannot find invitation with hash_code #{session[:hash_code]}" unless @user_invitation.present?
@vector = Vector.new(user: current_user, vector_type: @user_invitation.invitation_type)
@vector.build_address
end
////create action///
定义创建 ui = UserInvitation.where(hash_code: session[:hash_code]).first @vector = Vector.new(vector_params.merge({user_id: current_user.id, vendor_type: ui.invitation_type})) respond_to 做|格式| 如果@vector.save ui.completed_registration = true 用户界面.保存 session [:hash_code] = 无 format.html {redirect_to vector_path(@vector.id)} 别的 format.html {呈现操作::invited_new} 结尾 结尾 结束
在您的 invited_new
操作中,@user_invitation
由于显示此错误而变为空白,这是解决方案
def invited_new
@user_invitation = UserInvitation.where(hash_code: session[:hash_code]).first
unless @user_invitation.blank?
@vector = Vector.new(user: current_user, vector_type: @user_invitation.invitation_type)
@vector.build_address
end
end
我是一名优秀的程序员,十分优秀!