gpt4 book ai didi

ruby-on-rails - Ruby 错误 :No Method error. .nil:NilClass 的未定义方法 invitation_type

转载 作者:太空宇宙 更新时间:2023-11-03 17:34:05 24 4
gpt4 key购买 nike

我正在 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

关于ruby-on-rails - Ruby 错误 :No Method error. .nil:NilClass 的未定义方法 invitation_type,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23235913/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com