I am getting the following error when submitting an update form with nested models in Rails 3.2. Any help with this will be greatly appreciated.
NoMethodError in ProfilesController#update
undefined method `update_attributes' for nil:NilClass
Rails.root: C:/Documents and Settings/Workspace/project_x
Application Trace | Framework Trace | Full Trace
app/controllers/profiles_controller.rb:32:in `update'
Request
Parameters:
{"utf8"=>"✓",
"_method"=>"put",
"authenticity_token"=>"PQFLzhgvFzRyJ2BT/GdIpjRiOM729JoYQUHrZGUi08c=",
"profile"=>{"addresses_attributes"=>{"0"=>{"address1"=>"588 brodie",
"address2"=>"Apt1115",
"city"=>"Austin",
"state"=>"Alabama",
"zip"=>"78745",
"id"=>"1"}},
"vital_attributes"=>{"birthday(2i)"=>"1",
"birthday(3i)"=>"11",
"birthday(1i)"=>"1910",
"height_feet"=>"3",
"height_inches"=>"3",
"weight"=>"220",
"id"=>"1"},
"certificate_attributes"=>{"C_ASEL"=>"1",
"C_AMEL"=>"1",
"C_ASES"=>"1",
"C_AMES"=>"0",
"ATP_ASEL"=>"0",
"ATP_AMEL"=>"0",
"ATP_ASES"=>"0",
"ATP_AMES"=>"0",
"FI_ASE"=>"1",
"FI_AME"=>"1",
"FI_INSTA"=>"1",
"GI_Basic"=>"0",
"GI_Advanced"=>"0",
"GI_Instrument"=>"0",
"id"=>"1"}},
"commit"=>"Save & Continue",
"id"=>"1"}
Here are the models, controller, and form involved:
class Profile < ActiveRecord::Base
attr_accessible :addresses_attributes, :vital_attributes, :certificate_attributes
belongs_to :contact
has_many :addresses
has_one :certificate
has_one :vital
accepts_nested_attributes_for :addresses, :certificate, :vital
end
class Address < ActiveRecord::Base
belongs_to :profile
end
class Vital < ActiveRecord::Base
belongs_to :profile
end
class Certificate < ActiveRecord::Base
belongs_to :profile
end
class ProfilesController < ApplicationController
before_filter :authenticate, :only => [:show, :edit, :update]
before_filter :correct_contact, :only => [:show, :edit, :update]
.
.
.
def edit
#profile = Profile.find(params[:id])
#address = #profile.addresses.first
#vital = #profile.vital
#certificate = #profile.certificate
end
def update
if #profile.update_attributes(params[:profile])
flash[:success] = "Profile updated"
render 'edit'
else
render 'edit'
end
end
private #######################################################################
def authenticate
deny_access unless signed_in?
end
def correct_contact
#contact = Contact.find(params[:id])
redirect_to(root_path) unless current_contact?(#contact)
end
end
<%= form_for #profile, :html => { :class => "form-horizontal" } do |f| %>
<%= f.fields_for :addresses do |aa| %>
<%= aa.label :address1, "Address 1:", :class => "control-label" %>
<%=aa.label :address2, "Address 2:", :class => "control-label" %>
<%=aa.text_field :address2 %>
<%= aa.label :city, "City:", :class => "control-label" %>
<%= aa.text_field :city %>
<%= aa.label :state, "State:", :class => "control-label" %>
<%= aa.select(:state, options_for_select([["Alabama", "Alabama"], ["Texas", "Texas"]]), {}, :class => "select-auto-size") %>
<%= aa.label :zip, "Zip Code:", :class => "control-label" %>
<%= aa.text_field :zip %>
<% end %>
<%= f.fields_for :vital do |ab| %>
<%= ab.label :birthday, "Birthday:", :class => "control-label" %>
<%= ab.date_select :birthday, { :start_year => 1910, :end_year => 1995, :order => [:month, :day, :year] },{ :class => "select-auto-size" } %>
<%= ab.label :sex, "Sex:", :class => "control-label" %>
<%= ab.radio_button :sex, "Male", :id => "optionRadios1" %> Male
<%= ab.radio_button :sex, "Female", :id => "optionRadios2" %> Female
<%= ab.label :height, "Height:", :class => "control-label" %>
<%= ab.select(:height_feet, options_for_select([["3'", 3], ["4'", 4]]), {}, :class => "select-auto-size") %>
<%= ab.select(:height_inches, options_for_select([["3''", 3], ["4''", 4]]), {}, :class => "select-auto-size") %>
<%= ab.label :weight, "Weight:", :class => "control-label" %>
<%= ab.text_field :weight, :class => "input-mini" %><span>
<% end %>
<%= f.fields_for :certificate do |ac| %>
<label class="checkbox"><%= ac.check_box :C_ASEL %> Single</label>
<label class="checkbox"><%= ac.check_box :C_AMEL %> Multi</label>
<label class="checkbox"><%= ac.check_box :C_ASES %> Other</label>
<% end %>
<%= f.submit "Save & Continue", :class => "btn" %>
<% end %>
undefined method `update_attributes' for nil:NilClass
This is telling you that in your update controller action in ProfilesController you are calling update_attributes on a variable that is currently nil instead of an instance of the model you're trying to update. Specifically
def update
if #profile.update_attributes(params[:profile])
# ...
You are not assigning #profile to any model instance, so it's nil. You must do what you've done in your edit action.
def update
#profile = Profile.find(params[:id])
if #profile.update_attributes(params[:profile])
# ...
Related
Background: I would like to make a team and have the user verify the address of that team before the team is saved.
In my application I have a form that creates a team when the form is submitted. Within this form I have a partial that is suppose to render with a field location. When the user clicks submit within the partial form the location field (within the partial form and not the create team form) should go to the verify_Address action within the teams_controller. Instead of this happening I get an error when I load the page.
The error on pageload:
undefined local variable or method `verify_address' for #<#<Class:0x000001063ec8d8>:0x00000104555af0>
with this line highlighted: <%= form_for :team, url: verify_address, method: :post, remote:true do |f|%>
below are my files within the app.
route.rb file:
resources :users, :path => :captains, :as => :captains, :controller => "captains" do
resources :teams, :only => [:new, :create, :edit, :update, :destroy], controller: 'captains/teams'
end
resources :teams, :only => [:index, :show] do
resources :users, :path => :teammates, :as => :teammates, :controller => "teammates"
end
put 'captains/:id/teams/verify_address' => "teams#verify_address",as: 'verify_address'
get 'captains/:id/teams/verify_address' => "teams#verify_address"
controller/captains/teams_controller.rb:
class Captains::TeamsController < ApplicationController
respond_to :html, :js
def new
#team = Team.new
#captain = current_user
end
def verify_address
#address = params[:team][:location]
#validate_address = Team.validate_address(#address)
end
def create
#captain = current_user.id
#team = Team.create(
:name => params[:team][:name],
:location => params[:team][:location],
:sport => params[:team][:sport],
:captain_id => #captain,
:avatar => params[:team][:avatar]
)
if #team.present?
redirect_to #team # show view for team
end
binding.pry
end
end
the partial views/captains/teams/_verify_address.html.erb:
<%= form_for :team, url: verify_address, method: :post, remote:true do |f|%>
<div class = "form-group">
<%= f.label :location %>
<%= f.text_field :location, class: 'form-control', placeholder: "Enter wiki title", id:'team_title' %>
</div>
<div class = "form-group">
<%= f.submit "Verify address" ,class: 'btn btn-success' ,id: 'verify_address' %>
</div>
<% end %>
the main form views/captains/teams/new.html.erb:
<%= form_for :team, url: captain_teams_path(#captain, #team), method: :post do |f|
%>
<div class="form-group">
<%= f.label :avatar %>
<%= f.file_field :avatar %>
<%= f.hidden_field :avatar_cache %>
</div>
<div class = "form-group">
<%= f.label :name %>
<%= f.text_field :name, class: 'form-control', placeholder: "Enter wiki title", id:'team_title' %>
</div>
<div class = "form-group">
<%= f.label :sport %>
<%= f.text_field :sport, class: 'form-control', placeholder: "Enter wiki title", id:'team_title' %>
</div>
<div class = "form-group">
<%= f.label :location %>
<%= f.text_field :location, class: 'form-control', placeholder: "Enter wiki title", id:'team_title' %>
</div>
<div class = "form-group">
<%= f.submit class: 'btn btn-success' ,id: 'team_role_submit' %>
</div>
<% end %>
</div>
<%= render partial: "/captains/teams/verify_address", locals: { address: #address, validate_address: #validate_address}%>
</div>
Creating a custom route verify_address generates verify_address_path url helper, which you should use in your form.
i am getting this error end have no clue how to fix it. the weird thing is, it was working before. i think after i run annotate, it is broken, but not sure.
the error comes from confs.controller index and own methods.
it also rejects something like this: conf.machine_brand[0,1].upcase as NoMethodError [ ] bla bla
this is my conf model:
# == Schema Information
#
# Table name: confs
#
# id :integer not null, primary key
# machine_brand :string(255)
# machine_model :string(255)
# control_unit_brand :string(255)
# control_unit_model :string(255)
# tool_axis_x :decimal(, )
# tool_axis_y :decimal(, )
# tool_axis_z :decimal(, )
# rotary_axis_number :integer
# linear_axis_number :integer
# turning_mode :boolean
# milling_mode :boolean
# description :text
# xml :text
# user_id :integer
# developer_id :integer
# created_at :datetime not null
# updated_at :datetime not null
#
class Conf < ActiveRecord::Base
attr_accessible :linear_axis_number, :control_unit_brand, :control_unit_model, :description, :developer_id, :machine_brand, :machine_model, :milling_mode, :rotary_axis_number, :tool_axis_x, :tool_axis_y, :tool_axis_z, :turning_mode, :user_id, :xml
belongs_to :developer, :class_name => 'User', :foreign_key => 'developer_id'
belongs_to :receiver, :class_name => 'User', :foreign_key => 'user_id'
validates :user_id, presence: true
validates :developer_id, presence: true
end
this is confs.controller:
class ConfsController < ApplicationController
before_filter :signed_in_user, only:[:index, :edit, :update, :destroy]
before_filter :developer_user, only: :destroy
def new
#conf = Conf.new
end
def index
#grouped = {}
Conf.all.each do |conf|
letter = conf.machine_brand.slice(0,1).upcase
#grouped[letter] ||= []
#grouped[letter] << conf
end
end
def show
#conf = Conf.find(params[:id])
respond_to do |format|
format.html #index.html.erb
format.json { render json: #conf }
format.xml { render xml: #conf }
end
end
def own
#grouped = {}
Conf.where(:developer_id => current_user.id).each do |conf|
letter = conf.machine_brand.slice(0,1).upcase
#grouped[letter] ||= []
#grouped[letter] << conf
end
end
def create
#conf = Conf.new(conf_params)
if #conf.save
flash[:success] = "New Configuration uploaded!"
redirect_to conf_show_path
else
flash[:error] = "There is a problem!"
render 'new'
end
end
def destroy
#conf = Conf.find(params[:id]).destroy
redirect_to conf_show_own_path
end
def update
#conf.update_attributes(params[:conf])
end
private
def signed_in_user
unless signed_in?
store_location
redirect_to signin_url, notice: "Please sign in"
end
end
def admin_user
redirect_to(root_path) unless current_user.admin?
end
def developer_user
redirect_to(root_path) unless current_user.developer?
end
def conf_params
params.require(:conf).permit(:xml, :user_id, :developer_id) if params[:conf]
end
end
and this is conf.new if you wish:
<% provide(:title, 'New Configuration')%>
<h1> Upload new configuration </h1>
<div class="row">
<div class="span6 offset3">
<%= form_for #conf, :html => {:multipart => true} do |f| %>
<%= f.label :machine_brand %>
<%= f.text_field :machine_brand %>
<%= f.label :machine_model %>
<%= f.text_field :machine_model %>
<%= f.label :control_unit_brand %>
<%= f.text_field :control_unit_brand %>
<%= f.label :control_unit_model %>
<%= f.text_field :control_unit_model %>
<%= f.label :tool_axis_x %>
<%= f.text_field :tool_axis_x %>
<%= f.label :tool_axis_y %>
<%= f.text_field :tool_axis_y %>
<%= f.label :tool_axis_z %>
<%= f.text_field :tool_axis_z %>
<%= f.label :rotary_axis_number %>
<%= f.text_field :rotary_axis_number %>
<%= f.label :linear_axis_number %>
<%= f.text_field :linear_axis_number %>
<%= f.label :turning_mode %>
<%= f.text_field :turning_mode %>
<%= f.label :milling_mode %>
<%= f.text_field :milling_mode %>
<%= f.label :description %>
<%= f.text_field :description %>
<%= f.label :xml %>
<%= f.text_field :xml %>
<%= f.label :client %>
<%= f.collection_select :user_id, User.where(:admin => false, :developer => false), :id, :name, options ={:prompt => "Select a client"}, :class =>"user" %>
<%= f.label :me %>
<%= f.collection_select :developer_id, User.where(:id => current_user.id), :id, :name, options ={:prompt => "Select me"}, :class =>"user" %>
<br />
<%= f.submit "Upload", class: "btn btn-large btn-primary" %>
<% end %>
</div>
</div>
conf.machine_brand.slice(0,1)
I think you got the error here machine_brand so simply do in your controller
letter = params[:machine_brand].to_s.slice(0,1).upcase unless params[:machine_brand].blank?
or
letter = params[:conf][:machine_brand].to_s.slice(0,1).upcase unless params[:machine_brand].blank?
As #Rajarshi mentioned, the error is in the following code
conf.machine_brand.slice(0,1).upcase
The error says that you're calling slice on a nil object, which means that one of your conf records has a nil machine_brand. I'm not sure how you'd want to approach this problem but this issue will be lessened if you add a validation that requires a machine_brand
class Conf < ActiveRecord::Base
...
validates :machine_brand, presence: true
or you can only fetch for records where machine_brand is present
Conf.where('machine_brand IS NOT NULL').each do |conf|
conf.machine_branch.slice(...)
I have a User model and a Company model linked like this:
class User < ActiveRecord::Base
belongs_to :company
accepts_nested_attributes_for :company
end
class Company < ActiveRecord::Base
has_many :users
end
On the sign in page, I want the user to set up both his info (mail, password) and his company info (several fields). So my form looks like this:
<%= simple_form_for #user, :html => { :class => 'form-horizontal' } do |f| %>
<%= f.input :email, :required => true, :placeholder => "user#domain.com" %>
<%= f.input :password, :required => true %>
<%= f.input :password_confirmation, :required => true %>
<h2>Company info</h2>
<%= simple_fields_for :company, :html => { :class => 'form-horizontal' } do |fa| %>
<%= fa.input :name %>
<%= fa.input :url %>
<%= fa.input :description, :as => :text, :input_html => { :cols => 60, :rows => 3 } %>
<%= fa.input :logo %>
<%= fa.input :industry %>
<%= fa.input :headquarters %>
<% end %>
<div class="form-actions">
<%= f.submit nil, :class => 'btn btn-primary' %>
<%= link_to t('.cancel', :default => t("helpers.links.cancel")),
root_url, :class => 'btn' %>
</div>
<% end %>
My user model has a company_id:integer field. So logically, when I sign in the user, the first thing to do is to create the Company before the User and then give to the user creation model the appropriate company_id. So I wrote this:
class UsersController < ApplicationController
before_create :create_company
def new
#user = User.new
end
def create
#user = User.new(params[:user])
if #user.save
redirect_to root_url, :notice => "Registration successful."
else
render :action => 'new'
end
end
private
def create_company
#company = Company.new(params[:company])
if #company.save
self.company_id = #company.id
else
render :action => 'new'
end
end
end
Problem is: when accessing /users/new I get this error:
undefined method `before_create' for UsersController:Class
What's going wrong? I checked, before_create has not been deprecated, I'm in Rails 3.2.8. This is probably something stupid with my create_company method but I can't figure why...
Thanks a lot for helping!
before_create is a hook method belonging to ActiveRecord
before_filter is a hook method belonging to Controller.
so I suggest you to re-build your code after you make clear which is which. ^_^
I have been having some serious trouble all weekend trying to get this basic contact form to work correctly.
Effectively I want the user to be able to complete the form, hit send and have the message send straight to a predefined email address.
The error I am continually getting is:
NoMethodError in Messages#new
Showing C:/Sites/jobapp_v2/app/views/messages/new.html.erb where line #1 raised:
undefined method `[]' for nil:NilClass
Extracted source (around line #1):
1: <%= form_for #message, :url => contact_path do |f| %>
2:
3: <div class="field">
4: <%= f.label :name %>
I have the following setup:
Messages controller
class MessagesController < ApplicationController
def new
#user = current_user
#message = Message.new
end
def create
#message = Message.new(params[:message])
if #message.valid?
NotificationsMailer.new_message(#message).deliver
redirect_to(root_path, :notice => "Message was successfully sent.")
else
flash.now.alert = "Please fill all fields."
render :new
end
end
end
Message Model
class Message < ActiveRecord::Base
include ActiveModel::Validations
include ActiveModel::Conversion
extend ActiveModel::Naming
attr_accessor :name, :email, :subject, :body
validates :name, :email, :subject, :body, :presence => true
validates :email, :format => { :with => %r{.+#.+\..+} }, :allow_blank => true
def initialize(attributes = {})
attributes.each do |name, value|
send("#{name}=", value)
end
end
def persisted?
false
end
end
Routes.rb
JobappV2::Application.routes.draw do
devise_for :users
resources :newsletters
match "contact" => "messages#new", :as => "contact", :via => :get
match "contact" => "messages#create", :as => "contact", :via => :post
get "pages/about"
get "pages/contact"
get "pages/terms"
resources :jobs
resources :users do
resources :jobs
end
root :to => 'jobs#index'
end
NotificationsMailer.rb
class NotificationsMailer < ActionMailer::Base
default :from => "advertise#artisanmag.co.uk"
default :to => "tom.pinchen#artisanmag.co.uk"
def new_message(message)
#message = message
mail(:subject => "Hello")
end
end
Views/notification_mailer/new_message.text.erb
Name: <%= #message.name %>
Email: <%= #message.email %>
Subject: <%= #message.subject %>
Body: <%= #message.body %>
Views/messages/new.html.erb
<%= form_for #message, :url => contact_path do |f| %>
<div class="field">
<%= f.label :name %>
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :email %>
<%= f.text_field :email %>
</div>
<div class="field">
<%= f.label :subject %>
<%= f.text_field :subject %>
</div>
<div class="field">
<%= f.label :body %>
<%= f.text_area :body %>
</div>
<%= f.submit "Send" %>
<% end %>
I really for can't work out at all what is causing this undefined method error so any help people can offer would really be much appreciated! Thanks in Advance :)
The way you have hooked things up, you don't need :url => contact_path
<%= form_for #message do |f| %>
will work just fine.
I am having problems with my ruby on rails app. I have two models - 'patient' and 'address', a patient has one address, and an address belongs to a patient.
Patient.rb
class Patient < ActiveRecord::Base
has_many :charge_slips
has_one :address
validates_presence_of :last_name
validates_presence_of :first_name
validates_presence_of :middle_name
end
Address.rb
class Address < ActiveRecord::Base
belongs_to :patient
validates_associated :patient
end
Patient-controller.rb
class PatientController < ApplicationController
def index
#title = "Outpatient Services - Patient"
#today = Date.today.to_formatted_s(:long)
#patients = Patient.find(:all)
end
def new
#patient = Patient.new
#address = Address.new
end
def create
#patient = Patient.new(params[:patient])
#patient.created_on = Date.today.to_formatted_s(:long)
if #patient.save
#address = Address.new(params[:address])
#address.patient_id = #patient.id
if #address.save
redirect_to :action => 'index'
else
redirect_to :action => 'new'
end
redirect_to :action => 'index'
else
redirect_to :action => 'new'
end
end
end
new.html.rb
<%= content_tag('h3', 'Create New Patient') %>
<hr>
<% form_for #patient, :url => { :action => "create" } do |patient_form| -%>
<%= error_messages_for :patient %>
<%= patient_form.label :last_name, 'Last Name:' %> <%= patient_form.text_field :last_name, :size => 30 %><br>
<%= patient_form.label :first_name, 'First Name:' %> <%= patient_form.text_field :first_name, :size => 30 %><br>
<%= patient_form.label :middle_name, 'Middle Name:' %> <%= patient_form.text_field :middle_name, :size => 30 %><br>
<fieldset>
<legend>Patient's Permanent Address</legend>
<%= error_messages_for :address %>
<% patient_form.fields_for #address do |address_fields| -%>
<%= address_fields.label :street_name, 'Street Name:' %> <%= address_fields.text_field :street_name %><br>
<%= address_fields.label :barangay, 'Barangay:' %> <%= address_fields.text_field :barangay %><br>
<%= address_fields.label :city_municipality, 'City/Municipality:' %> <%= address_fields.text_field :city_municipality %><br>
<%= address_fields.label :country, 'Country:' %> <%= address_fields.text_field :country %><br>
<%= address_fields.label :zip_cide, 'Zip Code:' %> <%= address_fields.text_field :zip_code %><br>
<% end -%>
</fieldset>
<%= submit_tag "Add Patient" %>
<% end -%>
Everytime I add a new patient an error is thrown. Here is a part of the error:
ActiveRecord::AssociationTypeMismatch in PatientController#create
Address(#31360520) expected, got HashWithIndifferentAccess(#23815500)
RAILS_ROOT: C:/www/Outpatient Application Trace | Framework Trace | Full Trace
C:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.3/lib/active_record/associations/association_proxy.rb:263:in `raise_on_type_mismatch'
C:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.3/lib/active_record/associations/has_one_association.rb:52:in `replace'
C:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.3/lib/active_record/associations.rb:1246:in `address='
C:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.3/lib/active_record/base.rb:2740:in `send'
C:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.3/lib/active_record/base.rb:2740:in `attributes='
C:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.3/lib/active_record/base.rb:2736:in `each'
C:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.3/lib/active_record/base.rb:2736:in `attributes='
C:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.3/lib/active_record/base.rb:2434:in `initialize'
C:/www/Outpatient/app/controllers/patient_controller.rb:14:in `new'
C:/www/Outpatient/app/controllers/patient_controller.rb:14:in `create'
I am new to RoR and would like to learn the language through practice. I want to know what might be wrong with the code. Thanks!
First your Patient model needs an accepts_nested_attributes_for
class Patient < ActiveRecord::Base
has_many :charge_slips
has_one :address
validates_presence_of :last_name
validates_presence_of :first_name
validates_presence_of :middle_name
accepts_nested_attributes_for :address
end
Your controller can be simplified a great deal. There is no need to save the address separately since #patient.save will take care of that. You don't need to set
the created_on attribute manually since it will be set automagically :) Also when #patient.save fails you probably want to render :action => 'new' and not redirect_to :action => 'new'. This will redisplay the form with any validation errors (redirect_to will not.)
Also note that i renamed your controller class to PatientsController instead of PatientController. This will be more in line with Rails' RESTful conventions and will also help you simplify your view a bit. If you do this you'll need a map.resources :patients in your routes.db file, and you'll need to rename your files too.
class PatientsController < ApplicationController
def index
#title = "Outpatient Services - Patient"
#today = Date.today.to_formatted_s(:long)
#patients = Patient.find(:all)
end
def new
#patient = Patient.new
#patient.build_address
end
def create
#patient = Patient.new(params[:patient])
if #patient.save
redirect_to :action => 'index'
else
render :action => 'new'
end
end
end
Your view has a small error. It needs to be fields_for :address and not fields_for #address. Also since your controller is now RESTful your can remove the :url => { :action => "create" } part.
<%= content_tag('h3', 'Create New Patient') %>
<hr>
<% form_for #patient do |patient_form| -%>
<%= error_messages_for :patient %>
<%= patient_form.label :last_name, 'Last Name:' %> <%= patient_form.text_field :last_name, :size => 30 %><br>
<%= patient_form.label :first_name, 'First Name:' %> <%= patient_form.text_field :first_name, :size => 30 %><br>
<%= patient_form.label :middle_name, 'Middle Name:' %> <%= patient_form.text_field :middle_name, :size => 30 %><br>
<fieldset>
<legend>Patient's Permanent Address</legend>
<%= error_messages_for :address %>
<% patient_form.fields_for :address do |address_fields| -%>
<%= address_fields.label :street_name, 'Street Name:' %> <%= address_fields.text_field :street_name %><br>
<%= address_fields.label :barangay, 'Barangay:' %> <%= address_fields.text_field :barangay %><br>
<%= address_fields.label :city_municipality, 'City/Municipality:' %> <%= address_fields.text_field :city_municipality %><br>
<%= address_fields.label :country, 'Country:' %> <%= address_fields.text_field :country %><br>
<%= address_fields.label :zip_cide, 'Zip Code:' %> <%= address_fields.text_field :zip_code %><br>
<% end -%>
</fieldset>
<%= submit_tag "Add Patient" %>
<% end -%>
Hope this helps :)