rails 3.2 NoMethodError undefined method `slice' for nil:NilClass - ruby-on-rails

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(...)

Related

Not able to save data into Model

I can't save the data into my model. Every time when the code run it will ran into the else statement which failed to save the data in the CREATE action. Any idea?
This is my invoices_controller.rb
class InvoicesController < ApplicationController
def new
#permits = Permit.find(params[:permit_id])
#invoice = Invoice.new
end
def create
#permit = Permit.find(params[:permit_id])
#invoice = #permit.build_invoice(invoice_params)
if #invoice.save
redirect_to payment_path
else
redirect_to root_path
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_invoice
#invoice = Invoice.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def invoice_params
params.require(:invoice).permit(:vehicle_type, :name, :department, :carplate, :duration, :permitstart, :permitend, :price, :time)
end
end
Invoices/new.html.erb ( This is the data I wanted to save)
<% provide(:title, 'Invoice') %>
<h1>Invoice</h1>
<div class="row">
<div class="col-md-6 col-md-offset-3" id="datashow">
<%= form_for(#invoice) do |f| %>
<h2>Time : <%=#permits.created_at%></h2></br>
<h2>Invoice ID : <%=#permits.id%></h2></br>
<%= f.label :"Vehicle" %>
<%= f.text_field :vehicle_type, :value => #permits.vehicle_type, readonly: true %>
<%= f.label :"License Plate" %>
<%= f.text_field :carplate, :value => #permits.carplate, readonly: true %>
<%= f.label :"Student ID" %>
<%= f.text_field :studentid, :value => #permits.studentid, readonly: true %>
<%= f.label :name %>
<%= f.text_field :name, :value => #permits.name, readonly: true %>
<%= f.label :"Department of applicant" %>
<%= f.text_field :department, :value => #permits.department, readonly: true %>
<%= f.label :permit_start %>
<%= f.text_field :permitstart, :value => #permits.permitstart, readonly: true %>
<%= f.label :permit_end %>
<%= f.text_field :permitend, :value => #permits.permitend, readonly: true %>
<%= f.label :"Price" %>
<%= (f.text_field :price, :value => '$AUD 50' , readonly: true) %>
<%= hidden_field_tag(:permit_id, #permits.id) %>
<%= f.submit "Make Payment", class: "btn btn-primary" %>
<% end %>
</div>
</div>
Invoice.rb
class Invoice < ApplicationRecord
belongs_to :user
has_one :receipt
belongs_to :permit
end
Permit.rb
class Permit < ApplicationRecord
belongs_to :user
has_one :invoice
end
If you are unsure why your object is not created, you have multiple options.
First you can use #invoice.save! instead of #invoice.save during debugging. This will raise an exception and give you some clues, what's going wrong.
Or you can use a debugger and inspect #invoice.errors.full_messages.
Further more you can output #invoice.errors.full_messages via Rails.logger.error #invoice.errors.full_messages.to_sentence.
Or you can use the error message as a flash message flash[:error] = #item.errors.full_messages.to_sentence
This should help you find the error.
from: build method on ruby on rails
build won't "create" a record in database, just create a new object in memory so that the view can take this object and display something, especially for a form.
So build isn't working because you aren't creating (create and saving) a record. build doesn't save a record.
Try:
def create
#permit = Permit.find(params[:permit_id])
#invoice = #permit.invoices.create(invoice_params)
if #invoice.save
redirect_to payment_path
else
redirect_to root_path
end
end

Rails passing params to different model and then saving it not working

I have a 'post' controller in that I have two variable title and body which I am passing through strong parameters.But I need to use two other variable which are path and name which are in different model name 'Document'..And also I am saving the content in database ..but unable to do so..getting this error view [posts/_form.html.erb]
undefined method `name' for #
[posts_controller]
class PostsController < ApplicationController
before_action :authenticate_user!
def index
#posts = Post.user_post(current_user).order('created_at DESC').paginate(:page => params[:page], :per_page => 5)
end
def new
#post = Post.new
end
def show
#post = find_params
end
def create
#post = Post.create(post_params)
#post.user = current_user
if #post.save
redirect_to #post
else
render 'new'
end
end
def edit
#post = find_params
end
def update
#post = find_params
if #post.update(post_params)
redirect_to #post
else
render 'edit'
end
end
def destroy
#post = find_params
#post.destroy
redirect_to posts_path
end
private
def post_params
params.require(:post).permit(:title, :body)
Document.new(params,:files=>[])
end
def find_params
Post.find(params[:id])
end
end
[post/_form.html.erb]
<%= form_for #post,html: { multipart: true } do |f| %>
<% if #post.errors.any? %>
<div id="errors">
<h2><%= pluralize(#post.errors.count, "error") %> prevented this post from saving:</h2>
<ul>
<% #post.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<%= f.label :title %><br>
<%= f.text_field :title %><br>
<br>
<%= f.label :body %><br>
<%= f.text_field :body %><br>
<br>
<%= f.label :name %> <br>
<%= f.text_field :name %><br>
<br>
<br>
<%= f.label :path %><br>
<%= f.file_field :path %><br>
<%= f.submit %>
<% end %>
[document.rb]
class Document < ActiveRecord::Base
validates :name, presence: true
validates :path, presence: true
validates :resource_type, presence: true
validates :resource_id, presence: true
mount_uploader :path, PathUploader
validates :name, presence: true
# def self.abc
# params.permit(:name,:path)
# end
def initialize(params,file)
params=file[:name]
#params.permit(name =>:name,path =>:path)
end
end
undefined method `name' for #
You're referencing a non-existent attributes for your Post form:
<%= form_for #post,html: { multipart: true } do |f| %>
<%= f.label :title %><br>
<%= f.text_field :title %><br>
<br>
<%= f.label :body %><br>
<%= f.text_field :body %><br>
<%= f.submit %>
<% end %>
Remove :name & :path references.
--
If you want to pass "extra" attributes to another model, you need to use accepts_nested_attributes_for or set the params separately to your "primary" model:
#app/models/post.rb
class Post < ActiveRecord::Base
has_many :documents
accepts_nested_attributes_for :documents
end
#app/models/document.rb
class Document < ActiveRecord::Base
belongs_to :post
end
This will allow you to pass the documents as "nested" attributes of your Post model:
#app/controllers/posts_controller.rb
class PostsController < ApplicationController
def new
#post = Post.new
#post.documents.build
end
def create
#post = Post.new post_params
#post.save
end
private
def post_params
params.require(:post).permit(:title, :body, documents_attributes: [:name, :path])
end
end
#app/views/posts/_form.html.erb
<%= form_for #post do |f| %>
<%= f.text_field :title %>
<%= f.text_area :body %>
<%= f.fields_for :documents do |d| %>
<%= d.text_field :name %>
<%= d.text_field :path %>
<% end %>
<%= f.submit %>
<% end %>
So undefined method on a model will indicate that, well, the method doesn't exist on the model. Want to see a model's methods? Post.methods. However, in this example, the column name is not defined on the model., and you're trying to tell Post that it has a name. What you need to do is nest your parameters.
While there is a ton of cleaning up that might want to focus on first, your answer is found in the accepts_nestable_attributes_for class methods, as shown here, http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html, and strong_params documentation as shown here, http://edgeapi.rubyonrails.org/classes/ActionController/StrongParameters.html
In your case, you want to create a new document from a post. Your permitted params hash will look like this,
params.require(:post).permit(:title, :body, :document_attributes => [:name])
Ensure that document_attributes is singular; if a person has_many pets (for example), then you'd have pets_attributes.
In your form, something that often trips people up is the builder.
<%= form_for #post do |f| %>
<%= f.text_field :title %>
<%= f.text_field :body %>
<%= f.fields_for #post.document do |document_field| %>
<%= document_field.text_field :name %>
<% end %>
<%= f.submit %>
<% end %>
Make sure that you're telling ERB that <%= f.fields_for %>, not just <% f.fields_for %>.

rails How to save a serialized hash

I have a serialized object :address in Hotel model and I don't know how to save it properly in the DB. I have the following:
#model hotel
class Hotel < ActiveRecord::Base
belongs_to :user
serialize :address, Hash
end
...and view 'new'
<%= form_for(#hotel) do |f| %>
<%= f.label :title %>
<%= f.text_field :title %>
<%= f.label :stars %>
<%= f.text_field :stars %>
<%= f.label :room, "Room description" %>
<%= f.text_area :room, size: "20x10" %>
<%= f.label :price %>
<%= f.number_field :price %>
<%= f.fields_for :address do |o| %>
<%= o.label :country %>
<%= o.text_field :country %>
<%= o.label :state %>
<%= o.text_field :state %>
<%= o.label :city %>
<%= o.text_field :city %>
<%= o.label :street %>
<%= o.text_field :street %>
<% end %>
<%= f.submit "Create hotel", class: "btn btn-large btn-primary" %>
<% end %>
With this code what I get is: hotel address nil...
Okay.. We will go another way. After googling much I came to this code:
# hotel.rb model
class Hotel < ActiveRecord::Base
class Address
include ActiveModel::Conversion
extend ActiveModel::Naming
attr_accessor :country, :state, :city, :street
def persisted?; true end
def id; 1 end
def self.load json
obj = self.new
unless json.nil?
attrs = JSON.parse json
obj.country = attrs['country']
obj.state = attrs['state']
obj.city = attrs['city']
obj.street = attrs['street']
end
obj
end
def self.dump obj
obj.to_json if obj
end
end
belongs_to :user
serialize :address, Address
end
and the same view new.html.erb
The result is: Address:0xb0e530c
So, nothing saves in the database... I don't know what to try next, I'll appreciate any help. Didn't know that serialized object will cause so much problems to me.
THANKS!
PS Here's hotels_controller.
class HotelsController < ApplicationController
before_action :signed_in_user, only: [:index, :edit, :update, :destroy]
def new
#hotel = Hotel.new
end
def index
#hotels = Hotel.paginate(page: params[:page])
end
def show
#hotel = Hotel.find(params[:id])
end
def create
#hotel = current_user.hotels.build(hotel_params)
if #hotel.save
flash[:success] = "Hotel created!"
redirect_to #hotel
else
render 'new'
end
end
private
def hotel_params
params.require(:hotel).permit(:title, :stars, :room, :price, :address)
end
end
First thing on your migration file make sure that you are saving your fields as a test like
def self.up
add_column : hotels, : address, :text
end
Then Rails will convert it into YAML / Hash for you (and perform proper serialization).
Wish you the best of luck.
PS take a look at https://stackoverflow.com/a/6702790/1380867

Nested Forms for Embedded Documents

I have the following Models
cities(id, name, geo {lng,lat})
geo(lng,lat)
Cities Model
class City
include Mongoid::Document
include Mongoid::Timestamps
field :name, type: String
field :timezone, type: String
field :slug, type: String
belongs_to :region
belongs_to :country
embeds_one :geo_location
accepts_nested_attributes_for :geo_location
end
Geo Locations Model
class GeoLocation
include Mongoid::Document
include Mongoid::Timestamps
field :lng, type: String
field :lat, type: String
embedded_in :city
end
Cities Controller
class CitiesController < ApplicationController
before_action :set_city, only: [:show, :edit, :update, :destroy]
# GET /cities
def index
#cities = City.all
end
# GET /cities/1
def show
end
# GET /cities/new
def new
#city = City.new
#regions = Region.all.asc(:name)
#countries = Country.all.asc(:name)
end
# GET /cities/1/edit
def edit
#regions = Region.all.asc(:name)
#countries = Country.all.asc(:name)
end
# POST /cities
def create
#city = City.new(city_params)
if #city.save
redirect_to #city, notice: 'City was successfully created.'
else
render action: 'new'
end
end
# PATCH/PUT /cities/1
def update
if #city.update(city_params)
redirect_to #city, notice: 'City was successfully updated.'
else
render action: 'edit'
end
end
# DELETE /cities/1
def destroy
#city.destroy
redirect_to cities_url, notice: 'City was successfully destroyed.'
end
private
# Use callbacks to share common setup or constraints between actions.
def set_city
#city = City.find(params[:id])
end
# Only allow a trusted parameter "white list" through.
def city_params
params.require(:city).permit(:name, :timezone, :region_id, :country_id, :slug, :geo_locations_attributes => [:id, :lag, :lat])
end
end
Form:
<%= form_for(#city) do |f| %>
<% if #city.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#city.errors.count, "error") %> prohibited this city from being saved:</h2>
<ul>
<% #city.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :name %><br>
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :country %><br>
<%= f.collection_select :country_id, #countries, :id, :name, :prompt => "Please Select" %>
</div>
<div class="field">
<%= f.label :region %><br>
<%= f.collection_select :region_id, #regions, :id, :name, :prompt => "Please Select" %>
</div>
<div class="field">
<%= f.label :timezone %><br>
<%= f.text_field :timezone %>
</div>
<div class="field">
<%= f.label :slug %><br>
<%= f.text_field :slug %>
</div>
<%= f.fields_for :geo_locations do |geo_location| %>
<div class="field">
<%= geo_location.label :lag %><br>
<%= geo_location.text_field :lag %>
</div>
<div class="field">
<%= geo_location.label :lat %><br>
<%= geo_location.text_field :lat %>
</div>
<% end %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
New View
<h1>New city</h1>
<%= render 'form' %>
<%= link_to 'Back', cities_path %>
The ERROR I am getting
Unpermitted parameters: geo_location
On controller, Replace your city_params method with this,
def city_params
params.require(:city).permit(:name, :timezone, :region_id, :country_id, :slug, :geo_location_attributes => [:id, :lag, :lat])
end
On view, Replace this "f.fields_for :geo_locations" with "f.fields_for :geo_location"
Problem in geo_locations_attributes. It should be geo_location_attributes as this is one-to-one relationship.

AssociationTypeMismatch Error on Ruby on Rails app

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 :)

Resources