未定义的方法`坚持?'为true:TrueClass

时间:2017-01-17 04:17:59

标签: ruby-on-rails facebook model controller omniauth

我得到这个错误,我不知道为什么。像这样的类似问题无法解决我的问题。

  • 可以来自user = User.where(:email => auth.info.email).first
  • 可以来自where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
  • 或甚至在omniauth_controller
  • persisted?部分

这个错误的可能选项或解决方案是什么?谢谢!! ^^

Omniauth_controller :哪里坚持?错误发生在。

def facebook

    puts "1111111111 yayayay"
    # raise request.env["omniauth.params"].inspect

        user = User.from_omniauth((request.env["omniauth.auth"]), (request.env["omniauth.params"]), (request.remote_ip))

        if user.persisted?
      puts "3333333 okayay"
            sign_in_and_redirect @user, :event => :authentication
            set_flash_message(:notice, :success, :kind => "Facebook") if is_navigational_format?
        else
      puts "wtf wtf wtf wtf wtf"
            session["devise.facebook_data"] = request.env["omniauth.auth"]
            redirect_to new_user_registration_url

        end
    end

用户模型我在其中创建条带验证和facebook omniauth。

#gathering the info from social media when making an account.
def self.from_omniauth(auth, auth_params, request)

 anonymous_username = "NewUser#{User.last.id + 100}"
 generated_password = Devise.friendly_token[0,20]

 user = User.where(:email => auth.info.email).first

 puts "auth params are #{auth_params}"
 puts "user type is #{auth_params["user_type"]}"



 if user
  return user
else
  where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
    user.confirmed_at = Time.now
    user.fullname = auth.info.name
    user.provider = auth.provider
    user.user_type = auth_params["user_type"] 
    user.uid = auth.uid
    user.username = anonymous_username
    user.email = auth.info.email
    if auth.info.image.present?
     avatar_url = process_uri(auth.info.image)
     user.avatar = URI.parse(avatar_url)
   end 
   user.password = generated_password 
   user.save
   user
 end

#reviser account creation
user = User.where(:email => auth.info.email).first
puts "before the if #{user}  and #{user.user_type}"
if user.user_type == 'reviser'
  puts 'this is reviser starting to create account'
require "stripe"
  email = auth.info.email

  begin

  Stripe.api_key = ENV['STRIPE_SECRET_KEY']

    account = Stripe::Account.create(
    {
      :country => 'US',
      :managed => true,
      :tos_acceptance => {
        :date => Time.now.to_i,
        :ip => request
        },
      :transfer_schedule => {
        :interval => 'manual'
        },
        :email => email,

        :legal_entity => {
          :type => 'individual'
          }

        }
        )

    verification = Verification.create!(
        user_id: user.id,
        country: 'US',
        email: email,
        terms_of_service: true


        )


        puts "success!1: #{account.id}"
        puts "success!4: #{account.keys.secret}"
        puts "success!5: #{account.keys.publishable}"
        verification.update_attributes account_id: account.id





      rescue Stripe::InvalidRequestError => e
        if e.message == "An account with this email already exists."
         redirect_to stripe_path, alert: "An account with this email already exists."
       else
        puts "#{e.message}"
        flash[:error]= e.message
      end

    rescue Stripe::AuthenticationError => e
      redirect_to stripe_path, alert: "oops! Please let us know about this error! Please Try Again Later!"
  # Authentication with Stripe's API failed
  # (maybe you changed API keys recently)
rescue Stripe::APIConnectionError => e
  redirect_to stripe_path, alert: "oops! Something went wrong! Please Try Again!"
  # Network communication with Stripe failed
rescue Stripe::StripeError => e
  redirect_to stripe_path, alert: "oops! Something went wrong! Please Try Again!"
  # Display a very generic error to the user, and maybe send
  # yourself an email

end
end



end
end

1 个答案:

答案 0 :(得分:2)

由于行from_omniauth,您的方法user.save返回true。最后返回user,如:

def self.from_omniauth(auth, auth_params, request)

  anonymous_username = "NewUser#{User.last.id + 100}"
  generated_password = Devise.friendly_token[0,20]

  user = User.where(:email => auth.info.email).first

  puts "auth params are #{auth_params}"
  puts "user type is #{auth_params["user_type"]}"



  if user
    return user
  else
    where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
      user.confirmed_at = Time.now
      user.fullname = auth.info.name
      user.provider = auth.provider
      user.user_type = auth_params["user_type"]
      user.uid = auth.uid
      user.username = anonymous_username
      user.email = auth.info.email
      if auth.info.image.present?
        avatar_url = process_uri(auth.info.image)
        user.avatar = URI.parse(avatar_url)
      end
      user.password = generated_password
      user.save
    end

#reviser account creation
    user = User.where(:email => auth.info.email).first
    puts "before the if #{user}  and #{user.user_type}"
    if user.user_type == 'reviser'
      puts 'this is reviser starting to create account'
      require "stripe"
      email = auth.info.email

      begin

        Stripe.api_key = ENV['STRIPE_SECRET_KEY']

        account = Stripe::Account.create(
            {
                :country => 'US',
                :managed => true,
                :tos_acceptance => {
                    :date => Time.now.to_i,
                    :ip => request
                },
                :transfer_schedule => {
                    :interval => 'manual'
                },
                :email => email,

                :legal_entity => {
                    :type => 'individual'
                }

            }
        )

        verification = Verification.create!(
            user_id: user.id,
            country: 'US',
            email: email,
            terms_of_service: true


        )


        puts "success!1: #{account.id}"
        puts "success!4: #{account.keys.secret}"
        puts "success!5: #{account.keys.publishable}"
        verification.update_attributes account_id: account.id





      rescue Stripe::InvalidRequestError => e
        if e.message == "An account with this email already exists."
          redirect_to stripe_path, alert: "An account with this email already exists."
        else
          puts "#{e.message}"
          flash[:error]= e.message
        end

      rescue Stripe::AuthenticationError => e
        redirect_to stripe_path, alert: "oops! Please let us know about this error! Please Try Again Later!"
          # Authentication with Stripe's API failed
          # (maybe you changed API keys recently)
      rescue Stripe::APIConnectionError => e
        redirect_to stripe_path, alert: "oops! Something went wrong! Please Try Again!"
          # Network communication with Stripe failed
      rescue Stripe::StripeError => e
        redirect_to stripe_path, alert: "oops! Something went wrong! Please Try Again!"
        # Display a very generic error to the user, and maybe send
        # yourself an email

      end
    end

    user

  end
end