尝试将嵌套的自定义属性Profile(一个Mongoid文档)添加到我的设计用户类.提交Devise注册表单时,它还应创建用户和相应的Profile对象.
我希望最终结果在我的MongoDB中看起来像这样:
用户:
{
# Devise fields:
"email": "my@email.com",
...
# Custom field
"profile" : "<object_id>"
}
Run Code Online (Sandbox Code Playgroud)
轮廓:
{
"first_name": "Dave",
....
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,每当我提交注册时,我都会在控制台中收到此消息.它成功创建了一个用户,但无法创建关联的配置文件.
Started POST "/" for 127.0.0.1 at 2013-04-20 23:37:10 -0400
Processing by Users::RegistrationsController#create as HTML
Parameters:
{"utf8"=>"?",
"authenticity_token"=>"awN2GU8EYEfisU0",
"user"=>
{"profile_attributes"=>
{"first_name"=>"Dave",
"birthday(2i)"=>"4",
"birthday(3i)"=>"21",
"birthday(1i)"=>"1933",
"occupation_title"=>"Software Developer"},
"password"=>"[FILTERED]",
"password_confirmation"=>"[FILTERED]",
"email"=>"my@email.com"}}
Unpermitted parameters: profile_attributes
Run Code Online (Sandbox Code Playgroud)
我有设置:
车型/ user.rb:
class User
include Mongoid::Document
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, …Run Code Online (Sandbox Code Playgroud) ruby-on-rails mongodb devise strong-parameters ruby-on-rails-4
我正在使用Rails 4和Devise 3.0.0,并且很擅长使用这些新的强大参数.我使用Devise wiki上的文档为User模型添加了一个用户名.我遇到的问题是Rails 4中的强参数变化.
如何将:login属性添加到用户模型以启用用户名或电子邮件登录?
有没有办法在Rails 4中为树结构指定任意深度强参数?例如,我如何指定如下内容:
{
"node": {
"name": "parent",
"nodes": [
{ "name": "child1", "nodes": []},
{ "name": "child2", "nodes": [
{"name": "grandchild", "nodes": []}
]}
]
}
}
Run Code Online (Sandbox Code Playgroud)
允许每个节点具有name属性和nodes属性?
我有一个has_many关联的多选框.params进来如下:
foo_ids: ["1", "2", "3"]
Run Code Online (Sandbox Code Playgroud)
使用强参数,我不允许这个属性,因为我想自己授权,所以人们不能只是把它们想要的东西放进去.
def update
bar.foos = authorized_foos
bar.update(baz_params)
respond_with bar
end
private
def authorized_foos
foos = Foo.find(params[:baz][:foo_ids])
foos.each do |foo|
authorize foo, :manage?
end
end
Run Code Online (Sandbox Code Playgroud)
这种方法将迫使我找到所有的foos,循环遍历它们,并单独授权每个foos.是否有更简单的方法来管理has_many授权,最好是使用Pundit gem?
当使用rails 4.0强参数时,如何允许这样的json?
{
"user":
{
"first_name":"Jello"
},
"users_to_employer":[
{
"start_date":"2013-09-03T16:45:27+02:00",
"end_date":"2013-09-10T16:45:27+02:00",
"employer":{"company_name":"Telenor"}
},
{
"start_date":"2013-09-17T16:45:27+02:00",
"end_date":null,
"employer":{"company_name":"Erixon"}
}
]
}
Run Code Online (Sandbox Code Playgroud)
我尝试了以下内容:
params.require(:users_to_employers => []).permit(
:start_date,
:end_date => nil,
:employer => [
:company_name
])
Run Code Online (Sandbox Code Playgroud)
但它没有用.
我有一些Backbone模型,有很多深层嵌套的属性.
在我保存这些模型之前,我没有遇到任何问题,但我最近更新到Rails 4并开始使用强参数.现在我收到了一个found unpermitted parameters错误.
attr_accessible 简单地忽略了不允许的参数,但我猜强参数的工作方式不同?
有没有办法让Rails简单地忽略未经许可的参数,只更新允许的参数?
从我的Backbone模型中删除所有未经许可的参数将是一个巨大的痛苦.
是否有一种简单的方法可以在Spree中向控制器添加新的允许参数而不更改默认值?
我正在尝试更改OrdersController中的orders_params方法.
很长一段时间的Java开发人员 - ror的新手 - 我在Rails 4中构建了一个JSON REST API,我遇到了使用强参数的问题.当我对我的一个端点执行HTTP POST请求时,我希望我的控制器需要主对象,需要它的一些属性,如果存在某些其他属性,它也会返回它们.看到那些需要和允许方法返回哈希,我想知道什么是允许和要求同一对象的某些属性的最佳实践.我应该合并两个哈希吗?非常感谢您的投入,因为我花了很多时间研究这个问题.
我一次提交一个包含2-4个对象的表单,具体取决于父项的数量.我意识到这可能是非常规的,但我真的希望用户能够在一个表单上一次编辑所有对象.在我的表格上,我正在做:
<%= simple_fields_for "derps[]", derp do |f| %>
<% end %>
Run Code Online (Sandbox Code Playgroud)
然后我在控制器中这样做:
def update
@derps = []
@rejects = []
derps_params.each do |key, hash|
derp = Derp.find(key)
derp.assign_attributes(hash)
@rejects << derp unless derp.save
end
if @rejects.empty?
redirect_to @parent, flash: {success: 'Derps were successfully updated.'}
else
@derps = @rejects
render :edit
end
end
Run Code Online (Sandbox Code Playgroud)
让我们说有两个对象 - params正在通过:
"derps"=>{"1"=>{"attribute"=>"39", "another_attribute"=>"serp", "a_third_attribute"=>"yerp"}, "2"=>{"attribute"=>"30", "another_attribute"=>"49", }}
Run Code Online (Sandbox Code Playgroud)
我在Rails 3中工作时没有强大的参数.我正在升级到rails 4,我正在努力解决这个问题 - 我不断获得"未经许可的参数:1,2"
我假设我需要做以下事情:
def mashes_params
params.require(:derps).permit(
id: []
Run Code Online (Sandbox Code Playgroud)
要么
def mashes_params
params.require(:derps).permit(
:id,
Run Code Online (Sandbox Code Playgroud)
沿着这些方向的东西,但我已经尝试过每一个我能想到没有运气的方式.
这里有什么想法?
我们使用strongloop流程管理器在生产盒上运行我的环回应用程序,并使用它sl-deploy来部署代码.
我们正面临以下问题: -
FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - process out of memory
Run Code Online (Sandbox Code Playgroud)
在谷歌搜索我发现上述问题可以通过增加max_old_space_size节点选项值以某种方式解决.
我做了以下更改来运行应用程序,并在sl-pm进程之前传递了此变量,但仍面临同样的问题,并发现我的slc进程仍然使用默认值max_old_space_size
/opt/node/bin/node **--max_old_space_size=3072**
/opt/node/lib/node_modules/strong-pm/bin/sl-pm.js --listen 8701 --base
/var/lib/strong-pm --base-port 3000 --driver direct
Run Code Online (Sandbox Code Playgroud)