强参数 - 设计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
最满意答案
这是你的答案:
是的 - 您需要在每个页面上建立连接。 通常,连接参数和脚本位于每个页面上所需的一个文件中。
查询看起来像:
SELECT `name` FROM `movies` ORDER BY ID DESC LIMIT 0, 10;
你可能会发现一些WordPress插件会做类似的事情。 如果这是一个概念证明,那么我建议你这样做(如果存在这样的插件)。 在PHP / MySQL开发中广泛阅读也是一个好主意,或者聘请可以做到这一点的人,如果你想让这个项目变得真实的话。
Here are your answers:
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.
A query looks like:
SELECT `name` FROM `movies` ORDER BY ID DESC LIMIT 0, 10;
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.
相关问答
更多-
TCP/IP模型是一个________。[2023-10-02]
a -
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 ...
-
下列中不属于面向对象的编程语言的是?[2022-05-30]
a -
从我所看到的,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 ...
-
从网站设置与数据库的连接并运行查询(Setting Up a Connection to a Database from a Website and Running Queries)[2023-08-14]
这是你的答案: 是的 - 您需要在每个页面上建立连接。 通常,连接参数和脚本位于每个页面上所需的一个文件中。 查询看起来像: 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 ...