本文介绍了Rails 4 params.require().permit() 不考虑一个属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用 params.require().permit() 方法时遇到问题,因为它没有考虑我为其设置的属性之一,即属性 :on

I am having trouble using the params.require().permit() method because it is not considering one of the attributes I'm setting to it, namely the attribute :on

我拥有的是这个:

def event_recurrence_params
  params.require(:event_recurrence).permit(:room_id, [...], :on, :every)
end

然后,当我调用 event_recurrence_params 时,答案是

Then, when I call event_recurrence_params, the answer is

>> event_recurrence_params
=> {"room_id"=>"1", [...], "every"=>"week"}

即使 params[:event_recurrence] 的值是

>> params[:event_recurrence]
=> {"room_id"=>"1", [...], "on"=>["wednesday"], "every"=>"week"}

如您所见,:on 属性并未考虑在 event_recurrence_params 中,即使该属性被允许且 params[:event_recurrence] 包括它.

As you can see, the :on attribute is not being considered in the event_recurrence_params even though the attribute is permitted and the params[:event_recurrence] include it.

有什么想法吗?我尝试了很多东西,但没有一个奏效.谢谢!

Any ideas? I've tried lots of things but none of it worked.Thank you!

推荐答案

来自文档:

允许的标量类型为 String、Symbol、NilClass、Numeric、TrueClass、FalseClass、日期、时间、DateTime、StringIO、IO、ActionDispatch::Http::UploadedFile 和 Rack::Test::UploadedFile.

声明params中的值必须是一个允许的数组标量值将键映射到空数组:

To declare that the value in params must be an array of permitted scalar values map the key to an empty array:

所以你需要这样做:

params.require(:event_recurrence).permit(:room_id, [...], :every, on: [])

这篇关于Rails 4 params.require().permit() 不考虑一个属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 16:54