使用第二张表

时间:2016-12-06 12:13:25

标签: mysql ruby-on-rails wicked-gem

好的,还有一个,希望我能完成。

我正在使用Wicked进行多页表单,其中我也想使用mysql数据库中的多个表。 所以我创建了第二个表和表单,但是,表单不会显示第二个表的输入字段,也不显示下一个和上一个按钮。

用于创建表内容的表单页面的代码:(这不会像在另一页中那样具有下拉列表,使用相同的表(通过连接测量另一个使用的度量))

<%= 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>Maatregel aanmaken </legend>
       <%= f.fields_for :measures do |measure| %>
          <%#measure.fields_for :measure do |measure| %>
    <div>
      <%= f.label :measurement  %>
      <%= measure.text_field :measurement %>
    </div>

    <div>
      <%= f.label :type%>
      <%= measure.text_field :type %>
    </div>

    <div>
      <%= f.label :valid_from%>
      <%= measure.date_select :valid_from %>
    </div>

    <div>
      <%= f.label :valid_to%>
      <%= measure.date_select :valid_to %>
    </div>

    <div>
      <%= f.submit 'Next Step' %>
          <%# knop naar vorige pagina %>
      <%= button_to "vorige", enquirys_path %>

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

    </div>

  </fieldset>
<% end %>

steps_controller:

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

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

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

  private

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

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

end

数据库模型:

Enquiry_measure:

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 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 => true

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

# 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) }

  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

更新

在我忙于添加一些额外的(稍后)需要的表时,在代码中乱七八糟之后,错误消失了。 但是,它没有显示任何指定的字段或标签,它只是一个显示Maatregelen的空白页面,而不是其他内容。

Page nr 3(前2页工作,他们使用1个表,第3页和第4页使用另一个表,稍后我将添加更多页面,甚至使用其他表)它都使用相同的查询和步骤控制器。

<%= 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>Maatregelen</legend>
       <%= f.fields_for :enquiry_measures do |enquiry_measures| %>
                                <%= enquiry_measures.fields_for :measure do |measures| %>

    <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 -%>
      <%= collection_select :measurement, :enquiry_measures, measures.select(:measurement).uniq.order('measurement ASC'), :measurement, :measurement, {:prompt => 'kies een maatregel'},{:name => 'select_measurement'} %>
          </div>
     <div>
      <%= f.label :Gereed %>
      <br />
      <%= enquiry_measures.text_field :done %>
    </div>

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

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

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

          <% end %> <%# end of enquiry_measures.fields_for :measure %>
    <% end %> <%# end of f.fields_for :enquiry_measures %>
  </fieldset>
<% end %>

0 个答案:

没有答案