Rails params 属性 - 使用控制器中的 params 字段

用户7671402

我有以下的强PARAMS在我developers_controller命名location_params

  def location_params
    params.require(:location).permit(:country, {:ads_attributes => [:remote, :days]})
  end

当我在代码中设置调试断点时,为了测试如何使用字段 :remote 和 :days,我必须使用以下代码控制台中获取:remote:days

location_params[:ads_attributes]["2"][:remote]

我不明白也不知道如何删除这个["2"]索引。我想params用以下代码选择location_params[:ads_attributes][:remote]

这是控制台中的结果 location_params[:ads_attributes]

<ActionController::Parameters 
{"0"=><ActionController::Parameters {} permitted: true>, 
"1"=><ActionController::Parameters {"days"=>"12"} permitted: true>,
"2"=><ActionController::Parameters {"remote"=>"1"} permitted: true>}
permitted: true>

我确实在 stackoverflow 上进行了搜索,但无法找到解决此特定索引问题的方法。

大卫

我建议更好地修复表单视图,在此处添加这些数字:

<%= link_to_add_association "Add another", f, :ads %>
<%= f.fields_for :ads do |ff| %>
      <%= ff.form_group ::remote do |ad| %>
          <%= ad.label :remote %> *
          <%= ad.text_field :remote %>
      <% end %>
      <%= ff.form_group :days do |ad| %>
          <%= ad.label :days %> *
          <%= ad.text_field :days %>
      <% end %>
      <%= link_to_remove_association "remove", ff %>
<% end %>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Rails:控制器中“<entity>_params”的含义

使用strong_params在Rails 4控制器中创建两个对象

Ruby on Rails-禁止在params上使用属性?

Rails-如何访问嵌套的params属性?

Rails:模型中所有字段的默认序列化器和safe_params

rails params.merge在link_to中

使用URL_PARAMS查询Rails模型

如果 Rails 控制器已被继承,则覆盖 permited_params

Rails 4 Params丢失,Rspec测试嵌套资源控制器更新方法

Rails多个params(:filter)请求

无法读取 React 中未定义的属性“params”

如何更改 params require 中的一个属性?

当param =''时使用Beautiful Soup访问属性的params

如何在Ruby on Rails中的params数组中更改值?

Rails-从params哈希中获取所有键

Rails 4.2.x-在模型中使用params哈希数据

在Ruby on Rails控制器的“ params.require”方法中,是否有一种更动态的方法来列出编号的参数?

ReactJS:如何使用Params?

在Rails控制器中更新属性

Rails路线:更改资源标识符的路径名?始终使用params [:model_id]而不是params [:id]

ruby on rails params hash及其内容

Rails params [:id]不断返回相同的id

Rails:什么决定了传递的“params”的名称?

Rails 4新表单默认来自params

Rails如何发送params [:commit]值

Ruby PG Gem使用TextEncoder :: Array用于exec_params中的SELECT WHERE字段名

Neo4j在控制器中使用params [:search]并在模型中使用

如何在控制器中获取HTTP请求和查询字符串PARAMS

如何在CodeIgniter中使用params /为钩子获取控制器?