ruby表单编辑不保存嵌套表单

时间:2017-01-19 08:24:19

标签: mysql ruby-on-rails

我有一个嵌套的表单,用于在数据库中创建新记录。但是,编辑功能在编辑后不会更新这些记录。我似乎已经正确设置了所有内容,因为在将它与我在互联网或脚手架代码上找到的代码进行比较时,我没有发现任何异常。我做错了什么?

这是enquiries_controller的一部分:

  def edit
    #nog op kunnen slaan!!!! 1-01-17 Marco
    @enquiry = Enquiry.find(params[:id])
    @enquiry.enquirymeasures.build

    @enquiry.tools.build
    @enquiry.build_applicant
    @enquiry.signatures.build
   @enquiry.gasmeters.build

  end

  # POST /enquiries
  # POST /enquiries.json
  def create
    @enquiry.user_id = current_user.id
    @enquiry = Enquiry.new(enquiry_params)
    #@enquiry.enquirymeasures.build

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

  # PATCH/PUT /enquiries/1
  # PATCH/PUT /enquiries/1.json
  def update

     respond_to do |format|
      if @enquiry.update(enquiry_params)
        format.html { redirect_to @enquiry, notice: 'Enquiry was successfully updated.' }
        format.json { render :show, status: :ok, location: @enquiry }
      else
        format.html { render :edit }
        format.json { render json: @enquiry.errors, status: :unprocessable_entity }
      end
    end
  end

询问模式

class Enquiry < ActiveRecord::Base
  has_many :enquirymeasures, dependent: :destroy
  accepts_nested_attributes_for :enquirymeasures, :reject_if => lambda { |a| a[:responsible].blank? }, :allow_destroy => true


  has_many :tools, dependent: :destroy
  accepts_nested_attributes_for :tools

  has_many :controls, dependent: :destroy
  accepts_nested_attributes_for :controls

  has_one :applicant, dependent: :destroy
  accepts_nested_attributes_for :applicant

  has_one :contractor, through: :applicant

  has_many :signatures, dependent: :destroy
  accepts_nested_attributes_for :signatures
  has_many :representatives , through: :signatures, :source => :representatives

  has_many :gasmeters, dependent: :destroy
  accepts_nested_attributes_for :gasmeters

  belongs_to :user
end

部分_form部分(总共超过200行):

<%= form_for(@enquiry) do |f| %>
    <form role="form">
      <% if @enquiry.errors.any? %>
          <div id="error_explanation">
            <h2><%= pluralize(@enquiry.errors.count, "error") %> prohibited this enquiry from being saved:</h2>

            <ul>
              <% @enquiry.errors.full_messages.each do |message| %>
                  <li><%= message %></li>
              <% end %>
            </ul>
          </div>
      <% end %>
      <h1 align="center">Vul de onderstaande velden in om een werkvergunning aan te vragen.</h1>
      <p align="center">Algemene informatie</p>

..........

      <p align="center">De laatste stap bestaat uit het ondertekenen van het formulier met uw handtekening.</p>
      <div class="form-group">
        <%= f.fields_for :signatures do |s| %>
            <%= f.fields_for :representatives do |sr| %>
                <%= s.label :Gemachtigde %><br>
                <%= s.collection_select(:representative_id, Representative.all, :id, :name, prompt: true) %>
                <br>


                <%= s.label :datum %><br>
                <%= s.datetime_select :date %>

                <br>
                <%= s.label :Handtekening %><br>
                <%= s.text_field :signature %>

                <%# 17-1-2017 baseren van 1 dropdown op de andere %>


            <% end %>
        <% end %>
      </div>

      <div class="form-group">
<p> Goedkeuring van het formulier door JPB Groep</p>
      <% if can? :manage, Enquiry %>
          <%= f.label :Goedgekeurd %>
          <%= f.check_box :approved %>
          <%end%>
      </div>
      <div class="actions">
        <%= f.submit %>
      </div>
    </form>
<% end %>

经过一番摆弄后,我确实设法在编辑记录时可以看到记录中输入的数据,但保存不起作用。我可以看到控制台使用push,但它没有触发更新查询。 我没有收到任何错误,所以当点击提交时,页面会返回到索引页面。

添加路线

Rails.application.routes.draw do

  devise_for :users
  scope "/Admin" do
    resources :users, :enquiries, :roles
  end

  scope "/Aanvrager" do
    resources :users, :enquiries
  end

  mount RailsAdmin::Engine => '/admin', :as => 'rails_admin'


root 'enquiries#index'
  get 'welcome/Index'

enquiry_params

def enquiry_params
      params.require(:enquiry).permit(:id, :reference, :location, :description, :date, :amount, :approved, enquirymeasures_attributes: [:id, :responsible, :done, :needed, :measurement_id, :user],
                                      tools_attributes: [:id, :handtool, :other, :motorvehicle, :compressor, :ramp, :scaffold, :crane, :ladder, :generator, :tankladder],
                                        applicant_attributes: [:id, :name, :email, :contractor_id],
                                      signatures_attributes: [:id, :date, :signature, :representative_id],
                                      gasmeters_attributes: [:id, :date, :tester, :signature, :oxigen, :o_needed, :o_continu, :explosives, :e_needed, :e_continu, :mat1, :mat1_needed, :mat1_continu, :mat2, :mat2_needed, :mat2_continu, :mat3, :mat3_needed, :mat3_continu]).merge(user_id: current_user.id)

    end

0 个答案:

没有答案