我有一个带有关联的复杂表单,我希望实时查看内联验证错误。我偶然发现 RailsCasts 剧集客户端验证,并决定采用这种方法并安装了gem。
gem 'client_side_validations',
github: "DavyJonesLocker/client_side_validations",
branch: "4-2-stable"
根据gem的要求,我将validate: true
放在表单助手的顶部,看到它只对属性字段起作用,而不是对关联对象起作用。
例如,当字段name
为空时,它会显示内联错误{1}}。当用户未选择can't be blank
时,我希望它通知用户program
但不会。目前,它只是抛出我在Need a program!
控制器中声明的flash[:error]
消息,而没有特定于"There was a problem launching your Campaign."
选择框的内联消息。
这是设置..
视图/活动/ _forms.html.erb
program
视图/活动/ _target.html.erb
<%= form_for @campaign, url: {action: "create"}, validate: true do |f| %>
<%= f.label :campaign_name, "Campaign Name", class: "right-label" %>
<%= f.text_field :name %>
<%= f.label :comments %>
<%= f.text_area :comment %>
<%= f.label :program %>
<%= f.select :program_id,
Program.all.collect { |p| [ p.name, p.id ] }, { include_blank: true }, validate: { presence: true } %>
<%= f.label :data_file, "Data File (.zip)", class: "right-label" %>
<%= select_tag :uploadzip_id,
options_from_collection_for_select(
Uploadzip.all, :id, :file_name
), { include_blank: "Include a Zip File" } %>
<%= f.label :additional_files %>
<ul><%= f.collection_check_boxes :uploadpdf_ids, Uploadpdf.last(5).reverse, :id,
:file_name, { mulitple: true } do |b| %>
<li><%= b.label do %>
<%= b.check_box + truncate(File.basename(b.text,'.*'), length: 45) %>
<% end %>
</ul><% end %>
<%= render 'target', :f => f %>
<%= f.submit "Confirm & Execute", id: "campaign-create-button" %>
<% end %>
应用/模型/ campaign.rb
<%= f.label :plan, "Pick a Plan", class: "right-label" %>
<%= f.select :plan_id,
Plan.all.collect { |p|
[ p.name, p.id ]
}, { include_blank: true } %>
<%= f.label :channels, "Distribution Channel", class: "right-label" %>
<ul class="channels-list">
<% ["Folder", "Fax", "Email"].each do |channel| %>
<li><%= check_box_tag "campaign[channels][]", channel,
@campaign.channels.include?(channel),
id: "campaign_channels_#{channel}" %> <%= channel %></li>
<% end %>
</ul>
应用/控制器/ campaigns_controller.rb
class Campaign < ActiveRecord::Base
belongs_to :program
belongs_to :plan
has_many :uploadables, dependent: :destroy
has_many :uploadpdfs, through: :uploadables
has_one :uploadzip, dependent: :nullify
validates :name, presence: true,
uniqueness: true
validates :program, presence: { message: "Need a program!"}
validates :uploadzip, presence: true
validates :uploadpdf_ids, presence: true
serialize :channels, Array
end
我还关注了this教程,该教程提供的代码可以帮助显示视图中关联的验证错误。
配置/初始化/ errors_for_associations.rb
class CampaignsController < ApplicationController
def index
@campaigns = Campaign.all.order("created_at DESC")
end
def new
@campaign = Campaign.new
end
def create
@campaign = Campaign.new(campaign_params)
if @campaign.save
zip = Uploadzip.find(params[:uploadzip_id])
zip.campaign = @campaign
zip.save
flash[:success] = "Campaign Successfully Launched!"
redirect_to @campaign
else
flash[:error] = "There was a problem launching your Campaign."
redirect_to new_campaign_path
end
end
def show
@campaign = Campaign.includes(:program, :uploadzip, :plan, :uploadpdfs).find(params[:id])
end
private
def campaign_params
params.require(:campaign).permit(:name, :comment, :plan_id, :program_id, :campaign_id, uploadpdf_ids: [], channels: [])
end
end