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

标签 ruby-on-rails ruby

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

相关文章:

ruby - 如何一次安装多个 ruby gem ?

ruby-on-rails - 使用 validates_presence_of 和 validates_numericality_of 时验证失败

ruby - 瘦异步应用程序示例如何缓冲对主体的响应?

ruby-on-rails - 使用适用于 Ruby on Rails 的 AWS CI/CI 流程进行部署

ruby-on-rails - 在本地主机(OS X 10.5)上设置通配符域?

javascript - 通过 AJAX 将 Rails 变量传递给部分?

ruby-on-rails - Ruby Gem 开发 - 如何使用 ActiveRecord?

ruby-on-rails - 没有要加载的文件——RMagick.rb Rails,Ubuntu

mysql - Ruby 不能需要 mysql

ruby-on-rails - Rails MVC 中基于 CLI 的 "V"?