【问题标题】:why am I not able to create with this parameter in Rails 3?为什么我无法在 Rails 3 中使用此参数进行创建?
【发布时间】:2011-07-24 03:56:33
【问题描述】:

我有一个方法如下:

26         User.create!(:email => "token@email.com", :linkedin_uid => linkedin_uid, :password => Devise.friendly_token[0,20]) 

这是我的控制器:

20   def create
 21     @user = User.new(params[:user])
 22     if @user.save
 23       flash[:notice] = "Successfully created user."
 24       redirect_to @user
 25     else
 26       render :action => 'new'
 27     end
 28   end

即使在我尝试通过传递参数 :linkedin_uid 来创建它时从 rails 控制台,它仍然是 nil。 :(

这是该行的上下文:

18   def self.find_for_linked_in_oauth(omniauth_hash, signed_in_resource=nil)
 19     debugger
 20     #omniauth_hash is a hash passed in from env["omniauth_hash"] by callback controller
 21     linkedin_uid = omniauth_hash['uid']
 22       debugger
 23       if user = User.find_by_linkedin_uid(linkedin_uid)
 24         user
 25       else # Create an user with a stub password. 
 26         User.create!(:email => "token@email.com", :linkedin_uid => linkedin_uid, :password => Devise.friendly_token[0,20]) 
 27       end
 28   end

【问题讨论】:

    标签: ruby-on-rails oauth devise


    【解决方案1】:

    我假设这是一个来自 Devise 的用户模型。这里的问题很可能是你没有发linkedin_uidmass-assignable。您需要在 User 模型中通过

    attr_accessible :linkedin_uid
    

    每当您使用 attr_accessible 定义字段时,这些字段将成为唯一将通过 mass assignment 函数更新的字段。

    【讨论】:

    • 嗯,为什么 :email 和 :password 可以通过,但linkedin_uid 没有?
    • 我以为你的意思是没有创建对象。现在看来很明显这是一个attr_accessible 问题。我已经修改了答案。
    【解决方案2】:

    检查数据库中linkedin_uid 列的数据类型,确保它可以支持您在create 方法中提供的数据。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-01
      • 1970-01-01
      • 2013-12-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多