本文介绍了NoMethodError在/ undefined方法`name'为nil:NilClass的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尝试了许多解决方案,并提出了一个很好的解决方案,但仍然收到错误。我正在编辑我的整个问题。



我正在尝试使用friendly_id gem创建Friendly URL。
在我的项目中第一个用户需要用devise注册。
Devise将通过

我想让user.name成为Friendly_id候选人。我在我的个人资料模型中尝试过以下代码: -

  class Profile< ActiveRecord :: Base 

extends FriendlyId
friendly_id:user_name,use::slugged

def user_name
user.name
end

但它是错误

现在提交用户表单后。



请说明可能的解决方案,解释。



我的User.rb看起来像

  require'open-uri'

class User< ActiveRecord :: Base
paginates_per 10

验证:name,presence:true,length:{maximum:200}

范围:by_name, - > )do
join(:profile).where('lower(name)LIKE?',%#{name.downcase}%)
end

委托:年龄,::city,:region,:country,to::profile

has_one:profile,depend::destroy
acceptance_nested_attributes_for:profile


def self.new_with_session(params,session)
session_params = {'profile_attributes'=> {}}

provider = session ['devise.provider']
如果提供者&& data = session [devise。#{provider}]
session_params ['name'] = data [:name] if data [:name]
session_params ['email'] = data [:email ]如果data [:email]
session_params ['profile_attributes'] =
{头像:data [:image]}如果data [:image]
end

params.deep_merge!(session_params)
super.tap do | user |
如果auth = Authorization.find_from_session(session,provider)
user.authorizations<<< auth
end
end
end

def password_required?
超级&& registered_manually?
end

def registered_manually?
encrypted_pa​​ssword.present?
end

end

而我的profile.rb看起来像

  class Profile< ActiveRecord :: Base 

extends FriendlyId
friendly_id user.name,use::slugged


belongs_to:user
acceptable_nested_attributes_for:user


验证:网站,allow_blank:true,uri:true

def website =(url_str)
如果url_str.present?
url_str =http://#{url_str},除非url_str [/ ^ https?/]
write_attribute:website,url_str
end
end

end

我认为问题在这里:

 请求参数
{action=>new,controller=>users / registrations}
请建议可能的解决方案和说明。

和用户/注册:

  class Users :: RegistrationsController< Devise :: RegistrationsController 
layout'land'

def create
params [:user] [:profile_attributes] .delete(:place)

end
protected

def after_sign_up_path_for(resource)
welcome_path
end
end

我在个人资料控制器中创建用户

  def load_profile 
@ profile = Profile.friendly.find(params [:id])
if!@profile || @ profile.user.blocked_users.include?(current_user)
redirect_to home_path
else
@user = @ profile.user
end
end

@Rodrigo帮助我找出错误是由于Friendly_id无法创建与用户实例的链接的错误。

解决方案

此行出现错误:

  friendly_id user.name,use::slugged 

变量用户不' t存在于配置文件类范围内。你应该使用这样的东西:

  friendly_id:user_name,use::slugged 

def user_name
user.name
end


I have tried many solution and came up with good one but still getting error. I am editing my whole question.

I am trying to create Friendly URL with friendly_id gem.In my project First user need to signup with devise.Devise will pass some information to profile model with

I want to make user.name to be Friendly_id candidate. I have tried following code in my Profile model:-

 class Profile < ActiveRecord::Base

       extend FriendlyId
      friendly_id :user_name , use: :slugged

       def user_name
       user.name
       end

But it is giving error

now After submitting user form.

Please suggest possible solution with explanation.

My User.rb looks like

require 'open-uri'

class User < ActiveRecord::Base
  paginates_per 10

  validates :name , presence: true, length: { maximum: 200 }

  scope :by_name, ->(name) do
    joins(:profile).where('lower(name) LIKE ?', "%#{name.downcase}%")
  end

  delegate :age, :city, :region, :country, to: :profile

  has_one :profile, dependent: :destroy
  accepts_nested_attributes_for :profile


      def self.new_with_session(params, session)
        session_params = { 'profile_attributes' => {} }

        provider = session['devise.provider']
        if provider && data = session["devise.#{provider}"]
          session_params['name'] = data[:name] if data[:name]
          session_params['email'] = data[:email] if data[:email]
          session_params['profile_attributes'] =
            { avatar: data[:image] } if data[:image]
        end

        params.deep_merge!(session_params)
        super.tap do |user|
          if auth = Authorization.find_from_session(session, provider)
            user.authorizations << auth
          end
        end
      end

      def password_required?
        super && registered_manually?
      end

      def registered_manually?
        encrypted_password.present?
      end

    end

And my profile.rb looks like

class Profile < ActiveRecord::Base

   extend FriendlyId
    friendly_id user.name, use: :slugged


  belongs_to :user
  accepts_nested_attributes_for :user


  validates :website, allow_blank: true, uri: true

  def website=(url_str)
    if url_str.present?
      url_str = "http://#{url_str}" unless url_str[/^https?/]
      write_attribute :website, url_str
    end
  end

end

I think Problem is here:

Request parameters
{"action"=>"new", "controller"=>"users/registrations"}
Please suggest possible solution and explanation.

And users/registration:

class Users::RegistrationsController < Devise::RegistrationsController
  layout 'land'

  def create
    params[:user][:profile_attributes].delete(:place)

  end
  protected

    def after_sign_up_path_for(resource)
      welcome_path
    end
end

I am creating user in profile controller

    def load_profile
      @profile = Profile.friendly.find(params[:id])
      if !@profile || @profile.user.blocked_users.include?(current_user)
        redirect_to home_path
      else
        @user = @profile.user
      end
    end

@Rodrigo helped me find out error that error is due to Friendly_id can't create link with user instance.

解决方案

There is an error on this line:

friendly_id user.name, use: :slugged

The variable user doesn't exists at Profile class scope. You should use something like this:

friendly_id :user_name, use: :slugged

def user_name
  user.name
end

这篇关于NoMethodError在/ undefined方法`name'为nil:NilClass的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 07:39