首页 \ 问答 \ 强参数 - 设计3.0.0和Rails 4.“未允许的参数:名称”(Strong parameters - Devise 3.0.0 and Rails 4. “Unpermitted parameters: name”)

强参数 - 设计3.0.0和Rails 4.“未允许的参数:名称”(Strong parameters - Devise 3.0.0 and Rails 4. “Unpermitted parameters: name”)

我目前正在使用Rails 4和Devise 3.0.0。 我试图在注册表单中添加“名称”的自定义字段并编辑注册表单。 每当我提交表单时,都会出现以下错误:

Unpermitted parameters: name

WARNING: Can't mass-assign protected attributes for User: email, password, password_confirmation.

我知道这与Rails 4处理参数的方式有关,但我不明白我现在应该怎么做。 我已经四处搜索并看到我应该在涉及“params”的User模型中添加一些行。

我的用户模型目前看起来像这样:

class User < ActiveRecord::Base

  devise :database_authenticatable, :registerable, #:recoverable,
          :rememberable, :trackable, :validatable

  attr_accessible :name, :password, :password_confirmation, :remember_me, :email
end

根据Rails 4中如何使用attr_accessible? ,我应该将以下代码添加到“控制器”。

class PeopleController < ApplicationController
  def create
    Person.create(person_params)
  end

  private

  def person_params
    params.require(:person).permit(:name, :age)
  end
end

什么控制器? 这是文字代码吗? 由于我正在处理User,我是否必须使用User.create(user_params)? 而不是Person.create(person_params)?


I am currently using Rails 4 and Devise 3.0.0. I have tried to add a custom field of "Name" to the sign up form and edit registration form. Whenever I submit the form, the following errors arise:

Unpermitted parameters: name

WARNING: Can't mass-assign protected attributes for User: email, password, password_confirmation.

I understand that this has something to do with the way Rails 4 handles parameters, but I do not understand what I am supposed to do about it right now. I have searched around and have seen that I am supposed to add some lines to a User model involving "params."

My user model currently looks like this:

class User < ActiveRecord::Base

  devise :database_authenticatable, :registerable, #:recoverable,
          :rememberable, :trackable, :validatable

  attr_accessible :name, :password, :password_confirmation, :remember_me, :email
end

According to How is attr_accessible used in Rails 4?, I am supposed to add the following code to "The controller."

class PeopleController < ApplicationController
  def create
    Person.create(person_params)
  end

  private

  def person_params
    params.require(:person).permit(:name, :age)
  end
end

What controller? And is this literal code? Since I am dealing with User, do I have to use User.create(user_params)? instead of Person.create(person_params)?


原文:https://stackoverflow.com/questions/19922874
更新时间:2024-02-25 19:02

最满意答案

这是你的答案:

  1. 是的 - 您需要在每个页面上建立连接。 通常,连接参数和脚本位于每个页面上所需的一个文件中。

  2. 查询看起来像:

    SELECT `name` FROM `movies` ORDER BY ID DESC LIMIT 0, 10;
    
  3. 可能会发现一些WordPress插件会做类似的事情。 如果这是一个概念证明,那么我建议你这样做(如果存在这样的插件)。 在PHP / MySQL开发中广泛阅读也是一个好主意,或者聘请可以做到这一点的人,如果你想让这个项目变得真实的话。


Here are your answers:

  1. Yes- you will need to have the connection on every page. Usually, the connection parameters and script is located in one file which you require on every page.

  2. A query looks like:

    SELECT `name` FROM `movies` ORDER BY ID DESC LIMIT 0, 10;
    
  3. You may find some WordPress plugins which will do something similar. If it's a proof of concept, then I suggest you go this way (if such a plugin exists). It's also a good idea to read extensively on PHP/MySQL development or hire someone who can do that if you want to make that project real.

相关问答

更多
  • Heroku提供这些信息用于连接外部来源: https://devcenter.heroku.com/articles/heroku-postgresql#external-connections-ingress 第二个错误信息表明PostgreSQL没有被配置为接受你正在尝试创建的连接。 根据Heroku提供的信息,一个好的猜测是你没有连接SSL。 尝试在你的连接上启用它。 以下是有关在Navicat中使用SSL的说明: http : //mirror.navicat.com/manual/online_ ...
  • 这些是云环境中正常的瞬态错误。 您需要实现重试逻辑/策略。 查看以下答案: 如何在NHibernate中添加Retry Logic来处理SQL Azure中的瞬态故障? These are transient errors normal in a cloud environment. You need to implement a retry logic/policy. Check this answer: How do I add Retry Logic in NHibernate to handle T ...
  • 从我所看到的,CommandCollection属性是受保护的; 试试这个: GetProperty( "CommandCollection", BindingFlags.GetProperty | BindingFlags.Instance | BindingFlags.NonPublic ) Working code to change the database. It is also worth noting that all parts of the connection can be chang ...
  • 如果我理解正确的话,你正在考虑在各种包含的各种功能中保存的复杂代码之间共享连接对象。 在这种情况下,这将是一个坏主意。 如果其他代码可能已经看到需要修改它们,则很难保证连接上的正确状态和设置。 此外,您有时可能会获取获取firehose记录集的代码,并且在调用另一段也需要连接的代码时尚未完成处理。 在这种情况下,您无法共享连接。 让每个原子代码块获得自己的连接会更好。 连接将处于干净的已知状态。 必要时,多个连接可以并行运行。 正如其他人指出的那样,连接创建的成本几乎完全被底层连接池所缓解。 If I un ...
  • 这是你的答案: 是的 - 您需要在每个页面上建立连接。 通常,连接参数和脚本位于每个页面上所需的一个文件中。 查询看起来像: SELECT `name` FROM `movies` ORDER BY ID DESC LIMIT 0, 10; 你可能会发现一些WordPress插件会做类似的事情。 如果这是一个概念证明,那么我建议你这样做(如果存在这样的插件)。 在PHP / MySQL开发中广泛阅读也是一个好主意,或者聘请可以做到这一点的人,如果你想让这个项目变得真实的话。 Here are your a ...
  • 我通常从connection对象本身做到这一点。 from django.db import connections cursor = connections['DATABASE_NAME'].cursor() # replace DATABASE_NAME with the name of your # DATABASE connection, i.e., the appropriate key in the # settings.DATABASES dictionary cursor.execute( ...
  • 最后我设法通过mysql查询获得答案,感谢显示MySQL主机通过SQL命令答案在堆栈我运行以下查询并获得主机名并在我的PHP中使用它现在工作。 select @@hostname; show variables where Variable_name like '%host%'; Finally i managed to get the answer through mysql query thanks to Show MySQL host via SQL Command Answer at s ...
  • 我相信Oracle的Connection类方法是同步的。 看到这个API描述 。 这将是使用相同连接的工件,而不是可配置属性。 如果您需要解决此限制,您可以使用2个连接或查看连接池,如果您需要更灵活的解决方案。 I believe that Oracle's Connection class methods are synchronized. see this api description. This would then be an artifact of using the same connecti ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)