为什么我的params缺少外部`service`对象?

时间:2017-04-18 10:11:14

标签: ruby-on-rails forms

发布此表单时,我收到了这些参数:

Processing by ServicesController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"LrWV0NDir", 
  "bike_part_id"=>["9", "10"], "description" => "fix me",
  "bike_id"=>"8"}

我应该如何制作表单以将服务对象放在最上面?

E.g:

  service: {
    bike_id: 1,
    bike_part_ids:  [1,2]
  }

由于服务缺失,我的service_parms将不允许这样做:

 def service_params
   params.require(:service).permit(:description, :log, :due_date, 
   :bike_id, :user_id, :completed, bike_part_id: [])
 end

表格

.form-horizontal
  = form_for [@bike, @service] do |f|
    - @part = @bike_part


    - if @part.present?
      h2 = "Ny service for #{@part.kind}"
      br
      br
      .well
        - if @part.service_due?
          h4 På tide med ny service
          p
            = "Denne delen bør ha service hver #{@part.service_interval} km"
            br
            = "Siste service på #{@part.kind} ble gjort da sykkelen hadde gått"
            em = " #{@part.service_done_at_bike_distance} km"
        /= collection_select(:service, :bike_part_id, @bike.bike_parts.all,
          :id, :kind, prompt: true, selected: @part.id )


    .form-group
      = f.label :description
      = f.text_area :description, class: 'form-control', rows: 6,
        placeholder: "- What does the customer want us to help with? \n" \
        "- Have we agreed upon a price? \n" \
        "- Anything we should check up and call the customer about?"

    - unless @service.new_record?
      .form-group
        = f.label :log
        = f.text_area :log, class: 'form-control', rows: 6,
          placeholder: "Describe what's done.."

    .form-group
      = f.label :due_date
      br
      = f.datetime_select :due_date, start_year: Time.now.year,
        end_year: Time.now.year+1, order: [:day, :month, :year]

    - unless @service.new_record?
      .form-group
        .checkbox
          label
            = check_box_tag 'service[completed]', true, @service.completed
            | Ferdig



    .form-group
        = f.submit :class => 'btn btn-primary', value: 'Save'
        = link_to 'tilbake', :back, class: 'btn'

路由

 bike_services GET    /bikes/:bike_id/services(.:format)      services#index
               POST   /bikes/:bike_id/services(.:format)      services#create

从rails视图生成的HTML

<form action="/bikes/8/services" accept-charset="UTF-8" method="post"><input name="utf8" type="hidden" value="✓"><input type="hidden" name="authenticity_token" value="qNNd/A8lpCP1p4c7Bp5K1YZB/n+SfHztcOadQuFE/vfYwTCUx4TwESdSGWINK3Jiq1O6BmtLrWV0NDir+4a7cg==">
              <table>
                <tbody><tr>
                  <th>
                    Del
                  </th>
                  <th>
                    Service intervall
                  </th>
                  <th>
                    Antall km til neste service
                  </th>
                </tr>
                <tr>
                  <td>
                    cassette
                  </td>
                  <td align="right">
                    3000.0
                  </td>
                  <td align="right">
                    In 3
                  </td>
                  <td>
                    <input type="checkbox" name="bike_part_id[]" id="bike_part_id_" value="9">
                  </td>
                </tr>
                <tr>
                  <td>
                    chain
                  </td>
                  <td align="right">
                    3000.0
                  </td>
                  <td align="right">
                    In 3
                  </td>
                  <td>
                    <input type="checkbox" name="bike_part_id[]" id="bike_part_id_" value="10">
                  </td>
                </tr>
                <tr>
                  <td>
                    front break
                  </td>
                  <td align="right">
                    3000.0
                  </td>
                  <td align="right">
                    Overdue by -187,9
                  </td>
                  <td>
                    <input type="checkbox" name="bike_part_id[]" id="bike_part_id_" value="11">
                  </td>
                </tr>
                <tr>
                  <td>
                    front derailleur
                  </td>
                  <td align="right">
                    1000.0
                  </td>
                  <td align="right">
                    Overdue by -189,9
                  </td>
                  <td>
                    <input type="checkbox" name="bike_part_id[]" id="bike_part_id_" value="12">
                  </td>
                </tr>
              </tbody></table>
              <input type="submit" name="commit" value="registrer en service"></form>

1 个答案:

答案 0 :(得分:0)

您需要在您创建的表单中嵌套.form-group div。 Slim以缩进方式进行,您基本上在创建它的同一行上结束表单,因为.form-group div与指定form_for的级别相同。将表单中的所有代码缩进一次,如:

.form-horizontal
  = form_for [@bike, @service] do |f|
    .form-group
      = f.label :description
      = f.text_area :description, class: 'form-control', rows: 6,
    .form-group
      = f.submit :class => 'btn btn-primary', value: 'Save'

更新:看起来您的问题已被修改并且已添加缩进...我会将此保留在此处,以防您实际粘贴了您的超薄代码,因为在这种情况下您需要将其显示为上面编辑的帖子或我的回答。