【问题标题】:Rails Simple form submit not sending value over to controllerRails 简单表单提交不向控制器发送值
【发布时间】:2017-07-23 18:47:01
【问题描述】:

我无法弄清楚为什么 simple_form 不发送文本框值。

这是我的控制器 定义创建 student_station_result = StudentStationResult.new

student = Student.find_by_register_no(params[:result_entry][:register_no])
student_sezzion_id = StudentSezzion.joins(:student_group).where(student_groups: { sezzion_id: params[:sezzion_id] }, student_id: student.id).pluck(:id).first

# TODO: Replaced after the NAPFA result calculation is done
student_station_result.station_point = 1
student_station_result.student_sezzion_id = student_sezzion_id
student_station_result.raw_score = params[:result_entry][:raw_score]
student_station_result.sezzion_station_id = params[:sezzion_station_id]
student_station_result.save!
end

这是我的html

= simple_form_for :result_entry, url: sezzion_sezzion_station_student_group_student_station_results_path do |f|
- @students.each do |student|
  - student_station_result = get_student_station_result(@station_id, student.id)
  .same-category
    .collapsible-container
        .collapsible-head
          .wrap
            .index
              h1 = student.register_no
            .name
              h3 = student.name
            .grade
              h1 = student_station_result[:performance_grade] if student_station_result.present?
              h5 = "x#{student_station_result[:raw_score]}" if student_station_result.present?
            .arrow
              #arrow-toggle =image_tag('arrow.svg', class: ['arrow-up', 'size'])

          .collapsible-body
            .grade-wrap
              .station-grade A: < 12.01s
              .station-grade B: 12.01 - 13.11
              .station-grade C: 13.11 - 14.11
              .station-grade D: 14.21 - 15.11
              .station-grade E: 15.31 - 16.51

            .score-input
              = f.input :raw_score, label: false, wrapper: false, input_html: { class: ['result-entry-textbox'], value: (student_station_result[:raw_score] if student_station_result.present?)}

              = f.input :register_no, as: :hidden, input_html: { value: student.register_no }
              = f.submit 'SAVE', class: ['result-entry-save', 'result-save-button']

我们将不胜感激。谢谢!

【问题讨论】:

  • 为什么你认为这些值没有通过?我们实际上可以从您的复制/粘贴中读取值。你有消息错误吗?
  • @Maxence 错误是这样的:ActiveRecord::NotNullViolation。 raw_score 的值为空。我在文本框中键入了值,但它没有在参数中传递。
  • 也许可以试试:student_station_result.raw_score = params[:result_entry]["raw_score"](字符串而不是符号)
  • @YoavEpstein 它仍然无法正常工作。如果我没记错的话,可能是 simple_form 问题
  • value: (student_station_result[:raw_score] if...,不应该是value: (student_station_result.raw_score if...吗?

标签: ruby-on-rails simple-form


【解决方案1】:

我找到了解决方案,实际上它很愚蠢。因为

= simple_form_for :result_entry, url: sezzion_sezzion_station_student_group_student_station_results_path do |f|

位于页面顶部。因此只让最后一个字段被提交。它应该放在 simple_form 块内。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-06
    • 1970-01-01
    • 2020-04-02
    相关资源
    最近更新 更多