为什么我无法在 Rails 3 中使用此参数创建?

发布于 2024-10-27 07:49:54 字数 1353 浏览 3 评论 0原文

我有一个带有以下行的方法:

26         User.create!(:email => "[email protected]", :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

即使在 Rails 控制台中,当我尝试通过传递 params :linkedin_uid 来创建它时,它仍然显示为零。 :(

这是该行的上下文:

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 => "[email protected]", :linkedin_uid => linkedin_uid, :password => Devise.friendly_token[0,20]) 
 27       end
 28   end

I have a method with the following line:

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

Here is my controller:

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

Even from the rails console when I try to create it by passing the params :linkedin_uid, it still comes out nil. :(

Here is the context for the line:

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 => "[email protected]", :linkedin_uid => linkedin_uid, :password => Devise.friendly_token[0,20]) 
 27       end
 28   end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

£冰雨忧蓝° 2024-11-03 07:49:54

我假设这是来自 Devise 的用户模型。这里的问题可能是您还没有创建 linkedin_uid mass-可分配。您需要在您的 User 模型中通过以下方式执行此操作:

attr_accessible :linkedin_uid

每当您使用 attr_accessible 定义字段时,这些字段将成为唯一将通过 批量分配 函数。

I am assuming that this is a User model from Devise. The problem here is likely that you haven't made linkedin_uid mass-assignable. You will need to do that in your User model via

attr_accessible :linkedin_uid

Whenever you defined fields with attr_accessible those become the ONLY fields that will be updated via mass assignment functions.

忆沫 2024-11-03 07:49:54

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

Check your data type for the linkedin_uid column in the database to make sure it can support the data you are giving it in the create method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文