Rails - 复制和自定义表单输入字段

时间:2015-05-04 18:21:42

标签: ruby-on-rails ruby-on-rails-4 simple-form

我正在使用simple_form创建表单,并且无法向Form#Create发送四个表单参数: 1)日期,2)存款,3)注释和4)returned_tag。

我仔细检查了强列表的列表以及表单视图。任何帮助将不胜感激。

更新

正如@micah所指出的那样,问题似乎是date输入字段的重复。我根据选择的成员类型(使用下面的jQuery代码插入自定义date字段)复制它们以获取每个(开始日期和结束日期)的自定义选项。在没有重现上述错误的情况下,正确的方法是什么?换句话说,我唯一的选择是在我的数据库中以不同的名称创建单独的属性?

表单控制器:

class FormsController < ApplicationController
  before_action :logged_in_user
  before_action :admin_user, only: :destroy

  def index
    @forms = Form.paginate(page: params[:page], :per_page => 20)
  end

  def show
    @form = Form.find(params[:id])
    respond_to do |format|
      format.html
      format.pdf do
        render :pdf => @form.last_name + '_' + @form.first_name + '_' + @form.created_at.strftime("%d %b %Y"),
        :template => 'forms/show.pdf.erb',
        :show_as_html => params[:debug].present?
      end
    end
  end

  def new
    @form = Form.new
  end

  def create

    @form = Form.new(form_params)

    signature = Paperclip.io_adapters.for(params[:form][:base64])
    signature.original_filename = "signature.png"
    @form.signature = signature

    if @form.save
      flash[:success] = "The form has been successfully created!"
      redirect_to @form
    else
      render 'new'
    end
  end

  def edit
    @form = Form.find(params[:id])
  end

  def update
    @form = Form.find(params[:id])
    if @form.update_attributes(form_params)
      flash[:success] = "Form has been updated!"
      redirect_to @form
    else
      render 'edit'
    end
  end

  def destroy
    Form.find(params[:id]).destroy
    flash[:success] = "Form deleted"
    redirect_to forms_path
  end

  private

  def form_params
    params.require(:form).permit(:first_name, :last_name, :email, :phone, :address, :member_type, :date, :base64, :children, :hour, :interaction, :new_tag, :returned_tag, :deposit, :notes)
  end
end

新表单视图:

<% provide(:title, 'Create form') %>
<h1>Create a new form</h1>
<div class="col-md-12">
    <%= simple_form_for(@form, html: { class: 'form-horizontal' }) do |f| %>

      <%= render 'shared/error_messages' %>

      <%= f.input :first_name, error: false %>
      <%= f.input :last_name, error: false %>
      <%= f.input :email, error: false %>
      <%= f.input :phone, error: false %>
      <%= f.input :address, error: false %>
      <%= f.input :member_type, collection: Form::MEMBERSHIPS, include_blank: true, error: false %>

      <div id="children">
        <%= f.input :children, collection: Form::CHILDREN, error: false, include_blank: true %>
      </div>

      <div id="date-select-kollel">
        <p>Please provide proof of Kollel Attendence</p>
        <%= f.input :date, start_year: Time.now.year - 5, end_year: Time.now.year, label: "Please enter date began Kollel", error: false, include_blank: true %>
      </div>

      <div id="date-select-youth">
        <p>For 16 years old and younger; please provide valid ID</p>
        <%= f.input :date, start_year: Time.now.year - 17, end_year: Time.now.year, label: "Please enter DOB", error: false, include_blank: true %>
      </div>

      <div id="hour">
        <%= f.input :hour, collection: Form::HOURS, error: false, include_blank: true %>
      </div>

      <%= render 'layouts/signature' %>
      </br></br>
      <%= f.hidden_field :base64 %>

      <p>FOR OFFICE USE ONLY:</p>

      <%= f.input :interaction, label: "Interaction type", collection: Form::INTERACTIONS, error: false %>

      <div id="new">
        <%= f.input :new_tag, error: false %>
        <%= f.input :deposit, label: "Cash deposit", error: false %>
        <%= f.input :notes, error: false %>
      </div>

      <div id="exchange">
        <%= f.input :new_tag, error: false %>
        <%= f.input :returned_tag, error: false %>
        <%= f.input :notes, error: false %>
      </div>

      <div id="refund">
        <%= f.input :returned_tag, error: false %>
        <%= f.input :deposit, label: "Cash refund", error: false %>
        <%= f.input :notes, error: false %>
      </div>

      <%= f.submit "Create form", class: "btn btn-primary", id: "save-form" %>
    <% end %>
</div>

表单模型

# == Schema Information
#
# Table name: forms
#
#  id                     :integer          not null, primary key
#  first_name             :string
#  last_name              :string
#  email                  :string
#  address                :string
#  created_at             :datetime         not null
#  updated_at             :datetime         not null
#  member_type            :string
#  phone                  :string
#  signature_file_name    :string
#  signature_content_type :string
#  signature_file_size    :integer
#  signature_updated_at   :datetime
#  base64                 :string
#  children               :string
#  date                   :date
#  hour                   :string
#  interaction            :string
#  new_tag                :string
#  returned_tag           :string
#  deposit                :string
#  notes                  :text
#

class Form < ActiveRecord::Base
  VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z\d\-]+)*\.[a-z]+\z/i
  VALID_ENGLISH_REGEX = /[a-zA-Z0-9]/
  MEMBERSHIPS = ["Monthly member: $55", "Pay-Per-Use (cash): 15% discount", "Kollel discount: $35", "Youth member: $35", "Monthly with children (Friday's): $55 + $16 per child", "Other" ]
  CHILDREN = ["1 - $16", "2 - $32", "3 - $48", "4 - $64"]
  HOURS = ["6:30-7:30", "7:00-8:00", "7:30-8:30", "8:00-9:00", "8:30-9:30", "9:00-10:00"]
  INTERACTIONS = ["NEW", "EXCHANGE", "REFUND"]

  #paperclip
  has_attached_file :signature, :styles => { medium: "250x500>", thumb: "125x250>" }, :default_url => "/images/:style/missing.png"
  validates_attachment_content_type :signature, :content_type => ["image/jpeg", "image/gif", "image/png"]

  validates :first_name, :last_name, presence: true, length: { maximum: 50 }, format: { with: VALID_ENGLISH_REGEX }
  before_save :upcase
  validates :email, length: { maximum: 255 }, format: { with: VALID_EMAIL_REGEX }, allow_blank: true
  validates :address, presence: true, length: { maximum: 500 }, format: { with: VALID_ENGLISH_REGEX }
  validates :member_type, :phone, :interaction, :base64, presence: true
  simple_phone_number :phone

  validates :date, :hour, presence: true

  default_scope -> { order(created_at: :desc) }

  def upcase
    self.first_name = first_name.upcase
    self.last_name = last_name.upcase
  end

end

jQuery代码:

  $('#date-select-kollel, #date-select-youth, #hour, #children').hide();

    $("#form_member_type").change(function() {
      var selected = $('#form_member_type').find(":selected").val();
      if (selected == "Kollel discount: $35") {
        $('#children, #date-select-youth').hide();
        $('#date-select-kollel, #hour').show();
      } else if (selected == "Youth member: $35") {
        $('#date-select-kollel, #children').hide();
        $('#date-select-youth, #hour').show();
      } else if (selected == "Monthly with children (Friday's): $55 + $16 per child") {
        $('#date-select-kollel, #date-select-youth, #hour').hide();
        $('#children').show();
      } else {
        $('#date-select-kollel, #date-select-youth, #hour, #children').hide();
      }
    });

    $('#new, #exchange, #refund').hide();

    $('#form_interaction').change(function() {
      var selected = $('#form_interaction').find(':selected').val();
      if (selected == "NEW") {
        $('#exchange, #refund').hide();
        $('#new').show();
      } else if (selected == "EXCHANGE") {
        $('#new, #refund').hide();
        $('#exchange').show();
      } else if (selected == "REFUND") {
        $('#new, #exchange').hide();
        $('#refund').show();
      }
    });

0 个答案:

没有答案