【问题标题】:Rails form button doesn't make params I am expectingRails 表单按钮不会产生我期望的参数
【发布时间】:2017-06-22 13:34:06
【问题描述】:

我一直在为我的表单使用带有提交功能的收音机,但我决定改用普通按钮。但是我的表单没有像我的strong_params 所期望的那样在我的参数中设置通常的key: value 对。

我拥有并且期待新形式的参数:

{  "utf8"=>"✓", 
  "_method"=>"patch", 
  "model_name" => { "event"=>"university_decided" }, 
  "university_decision"=>"university_accepted", 
  "controller"=>"unis", 
  "action"=>"update", 
  "id"=>"5"
}

但是我得到了:

{  "utf8"=>"✓", 
  "_method"=>"patch", 
  "event"=>"university_decided" , 
  "university_decision"=>"university_accepted", 
  "controller"=>"unis", 
  "action"=>"update", 
  "id"=>"5"
}

与部分外的表单一起使用的原始部分:

  %p.material-font-title
    %em What did the university decide?

  .radio
    = f.label :university_decision_university_accepted do
      = f.radio_button :university_decision, "university_accepted"
      Accepted
  .radio
    = f.label :university_decision_university_declined do
      = f.radio_button :university_decision, "university_declined"
      Declined
  = f.button "Mark university decided",
    value: "event_name",
    name: "event",
    class: "btn btn-ghost-info"

这是我在部分中的新表单:

= form_for model_name do |f|
  = hidden_field_tag "event", "event_name"

  %p.material-font-title
    %em What did the university decide?

  = f.button "Accepted",
    value: "university_accepted",
    name: "university_decision",
    class: "btn btn-default"

从控制器视图调用:

 = render "#{path}/university_decision", model_name: @model

【问题讨论】:

  • form_for model_name 应该是form_for <an instance/object of model>
  • 它是,这是一个部分,我在那里传递模型对象
  • 您是否在 routes.rb 中为此模型创建了命名空间路由
  • 你对参数有什么期望?如果它的event,它不会作为模型参数出现,因为你将它作为tag
  • 不,活动按我的意愿进行。我的问题是 {university_decision: "university_accepted" 应该在 model_name: 内,但不是

标签: ruby-on-rails ruby forms


【解决方案1】:

这实际上是一个非常简单的解决方案,我忘记了我可以定义嵌套名称。所以按钮看起来像这样以使我的参数看起来正确:

  = f.button "Accepted",
    value: "university_accepted",
    name: "model_name[university_decision]",
    class: "btn btn-default"

【讨论】:

    猜你喜欢
    • 2015-09-08
    • 2021-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多