保存表单时的未定义方法

时间:2016-12-13 12:55:28

标签: ruby-on-rails wicked-gem

我正在使用Wicked创建一个多页面webform。但是,我需要在该表单中使用多个表。我成功使用了一个,并得到第二个表的页面很好地显示。但是,当我尝试保存它时,它会出现此错误:

NoMethodError in Enquirys::StepsController#update
undefined method `needed' for #<Enquiry:0x007fa4530e4cb0>

我现在已经搞砸了一段时间,但没有成功。

因为我有很多代码而且不知道在哪里查看,我在这个要点中添加了重要内容:https://gist.github.com/GroeGIT/f22bd079814df2c0acc8430c4db520a7

任何人都可以帮我解决这个问题,还是Wicked不支持使用不同表格的多页表单?

由于

模型

    class Enquiry < ActiveRecord::Base
  #ophalen van andere tabellen voor het formulier. Has_many is 1 op veel relatie
  #accepts_nested_attributes Nested attributes allow you to save attributes on associated records through the paren
  # de dere regel zorgt ervoor dat de maatregelen worden opgehaald via de tussentabel enquiry_measures.

  has_many :enquiry_measures, :class_name => 'EnquiryMeasure' #, inverse_of: :Enquiry
  accepts_nested_attributes_for :enquiry_measures, :allow_destroy => false

  has_many :measures, -> { uniq }, :class_name => 'Measure', :through => :enquiry_measures, dependent: :destroy

  has_many :controls, :class_name => 'Control' #, inverse_of: :Enquiry

  has_many :applicants, :class_name => 'Applicant' #, inverse_of: :Enquiry

  has_many :agrees, :class_name => 'Agree' #, inverse_of: :Enquiry

  has_many :signatures, :class_name => 'Signature' #, inverse_of: :Enquiry
  accepts_nested_attributes_for :signatures, :allow_destroy => false

  has_many :tools, :class_name => 'Tool' #, inverse_of: :Enquiry


# 28-11 MG de pagina's die in het form worden gebruikt.
  cattr_accessor :form_steps do
    %w(basic when measurements)
  end

  attr_accessor :form_step

  validates :Reference, :Location, presence: true, if: -> { required_for_step?(:basic) }
  validates :Amount, :Date, presence: true, if: -> { required_for_step?(:when) }
  validates :needed, presence: true, if: -> { required_for_step?(:measurements) }
#validates :needed, :measurement, presence: true, if: -> { required_for_step?(:createmeasures) }


  def required_for_step?(step)
    return true if form_step.nil?
    return true if self.form_steps.index(step.to_s) <= self.form_steps.index(form_step)
  end


end


class EnquiryMeasure < ActiveRecord::Base
    belongs_to :enquiry, :class_name => 'Enquiry' #, inverse_of: :enquiry_measures
    validates_presence_of :enquiry
    has_many :measure, :class_name => 'Measure'
end

控制器:  询问控制员:

    class EnquirysController < ApplicationController
  before_action :set_enquiry, only: [:show, :edit, :update, :destroy]
  before_action :set_measurement, only: [:show, :edit, :update]


  def index
    # Normally you'd have more complex requirements about
    # when not to show rows, but we don't show any records that don't have a name
    @enquirys = Enquiry.where.not(reference: nil)

    #voor het toevoegen van maatregelen. test!
    @measurements = Measure.where.not(measurement: nil)
      #@enquirymeasure = EnquiryMeasure.where.not(enquiry_measure_id: nil)

  end

  def new
    @enquiry = Enquiry.new
    #voor het toevoegen van maatregelen. test!
    @enquiry_measure = EnquiryMeasure.new
    @measurement = Measure.new
  end

  def create
    @enquiry = Enquiry.new
    #@enquiry_measure = EnquiryMeasure.new
    @enquiry.enquiry_measures.build#(:enquiry_id => :id)
    @enquiry.save(validate: false)
    #@enquiry_measure.save(validate: false)
    redirect_to enquiry_step_path(@enquiry, Enquiry.form_steps.first)
    @measurement = Measure.new
    @measurement.save(validate: false)
  end

  def show
    @enquiry = Enquiry.find(params[:enquiry_id])
    @measurement = Measure.find(params[:id])
  end

  def destroy
    @enquiry.destroy
    respond_to do |format|
      format.html { redirect_to enquirys_url }
      format.json { head :no_content }
    end
  end

  private
  # Use callbacks to share common setup or constraints between actions.
  def set_enquiry
    @enquiry = Enquiry.find(params[:id])
  end

  def set_measurement
    @measurement = Measure.find(params[:Measure.id])
  end

  # Never trust parameters from the scary internet, only allow the white list through.
  #Nodig voor het opslaan en tonen van items! alle weer te geven dingen dienen in de params te staan.
  def enquiry_params
  params.require(:enquiry).permit(:Reference, :Location, :Date, :Time, :Amount, measure_attributes: [:measurement, :type, :valid_from, :valid_to] )
  #nquiry_measures_attributes: [ :done, :responsible, :needed]
  end

end

Step_controller:

    class Enquirys::StepsController < ApplicationController
  include Wicked::Wizard
  steps *Enquiry.form_steps


  def show
    @enquiry = Enquiry.find(params[:enquiry_id])
    #@enquiry_measures = EnquiryMeasure.find(params[:enquiry_measure_id])
    render_wizard
  end

  def update
    @enquiry = Enquiry.find(params[:enquiry_id])
    @enquiry.update(enquiry_params(step))

    #werkt niet, could not find EnquiryMeasure with 'id'=
    #@enquiry_measure = EnquiryMeasure.find(params[:enquiry_measure_id])
   #@enquiry_measure.update(enquiry_measure_params(step))

    render_wizard @enquiry
  end

  private

  def enquiry_params(step)
    permitted_attributes = case step
                             when "basic"
                               [:Reference, :Description, :Location]
                             when "when"
                               [:Amount, :Date]
                             when "measurements"
                               [:responsible, :needed, :done]
                           #  when "createmeasures"
                            #   [:measurement]
                           end

    params.require(:enquiry).permit(permitted_attributes).merge(form_step: step)
    #params.require(:enquiry_measures).permit(permitted_attributes).merge(form_step: step)
  end

end

有问题的观点:

    <%= form_for @enquiry, method: :put, url: wizard_path do |f| %>
    <% if f.object.errors.any? %>
        <div class="error_messages">
          <% f.object.errors.full_messages.each do |error| %>
              <p><%= error %></p>
          <% end %>
        </div>
    <% end %>

    <fieldset>
      <legend>Informatie</legend>

      <div>
        <%= f.label :reference %>
        <%= f.text_field :Reference, disabled: true %>
      </div>

    </fieldset>

    <%# Code voor de Measurements. %>

    <fieldset>
    <legend>Maatregelen</legend>
    <%= f.fields_for :enquiry_measures do |enquiry_measures| %>
        <%# enquiry_measures.fields_for :measure do |measures| %>
        <% if false %>
            <div>
              <%= f.label :Maatregel %>
              <br/>
              <%= collection_select(:measure, :enquiry_id, Enquiry.all, :id, :measurement) %>
              <%# http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-collection_select -%>
             <%# uitgebreider, bovenstaande zou goed moeten zijn(8-12 MG) collection_select :measurement, :enquiry_measures, measurement.select(:measurement).uniq.order('measurement ASC'), :measurement, :measurement, {:prompt => 'kies een maatregel'}, {:name => 'select_measurement'} %>
            </div>
        <% end %>

        <div>
          <%# test met enquiry_measures ipv f.label 7-12 MG%>
          <%= f.label :Gereed %>
          <br/>
          <%= enquiry_measures.check_box :done %>
        </div>

        <div>
          <%= f.label :Verantwoordelijke %>
          <br/>
          <%= enquiry_measures.text_field :responsible %>
        </div>

        <div>
          <%= f.label :Benodigd %>
          <br/>
          <%= enquiry_measures.check_box :needed %>
        </div>

        <div>
          <%= f.submit 'Next Step' %>

          <%# 24-11 MG knop die je terug stuurt naar de homepage %>
          <%= button_tag "Annuleren", :type => 'button', :class => "subBtn", :onclick => "location.href = '#{root_path()}'" %>

        </div>

        <%# end of enquiry_measures.fields_for :measure, END Tag nog plaatsen! %>
    <% end %> <%# end of f.fields_for :enquiry_measures %>
    </fieldset>
<% end %>

0 个答案:

没有答案