Why do I get the error `undefined method 'map'`? - ruby-on-rails

In my Ruby on Rails application I am trying to display a three drop down menus in the _form.html.erb which are rendered from the file _booking_lookup.html.erb and get there data from the drop down menu methods in the models.
_form.html.erb:
<%= render(:partial => '/booking_lookup', :locals=> {:film => #film = Film.all, :showings => #showings = Showing.all, :seats => #seats = Seat.all, :my_path => '/films/booking_lookup' }) %>
_booking_lookup.html.erb:
<%= form_tag my_path, :method=>'post', :multipart => true do %>
<%= select_tag ('title_id'),
options_from_collection_for_select(#films, :id, :title_info, 0 ),
:prompt => "Film" %>
<%= select_tag ('showings_id'),
options_from_collection_for_select(#showings, :id, :showing_times, 0 ),
:prompt => "Showings" %>
<%= select_tag ('seat_id'),
options_from_collection_for_select(#seats, :id, :seats_available, 0 ),
:prompt => "Seats" %>
<%= submit_tag 'Search' %>
film.rb:
class Film < ActiveRecord::Base
has_many :showings
belongs_to :certificate
belongs_to :category
def title_info
"#{title}"
end
end
seat.rb:
class Seat < ActiveRecord::Base
belongs_to :screen
has_many :bookings
def seats_available
"#{row_letter}#{row_number}"
end
end
showing.rb:
class Showing < ActiveRecord::Base
belongs_to :film
has_many :bookings
belongs_to :screen
def showing_times
"#{show_date.strftime("%e %b %Y")} # #{show_time.strftime("%H:%M")}"
end
end
But for some reason with the line: <%= select_tag ('title_id'),
options_from_collection_for_select(#films, :id, :title_info, 0 ),
:prompt => "Film" %> I get the error:
NoMethodError in Bookings#new
undefined method `map' for nil:NilClass
The weird part is that I am using a lot of this code else where, I have a _multi_search.html.erb form:
<%= form_tag my_path, :method=>'post', :multipart => true do %>
<!-- Genre: -->
Search By:
<%= select_tag ('cat_id'),
options_from_collection_for_select(#categories, :id, :category_info, 0 ),
:prompt => "Genre" %>
<%= select_tag ('cert_id'),
options_from_collection_for_select(#certificates, :id, :certificate_info, 0 ),
:prompt => "Age Rating" %>
<%= text_field_tag :search_string, nil, placeholder: "ACTOR" %>
or
<%= select_tag ('title_id'),
options_from_collection_for_select(#films, :id, :title_info, 0 ),
:prompt => "Film" %>
<%= submit_tag 'Search' %>
<% end %>
And is used in the application.html.erb:
<%= render(:partial => '/multi_search', :locals=> {:categories => #categories = existing_genres, :certificates => #certificates = Certificate.all, :films => #films = Film.all, :my_path => '/films/multi_find' }) %>
And that works fine.
What am I doing wrong?

It looks like #films is nil. Try setting #films = Film.all (instead of #film = Film.all) in _form.html.erb.
Update:
I would recommend moving the queries to the controller action. In the Model-View-Controller pattern, Controllers should be asking Models for data, not Views.
# BookingLookupController
def new
#films = Film.all
#showings = Showing.all
#seats = Seat.all
end
You can then reference the instance variables in the view.
<%= render partial: '/booking_lookup', locals: {films: #films, showings: #showings, seats: #seats, my_path: '/films/booking_lookup' } %>

In Controller, select fields as you just want to display names in dropdown
def method_name
#films = Film.select([:id, :title_info])
#showings = Showing.select([:id, :showing_times])
#seats = Seat.select([:id, :seats_available])
end
In page
<%= render(:partial => '/booking_lookup', :locals=> {:films => #films, :showings => #showings, :seats => #seats, :my_path => '/films/booking_lookup' }) %>
In partial
options_from_collection_for_select(films, :id, :title_info, 0 ),:prompt => "Film" %>

Related

Ruby: create a parent only if at least 1 child exists

I have a parent - child relationship between Repairs & RepairItems.
A repair must have a least 1 RepairItem to be saved. I've made a nested (simple) form to create a Repair and display 3 blank repair_items.
I'm trying to work out how to make sure that a a Repair has at least 1 repair_item entered to allow the user to save the Repair. Otherwise I need to prompt the user that the repair can't be saved until at least 1 repair_item is entered..
Can anyone point me in the right direction for validation so that a user can't save a Repair without any Repair items entered? Thanks
class Repair < ActiveRecord::Base
attr_accessible :repair_id, :repairer_id, :fault_num, :vehicle_id, :date_reported, :date_closed, :hours_open, :mileage_open, :reported_to, :reported_by,
:repair_items_attributes
belongs_to :vehicle
belongs_to :repairer
has_many :repair_items, :dependent => :destroy
validates_presence_of :vehicle_id
validates_associated :repair_items
accepts_nested_attributes_for :repair_items, :reject_if => lambda { |a| a[:repair_type_id].blank? }, :allow_destroy => true
end
class RepairItem < ActiveRecord::Base
attr_accessible :repair_id, :problem, :solution, :repair_type_id, :priority, :repairer_id, :invoice, :cost, :tax,
:item_state_id, :mileage_closed, :hours_closed, :date_closed
belongs_to :repair
belongs_to :repairer
belongs_to :repair_type
belongs_to :item_state
#validates_presence_of :repair_id
validates_presence_of :repair_type_id
scope :open, where(:item_state_id => 1)
scope :monitor, where(:item_state_id=> 2)
scope :deferred, where(:item_state_id => 3)
scope :closed, where(:item_state_id => 4)
scope :cancelled, where(:item_state_id => 5)
end
class RepairsController < ApplicationController
before_filter :authorise
layout :resolve_layout
def index
#status = 1
#repairItems = RepairItem.open
end
def monitor
#status = 2
#repairItems = RepairItem.monitor
end
def deferred
#status = 3
#repairItems = RepairItem.deferred
end
def closed
#status = 4
#repairItems = RepairItem.closed
end
def cancelled
#status = 5
#repairItems = RepairItem.cancelled
end
def new
#repair = Repair.new
3.times { #repair.repair_items.build }
end
def create
# Instantiate a new object using form parameters
#repair = Repair.new(params[:repair])
# Save the object
if #repair.save
# If the save suceeds, redirect to the list action
redirect_to(repairs_path, :notice => 'Repair Created.')
else
# If the save fails, redisplay the form so user can fix problems
render :action => :new
end
end
<%= simple_form_for( #repair, :defaults => { :disabled => #current_user.read_only, :input_html => { :class => "span10" } }) do |f| %>
<fieldset>
<!-- This will display some text in red at the top of the form telling the user -->
<%= f.error_notification %>
<div class="st-row-fluid">
<div class="span2">
<%= f.association :vehicle, label_method: :fleet_num, value_method: :id, include_blank: true, label: 'Vehicle'%>
<p>Current Kms</p>
<p>Current Hours</p>
<p class="muted">Warranty Expires</p>
<p class="muted">Contract Maintenance</p>
</div>
<div class="span2">
<%= f.input :date_reported, :as => :date_picker, :input_html => { :class => "span10 st-datepicker"} %>
<%= f.input :mileage_open, :label => "Km/Miles" %>
<%= f.input :hours_open %>
</div>
<div class="span2">
<%= f.input :fault_num %>
<%= f.input :reported_to %>
<%= f.input :reported_by %>
</div>
</div>
<div class="row-fluid">
<h4> Items</h4>
<%= f.simple_fields_for :repair_items do |p| %>
<table class="table table-condensed">
<tr>
<%= render "repair_items", :p => p %>
</tr>
</table>
<% end %>
</div>
<%= f.error :base %>
<div class="st-form-actions">
<% if #current_user.read_only == false %>
<%= f.submit nil, :class => 'btn btn-success pull-right' %>
<% end %>
<%= link_to 'Cancel', repairs_path, :class => 'btn btn-danger pull-right' %>
</div>
</fieldset>
<% end %>
partial
<td><%= p.association :repair_type, label_method: :repair_type_label, value_method: :id, include_blank: true, label: 'Repair Type'%></td>
<td><%= p.input :problem %></td>
<td><%= p.input :solution %></td>
<td><%= p.input :priority %></td>
<td><%= p.association :repairer, label_method: :rep_name, value_method: :id, include_blank: true, label: 'Repairer'%></td>
</tr>
<tr>
<td><%= p.input :invoice %></td>
<td><%= p.input :cost %></td>
<td><%= p.input :tax %></td>
<td><%= p.input :date_closed, :as => :date_picker, :input_html => { :class => "span10 st-datepicker"} %></td>
<td><%= p.input :mileage_closed, :label => "Km/Miles" %></td>
<td><%= p.input :hours_closed %></td>
<td><%= p.association :item_state, label_method: :state_label, value_method: :id, label: 'Status', :default => 1 %></td>
Use validates_associated :repair_items in the repair to validate the repair items. The action has to construct the repair items using repair.RepairItem.build, so that the items will be associated with the repair, before attempting to save the repair. The repair is constructed before the repair items, but the repair_items will block the associated repair from saving.

Rails 3 - undefined method `map' for nil:NilClass for my own validation

I met very strange problem. I have Timetable model and try to write my custom validation. So, now i'm just trying to add test error for field to make sure that everything is ok. But it doesn't work. So, i try to update object of Timetable model but when i don't use my test custom validation everything works perfect. Otherwise i get such error:
NoMethodError in Timetables#update
undefined method `map' for nil:NilClass
32:
33: <div class="controls">
34: <%= f.select( :curriculum_id,
35: options_for_select( #subjects_with_curriculums,
36: #tt.curriculum_id ),
37: { :include_blank => true }) %>
38: </div>
Here is my model:
# == Schema Information
#
# Table name: timetables
#
# id :integer not null, primary key
# curriculum_id :integer
# school_class_id :integer
# tt_day_of_week :string(255)
# tt_number_of_lesson :integer
# tt_room :string(255)
# tt_type :string(255)
# created_at :datetime not null
# updated_at :datetime not null
#
class Timetable < ActiveRecord::Base
belongs_to :curriculum
belongs_to :school_class
has_many :lessons
validates :school_class_id, :presence => { :message => "should exist" }
validates :tt_day_of_week,
:presence => true,
:inclusion => { :in => %w(Mon Tue Wed Thu Fri) }
validates :tt_number_of_lesson,
:presence => true,
:inclusion => {
:in => 1..9,
:message => "should have 1..9 symbols"
}
validates :tt_room,
:length => {
:maximum => 3,
:message => "should have 3 symbols"
},
:allow_blank => true
validates :tt_type,
:inclusion => { :in => ["Primary lesson", "Extra"] },
:allow_blank => true
validate :test
def test
errors.add(:tt_number_of_lesson, "test")
end
end
My controller:
# encoding: UTF-8
class TimetablesController < ApplicationController
...
def edit
#types_of_lesson = collect_types_of_lesson
#tt = Timetable.find( params[:id] )
#subjects_with_curriculums = collect_subjects_with_curriculums( #tt.school_class )
end
def update
#tt = Timetable.find( params[:id] )
if #tt.update_attributes( params[:timetable] )
flash[:success] = "Расписание успешно обновлено!"
redirect_to timetables_path
else
flash.now[:error] = #tt.errors.full_messages.to_sentence :last_word_connector => ", ",
:two_words_connector => ", "
render 'edit'
end
end
private
# Collecting subjects names for school class and curriculum_id for each subject.
def collect_subjects_with_curriculums( school_class )
subjects = school_class.curriculums.collect do |c|
[ c.qualification.subject.subject_name, c.id ]
end
end
def timetable_for_class_with_existance_data( school_class )
return [] if Timetable.all.empty?
Timetable.select do |t|
( t.school_class.class_code == school_class.class_code ) and
not ( t.tt_room.blank? ) and not ( t.tt_type.blank? ) and
not ( t.curriculum_id.nil? )
end.to_a
end
# Return for school class it's timetable.
def timetable_for_class( school_class )
Timetable.select{|t| t.school_class.class_code == school_class.class_code }.to_a
end
def subjects_of_class( school_class )
subjects = school_class.curriculums.collect do |c|
c.qualification.subject.subject_name
end
end
# Return sorted by number of lesson tometable for one day.
def sorted_timetable_for_day( timetable, day )
timetable.select{ |t| t.tt_day_of_week == day }
.sort_by{ |e| e[:tt_number_of_lesson] }
end
# Return russian name for type of lesson.
def collect_types_of_lesson
[ ["Обязательно занятие", "Primary lesson"], ["Электив", "Extra"] ]
end
# Check if timetable already has been created for school class.
def timetable_exists?( school_class )
not timetable_for_class( school_class ).empty?
end
end
My view
<%= form_for #tt, :html => {:class => "form-horizontal"} do |f| %>
<%= field_set_tag do %>
<%= f.hidden_field :tt_number_of_lesson %>
<%= f.hidden_field :tt_day_of_week %>
<%= f.hidden_field :school_class_id %>
<div class="control-group">
<%= f.label :tt_day_of_week, "Day of the week", :class => "control-label" %>
<div class="controls">
<%= content_tag( :span, translate_day_of_week( #tt.tt_day_of_week ),
:class =>"input-xlarge uneditable-input span2" ) %>
</div>
</div>
<div class="control-group">
<%= f.label :tt_number_of_lesson, "Number of lesson", :class => "control-label" %>
<div class="controls">
<%= content_tag( :span, #tt.tt_number_of_lesson,
:class =>"input-xlarge uneditable-input span1" ) %>
</div>
</div>
<hr/>
<div class="control-group">
<%= f.label :curriculum_id, "Type of subject", :class => "control-label" %>
<div class="controls">
<%= f.select( :curriculum_id,
options_for_select( #subjects_with_curriculums,
#tt.curriculum_id ),
{ :include_blank => true }) %>
</div>
</div>
<div class="control-group">
<%= f.label :tt_room, "Code of the room", :class => "control-label" %>
<div class="controls">
<%= f.text_field :tt_room, :class => "span2", :maxlength => 3 %>
</div>
</div>
<div class="control-group">
<%= f.label :tt_type, "Type of the lesson", :class => "control-label" %>
<div class="controls">
<%= f.select( :tt_type,
options_for_select( #types_of_lesson,
#tt.tt_type ),
{ :include_blank => true }) %>
</div>
</div>
<%= f.submit "Update", :class => "btn btn-large btn-warning" %>
<% end %>
<% end %>
When i remove:
<div class="control-group">
<%= f.label :curriculum_id, "Type of subject", :class => "control-label" %>
<div class="controls">
<%= f.select( :curriculum_id,
options_for_select( #subjects_with_curriculums,
#tt.curriculum_id ),
{ :include_blank => true }) %>
</div>
</div>
<div class="control-group">
<%= f.label :tt_type, "Type of the lesson", :class => "control-label" %>
<div class="controls">
<%= f.select( :tt_type,
options_for_select( #types_of_lesson,
#tt.tt_type ),
{ :include_blank => true }) %>
</div>
</div>
I can view my test error. I can't figure what is going on.
You say this in your update controller:
render 'edit'
That simply tells Rails to render the edit template, it doesn't run any of the code associated with the edit controller, it simply renders edit.html.erb in the current context.
From the Layouts and Rendering in Rails Guide:
2.2.2 Rendering an Action’s View
[some stuff about render 'x', render :x, and render :action => :x being the same...]
Using render with :action is a frequent source of confusion for Rails newcomers. The specified action is used to determine which view to render, but Rails does not run any of the code for that action in the controller. Any instance variables that you require in the view must be set up in the current action before calling render.
Pay particular attention to the last sentence:
Any instance variables that you require in the view must be set up in the current action before calling render.
You're setting #subjects_with_curriculums in the edit controller but using the edit view from the update controller. The result is that #subjects_with_curriculums is nil when edit.html.erb tries to use it when update tries to render the edit view.
You'll need to set #subjects_with_curriculums in your update controller, you might need #types_of_lesson as well.

Rails: How to determine controller/action in view

This is my form partial:
<%= f.simple_fields_for :photo_attributes, :html => { :multipart => true } do |d| %>
<%= d.label :image, :label => 'Upload logo', :required => false %>
<%= d.file_field :image, :label => 'Image, :required => false', :style => 'margin-bottom:2px' %>
<%= d.input :image_url, :label => 'Billed URL', :required => false %>
<% end %>
If the action is edit I want to show this instead:
<%= f.simple_fields_for :photo, :html => { :multipart => true } do |d| %>
<%= d.label :image, :label => 'Upload logo', :required => false %>
<%= d.file_field :image, :label => 'Image, :required => false', :style => 'margin-bottom:2px' %>
<%= d.input :image_url, :label => 'Billed URL', :required => false %>
<% end %>
How can i achieve this?
current_page?(action: 'edit')
See ActionView::Helpers::UrlHelper#current_page?
Rails also makes the methods controller_path, controller_name, action_name available for use in the view.
Generally the form partial only contains the fields, not the form tag or the fields for, but if you have no other way, you can always see what params[:action] is currently set to and behave accordingly.
You could write something like
<%- form_url = #object.new_record? ? :photo_attributes : :photo %>
<% f.simple_fields_for form_url, :html => { :multipart => true } do |d| %>
That is, if you have an #object to check against. Otherwise you could use action_name (and even controller_name).
So something like:
<%- form_url = action_name == :edit ? :photo : :photo_attributes %>
<% f.simple_fields_for form_url, :html => { :multipart => true } do |d| %>
Hope this helps.
Rails 5: Display Action within the view
<%= action_name %>
If statement within the view
<% if action_name == "edit" %>
This is an edit action.
<% end %>
Just use #_controller.action_name in view

Rails collection_select

Manager :has_many :interns, :through => :assigns
Intern :has_many :managers, :through => :assigns
I am trying to create a new assign record. This is the assigns/new view where a given authenticated intern is creating a new association with a manager:
# assigns/new.html.erb
<% form_for #assign, :url => {:action => "create"} do |p| %>
<%= p.label "Select Manager", nil, :class => "label" %>
<%= collection_select :assign, :manager_id, #managers, :id, :manager_name, options ={:prompt => "Select a manager"}, html_options ={:class =>"listBox", :style => "width:25em;"} %>
<%= p.hidden_field :intern_id %>
<% end %>
# assigns controller
def new
#intern = Intern.find(params[:id])
#assign = Assign.new(:intern_id => #intern.id)
render :layout => 'centered'
end
def create
#assign = Assign.new(params[:assign])
#assign.manager_id = params[:manager_id]
if #assign.save
redirect_to :controller => "interns", :action => "home"
else
render :action => :new
end
end
The problem is: manager_id = nil. Why? Thanks very much.
Comment out following or
#assign.manager_id = params[:manager_id]
OR replace with
#assign.manager_id = params[:assign][:manager_id]
You don't need this line
#assign.manager_id = params[:manager_id]
You're already assigning the manager_id in the line before
#assign = Assign.new(params[:assign])

Rails custom validations with multi-model forms

I'm having problems with my rails custom validations.
def validates_hsp_program(*attr_names)
options = attr_names.extract_options!
regex = '^('
err = ''
$CetConfig.program.each do |key, val|
regex << val.to_s << '|'
err << $CetConfig.program_prefix + " " + val.to_s + ", "
end
regex.chomp!('|')
regex << ')$'
regex = Regexp.new(regex)
validates_each attr_names do | record, attr_name, value |
exit 1
unless value.nil? or value =~ regex
record.errors.add(attr_name, 'must be one of ' + err.chomp(", "));
end
end
end
The problem is that for debugging purposes I added exit 1 as I wasn't getting the error message for invalid date for that field. However, it never exits. This is the same thing I do in all my other custom validators. The only difference I can see is that this one is the second model of a multi-model form and all the others are on the first model... What am I doing wrong?
My Model
class ProfileProgram < ActiveRecord::Base
set_table_name "profile_program"
set_primary_key "STUDENT_ID"
belongs_to :profile_core, :primary_key => "STUDENT_ID", :foreign_key => "STUDENT_ID"
validates_presence_of :program
validates_hsp_program :program
end
Action from my controller
def create
#pc = ProfileCore.new(params[:profile_core])
#pp = ProfileProgram.new(params[:profile_program])
#pc.student_type = $CetConfig.student_type.application
#pc.AGENT_ID = current_agents_staff.AGENT_ID
year = #pc.contract.to_s
case #pp.program
when 10 then
sd = year + '-09-01'
ed = year + '-06-30'
when 51 then
sd = year + '-08-15'
ed = year + '-06-30'
when 52 then
sd = year + '-01-15'
ed = year + '-06-30'
when 12 then
sd = year + '-01-15'
ed = year + '-01-14'
else
sd = nil
ed = nil
end
#pc.start_date = Date.parse(sd) unless sd.nil?
#pc.end_date = Date.parse(ed) unless ed.nil?
#pc.program_status = $CetConfig.student_status.apply
if #pc.valid? and #pp.valid?
ProfileCore.transaction do
#pc.save!
#pp.save!
end
redirect_to(students_path(#pc.STUDENT_ID))
else
render :action => 'new'
end
end
My view (element_block is a helper that just stuffs the label and field into the right tags for the dl)
<% form_for :profile_core, #pc, :url => { :controller => 'core', :action => 'create'} do |f| %>
<%= error_messages_for :object => [ #pc, #pp ] %>
<dl>
<%= element_block f.label(:contract, 'Contract Year'), f.contract_year_select(:contract) %>
<% fields_for :profile_program do |pp| %>
<%= element_block pp.label(:program, 'Program'), pp.hsp_program_select(:program) %>
<% end %>
<%= element_block f.label(:passport_number, 'Passport Number'), f.text_field(:passport_number) %>
<%= element_block f.label(:passport_country, "Country that issued the student's passport"), f.countries_select(:passport_country) %>
<%= element_block f.label(:passport_expires, 'Passport Expiration Date'), f.text_field(:passport_expires, :class => 'datepicker') %>
<%= element_block f.label(:last_name, 'Last Name (as on passport)'), f.text_field(:last_name) %>
<%= element_block f.label(:first_name, 'First Name (as on passport)'), f.text_field(:first_name) %>
<%= element_block f.label(:middle_name, 'Middle Name (as on passport)'), f.text_field(:middle_name) %>
<%= element_block f.label(:other_names, 'Other Names'), f.text_field(:other_names) %>
<%= element_block f.label(:residence_street_address, 'Street Address'), f.text_field(:residence_street_address) %>
<%= element_block f.label(:residence_city, 'City'), f.text_field(:residence_city) %>
<%= element_block f.label(:residence_province, 'Province'), f.text_field(:residence_province) %>
<%= element_block f.label(:residence, 'Country'), f.text_field(:residence) %>
<%= element_block f.label(:residence_postal_code, 'Postal Code'), f.text_field(:residence_postal_code) %>
<%= element_block f.label(:birthdate, 'Date of Birth'), f.text_field(:birthdate, :class => 'datepicker', :id => "student_birth_date") %>
<%= element_block f.label(:citizenship, 'Country of Citizenship'), f.countries_select(:citizenship) %>
<%= element_block f.label(:birth_city, 'Birth City'), f.text_field(:birth_city) %>
<%= element_block f.label(:nationality, 'Nationality'), f.countries_select(:nationality) %>
<%= element_block f.label(:gender, 'Gender'), f.gender_select(:gender) %>
<%= element_block f.label(:email, 'Email'), f.text_field(:email) %>
<%= element_block f.label(:desires_esl, 'Does the student wish to participate in CLEP?'), f.bool_yes_no_select(:desires_esl) %>
<%= element_block f.label(:may_pay_tuiton, 'Willing to pay tuition'), f.yes_no_select(:may_pay_tuition) %>
</dl>
<div class="submit"><%= submit_tag("Proceed to Step Two") %></div>
<% end %>
This is how I ended up taking care of it with accepts_nested_attributes_for
Primary Model. (Irrelevant code replaced with ellipses)
class ProfileCore < ActiveRecord::Base
set_table_name "profile_core"
set_primary_key "STUDENT_ID"
belongs_to :agents_profile, :primary_key => "AGENT_ID", :foreign_key => "AGENT_ID"
has_one :profile_program, :primary_key => "STUDENT_ID", :foreign_key => "STUDENT_ID"
accepts_nested_attributes_for :profile_program
...
end
Secondary Model
class ProfileProgram < ActiveRecord::Base
set_table_name "profile_program"
set_primary_key "STUDENT_ID"
belongs_to :profile_core, :primary_key => "STUDENT_ID", :foreign_key => "STUDENT_ID"
validates_presence_of :program
validates_hsp_program :program
end
New action in the controller. (Create was irrelevant)
def new
#pc = ProfileCore.new
#pp = #pc.build_profile_program
end
View
<% form_for #pc, :url => { :controller => 'core', :action => 'create'} do |f| %>
<%= f.error_messages %>
<dl>
<%= element_block f.label(:contract, 'Contract Year'), f.contract_year_select(:contract) %>
<% f.fields_for :profile_program do |pp| %>
<%= element_block pp.label(:program, 'Program'), pp.hsp_program_select(:program) %>
<% end %>
<%= element_block f.label(:passport_number, 'Passport Number'), f.text_field(:passport_number) %>
...
<% end %>
It's not correct custom validation implementation, try something like that:
http://marklunds.com/articles/one/312
or http://chrisblunt.com/blog/2009/04/18/rails-writing-dry-custom-validators/

Resources