控制器接收 JSON 对象
{
user: {
name: "string",
details: {
info1: "string",
info2: []
}
}
}
Run Code Online (Sandbox Code Playgroud)
在权限控制器知道可以允许某些定义的字段(如名称)和具有所有嵌套属性的散列字段详细信息(也可以使用数组)。对于这种情况,正确的解决方案是什么?
糟糕的解决方案
permit无法使用,因为我必须选择用户允许的字段
tap do |whitelisted|无法使用,因为它不会使该字段“允许”
下面的情况不能是用户,因为使用数组不起作用
details_keys = params[:user][:details].keys
params.require(:user).permit(:name, details: details_keys)
大家下午好
我对我们计划升级到 Rails 4 的 Rails 3 应用程序进行了强大的参数化。一些控制器使用对象params不仅保存嵌套散列,还保存数组内散列内的数组内的散列等。更改数据结构的性质将是太激烈了,我们希望理想地让它返回相同的数据结构,但是强参数化
下面是一个 JSON 示例:
"my_example" => {
"options" =>
[{"id" => "1"
"name" => "claire"
"keywords" =>
["foo", "bar"]
},
{"id" => "2",
"name" => "marie",
"keywords =>
["baz"]
}],
"wut" => "I know, right?"
}
Run Code Online (Sandbox Code Playgroud)
但为了增加乐趣,该keywords数组可以包含任何字符串。我已经读过,这很棘手并且在其他版本的 Rails 中得到支持,但无论如何。
关于使用 Strong_parameters gem 制作复杂的数据结构有什么一般的经验法则吗?我知道 Rails 4 和 5 能更好地处理这个问题,但我很好奇。
我在 Rails Minitest 中遇到permit强参数错误。当我运行正常流程时(除了测试),这工作得很好。 每当我运行测试时,它都会给我这个奇怪的错误。
为什么这仅在测试时不起作用
parameter
@params_return_type = {:client_notes_and_action_items=>[{"notes_action_type"=>"return_help", "title"=>"test title", "description"=>"", "to_do_action_items_attributes"=>{"0"=>{"linked_item_id"=>"987", "linked_item_type"=>"client_purchases_shipping_detail", "initial_request"=>"true"}}, "estimated_completion"=>'Mon, 30 Mar 2020', "assigned_to"=>"45", "assigned_by"=>"41"}],"client_id"=>"76576"}
Run Code Online (Sandbox Code Playgroud)
运行测试时出错
NoMethodError: undefined method `permit' for #<Hash:0x007facebc78c98>
Run Code Online (Sandbox Code Playgroud)
你的意思?打印
this is my strong parameter
私人的
def self.client_notes_and_action_item_params(params)
params.permit( :client_id, :notes_action_type, :category, :description, :user_id, :image,
:comments, :status, :estimated_completion, :actual_completion, :title,
to_do_action_items_attributes: [:id, :linked_item_id, :linked_item_type, :deleted,
:initial_request])
end
Run Code Online (Sandbox Code Playgroud)
我的api代码是这样的
params[:client_notes_and_action_items].each do |client_notes_and_action_item|
ClientNotesAndActionItem.transaction do
action_item = ClientNotesAndActionItem.new(client_notes_and_action_item_params(client_notes_and_action_item))
Run Code Online (Sandbox Code Playgroud)
代码在代码的最后一行中断
任何想法是什么问题。如果强参数是一个问题,那么我应该对所有参数都出现此错误,正常流程仅在 Minitest 测试中断时工作正常。
我将数据从电子表格导入到应用程序数据库.
数据按行解析.每个解析的行看起来像:
{:department=>{
:school=>"Graduate School of Business",
:name=>"Graduate School of Business",
:program=>"MBA Program",
:url=>"http://www.gsb.stanford.edu/mba",
:intro=>"The Stanford MBA Program is a two-year, full-time, residential program.",
:students_number=>"Approximately 80 annually",
:students_with_aids_number=>nil,
:gre_scroe=>nil,
:toefl_score=>nil,
:world_rank=>nil,
:us_rank=>nil,
:without_aid_deadline=>nil,
:with_aid_deadline=>nil,
:salary=>nil,
:institute_id=>1}
}
Run Code Online (Sandbox Code Playgroud)
要创建一个部门:
# att is the hash shown above
department = Department.new(department_params(att))
if !department.save
puts "Error: \n department can't be save: #{department.errors.full_messages}"
end
def department_params params
params.require(:department).permit(:name,:url,:institute_id)
end
Run Code Online (Sandbox Code Playgroud)
但是我得到了错误:
no implicit conversion of Symbol into String
Run Code Online (Sandbox Code Playgroud)
哪个指向
params.require(:department).permit(:name,:url,:institute_id)
Run Code Online (Sandbox Code Playgroud)
我该怎么办呢?谢谢!
我很难让rails 4使用nested_attributes和serialize.我有:
class Client < ActiveRecord::Base
belongs_to :event
serialize :phones
end
class Event < ActiveRecord::Base
has_one :client
end
class EventsController < ApplicationController
...
def event_params
params.permit(client_attributes: [:phones])
end
end
Run Code Online (Sandbox Code Playgroud)
当我通过活动时:
{client_attributes: { phones: 'string'}}
Run Code Online (Sandbox Code Playgroud)
它有效,但当我尝试
{client_attributes: { phones: [{phone_1_hash},{phone_2_hash}]}}
Run Code Online (Sandbox Code Playgroud)
我收到'未经许可的参数:手机'消息并且字段未保存...
我试过用
class EventsController < ApplicationController
...
def event_params
params.permit(client_attributes: [phones:[]])
end
end
Run Code Online (Sandbox Code Playgroud)
要么
class Client < ActiveRecord::Base
belongs_to :event
serialize :phones, Array
end
Run Code Online (Sandbox Code Playgroud)
但到目前为止没有任何帮助 任何建议,将不胜感激.谢谢!
serialization nested-attributes strong-parameters ruby-on-rails-4
我无法通过构建获得rails 4,强大的参数来处理嵌套资源.任何建议都会非常受欢迎.
RSPEC告诉我
创建动作创建动作失败/错误:click_button"创建动作"NoMethodError:未定义的方法permit' for "create":String
# ./app/controllers/actions_controller.rb:24:inaction_params'#./ app/console/actions_controller.rb:10:in create'
# ./spec/features/creating_actions_spec.rb:16:inblock(2 levels)in'
我的浏览器告诉我:
ActionsController#中的NoMethodError为"create"创建未定义的方法`permit':String
提取的来源(第24行):
def action_params params.require(:action).permit(:title,:description)结束
动作控制器包含:
def create
@action = @project.actions.build(action_params)
if @action.save
flash[:notice] = "Action has been created."
redirect_to [@project, @action]
else
flash[:alert] = "Action has not been created."
render "new"
end
end
private
def action_params
params.require(:action).permit(:title, :description)
end
Run Code Online (Sandbox Code Playgroud)
我正在使用嵌套资源:
resources :projects do
resources :actions
end
Run Code Online (Sandbox Code Playgroud)
表格如下:
<%= form_for [@project, @action] do |f| %>
<% if @action.errors.any? %>
<div id="error_explanation" >
<h2><%= …Run Code Online (Sandbox Code Playgroud) 我有一个评论控制器和一个产品控制器.它在带有Forbidden Attributes错误的注释控制器的创建操作时失败.
我已从模型中删除了所有attr_accessible并将它们移动到控制器.还是有些不对劲.我无法弄清楚是什么.请任何人都可以告诉我我错过了什么.
@comment = @commentable.comments.new(params[:comment]) <--- Fail here
Run Code Online (Sandbox Code Playgroud)
Live Shell o/p来自更好的错误:
>> params[:comment]
=> {"content"=>"thanks"}
>> @commentable
=> #<Product id: 1, title: "Coffee Mug", description: "<p> This coffee mug blah blah", image_url: "http://coffee.com/en/8/82/The_P...", price: #<BigDecimal:7ff8769a9e00,'0.999E1',18(45)>, tags: nil, created_at: "2014-02-24 14:49:34", updated_at: "2014-02-24 14:49:34">
>> @commentable.comments
=> #<ActiveRecord::Associations::CollectionProxy []>
>> @commentable.comments.new(params[:comment])
!! #<ActiveModel::ForbiddenAttributesError: ActiveModel::ForbiddenAttributesError>
>>
Run Code Online (Sandbox Code Playgroud)
评论控制器:
class CommentsController < ApplicationController
def new
@comment = @commentable.comments.new
end
def create
@comment = @commentable.comments.new(params[:comment]) <-- fail here
if @comment.save
redirect_to product_path(params[:product_id])
else
render …Run Code Online (Sandbox Code Playgroud) 我想使用accepts_nested_attributes_for通过一个表单在两个不同的表(场地和停车场)上创建记录.我希望用户能够创建一个新的场地,并通过复选框指定该场地可用的停车选项.当我提交表单时,会创建包含模型(场所)的记录,但嵌套模型(停放)没有任何反应.当我检查来自服务器的响应时,我发现我遇到了"未经许可的参数:parking_attributes",虽然我不知道为什么.
我已经看过Railscast#196嵌套模型表,并尝试了多个stackoverflow帖子的建议(Rails 4嵌套属性不保存,Rails 4:fields_for in fields_for,Rails 4 - 嵌套模型(2级)不保存).如果有人可以帮助我,我会非常感激.
我已经包括了两个模型,场地控制器,场地/新视图以及服务器的响应.
venue.rb
class Venue < ActiveRecord::Base
has_many :parkings
accepts_nested_attributes_for :parkings
end
Run Code Online (Sandbox Code Playgroud)
parking.rb
class Parking < ActiveRecord::Base
belongs_to :venue
end
Run Code Online (Sandbox Code Playgroud)
venues_controller.rb
class VenuesController < ApplicationController
def index
@venues = Venue.all
end
def new
@venue = Venue.new
end
def create
@venue = Venue.new(venue_params)
if @venue.save
redirect_to @venue, flash: { success: "Venue successfully created" }
else
render :new
end
end
def show
@venue = Venue.find(params[:id])
end …Run Code Online (Sandbox Code Playgroud) checkbox nested-attributes fields-for strong-parameters ruby-on-rails-4
我有一个Profile模型,具有以下内容:
has_many :transcripts, dependent: :destroy
accepts_nested_attributes_for :transcripts, allow_destroy: true
Run Code Online (Sandbox Code Playgroud)
在我的Transcript模型上,我有以下内容:
include TranscriptUploader[:attachment]
Run Code Online (Sandbox Code Playgroud)
这是一个Shrine Uploader mount.
在我app/views/profile/_form.html.erb,我有以下内容:
<div id="transcripts" class="text-center">
<% if @profile.transcripts.any? %>
<% @profile.transcripts.each do |transcript| %>
<%= link_to "Click to view Transcript", transcript.url %>
<% end %>
<% end %>
<%= f.simple_fields_for :transcripts do |transcript| %>
<%= render 'transcript_fields', f: transcript %>
<% end %>
<br />
<div class="links">
<%= link_to_add_association 'Add Transcript', f, :transcripts, class: "btn btn-success add-transcript-button" %>
</div>
</div> …Run Code Online (Sandbox Code Playgroud) params.require(:xxx).permit(:a, :b, :c)为我工作的中途,就是如果params[:xxx][:d]有,它被删除permit.我想有一个例外.
要么我没有找到相关文档,要么缺乏文档.在这种情况下,我设置了什么选项(我希望有一个全局设置)来获取异常?