Please I need a help from you guys:
I have 2 models & 2 controllers:
- app/controller/register_controller.rb & app/model/register.rb
- app/controller/city_controller.rb & app/model/city.rb
In db/migrate/002_creates_city.rb, I fill it :
class CreateCitys < ActiveRecord::Migration
def self.up
create_table :citys do |t|
t.column :citys, :string
end
say_with_time ‘Adding Foreign Keys’ do
execute ‘ALTER TABLE registers ADD CONSTRAINT fk_reg_citys
FOREIGN KEY ( city_name ) REFERENCES kotas( id ) ON DELETE CASCADE’
end
def self.down
drop_table :kotas
end
end
I put has_many :registers in app/models/citys.rb
And belongs_to :citys in app/models/registers.rb
Then in app/view/register/_form.rhtml, I write this
…
Tujuan
<%= select_tag(:city, “-Select a city” +
options_from_collection_for_select(@find_city, :city, :city)) %>
…
In app/controller/register_controller.rb i declare @find_city like this:
…
def new
@register = Register.new
@find_city = City.find(:all, :order=>“city”)
end
…
And no changes for other scripts in controller that is from scaffold
generator.
I run the script, in http://localhost:3000/register/new the city name
list is shown. But I after I submit, the next page show me this error:
NoMethodError in /register#create
Showing app/views/register/_form.rhtml where line #18 raised:
You have a nil object when you didn’t expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.inject
Extracted source (around line #18):
15:
16:
Tujuan
17:
18: <%= select_tag(:city, “-Select a city” +
options_from_collection_for_select(@find_city, :city, :city)) %>
19:
20:
21:
Please help me what should i do so the selected city in scroll down menu
can be inserted to database register for column city_name.
Thank You