rails`button_to`嵌套参数格式不正确

时间:2016-10-05 20:19:57

标签: ruby-on-rails params button-to

我有一个控制器动作

  def create
    @subject_decision = SubjectDecision.new(subject_decision_params)

    respond_to do |format|
      if @subject_decision.save
        format.html { redirect_to @subject_decision, notice: 'Subject decision was successfully created.' }
        format.json { render :show, status: :created, location: @subject_decision }
      else
        format.html { render :new }
        format.json { render json: @subject_decision.errors, status: :unprocessable_entity }
      end
    end
  end

使用私有方法:

def subject_decision_params
  params.require(:subject_decision).permit(:decision_block_id, :choice_value, :timeline_id, :practice)
end

和视图中的button_to

<%= button_to "Choice A", timeline_subject_decisions_path(timeline_id: @timeline.id), method: :post, params:
    { subject_decision:
        {
            decision_block_id: @db.id,
            choice_value: 'a',
            timeline_id: @timeline.id,
            practice: true,
        }
    } 
%>

但是当我点击按钮时,我得到以下导轨错误:

undefined method `permit' for "choice_value=a&decision_block_id=&practice=true&timeline_id=1":String

为什么我不能像这样提交表单(button_to)的任何线索?

1 个答案:

答案 0 :(得分:1)

您可以将button_to格式化为

<%= button_to "Choice A", timeline_subject_decisions_path(timeline_id: @timeline.id), method: :post, params:
    {   
        'subject_decision[decision_block_id]' => @db.id,
        'subject_decision[choice_value]' => 'a',
        'subject_decision[timeline_id]' => @timeline.id,
        'subject_decision[practice]' => true,
    }
%>