Scroll Down Menu Problem

Please I need a help from you guys:

I have 2 models & 2 controllers:

  1. app/controller/register_controller.rb & app/model/register.rb
  2. 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

Sorry this the correction of MIGRATION SCRIPT:


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 citys( id ) ON DELETE CASCADE’
end

def self.down
drop_table :citys
end
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

Sounds like your create action (which you haven’t shown) is rendering
the form partial again, and that you’re not setting @find_city in
that action.
Normally you would only do this if the validation failed. You’ll need
to set @find_city again (or better, put that code in its own method
you can call from both new & create_

Fred

Frederick C. wrote:

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

Sounds like your create action (which you haven’t shown) is rendering
the form partial again, and that you’re not setting @find_city in
that action.
Normally you would only do this if the validation failed. You’ll need
to set @find_city again (or better, put that code in its own method
you can call from both new & create_

Fred


Thanks for the advice. It works. The problem now is the selected city in
scroll down cant be stored on database it always has no value. each time
i select city and press submit, the error always like this. in City
table only has ID and CITY for columns. Please need advice again.

1 error prohibited this register from being saved

There were problems with the following fields:

* City can't be blank

On 8 Oct 2007, at 02:26, Fransiscus X. wrote:

There were problems with the following fields:

* City can't be blank

We can’t guess what’s in your create action.

Fred

Sorry again, Here is the corrections of action Create script.

def create
@register = Register.new(params[:register])
@find_city = City.find(:all, :order=>“id”)
if @register.save
flash[:notice] = ‘Register was successfully created.’
redirect_to :action => ‘list’
else
render :action => ‘new’
end
end

Frederick C. wrote:

On 8 Oct 2007, at 02:26, Fransiscus X. wrote:

There were problems with the following fields:

* City can't be blank

We can’t guess what’s in your create action.

Fred


here is Create action script in controller register_controller.rb. Each
time i select city, the city always no value or can be selected. Please
help me. Thanks.

def create
@register = Register.new(params[:register])
@find_city = City.find(:all, :order=>“id”)
if @city.save
flash[:notice] = ‘Register was successfully created.’
redirect_to :action => ‘list’
else
render :action => ‘new’
end
end

Create Script is above and _form.rhtml script is :

City

<%= select_tag (:city, “-Select a city” +
options_from_collection_for_select(@find_city, “id”, “city”)) %>

Please let me know what should I do? so the selected city can be saved
without blank information. Thanks.

On 8 Oct 2007, at 02:46, Fransiscus X. wrote:

Sorry again, Here is the corrections of action Create script.

def create
@register = Register.new(params[:register])

<%= select_tag (:city, “-Select a city” +
options_from_collection_for_select(@find_city, “id”, “city”)) %>

Your parameters are in params, not params[:register]. Either change
the form so that they are in params[:register], or do Register.new
(params)

Fred

One Question again

How to make our selected city from scrolldown menu is consistent in Edit
Area. I mean automatic select city that we have selected and stored in
database before.

Thanks.

I made script _from.rhtml like this

<%= select_tag ‘register[city]’, “<option value=“0”>Select a
city” + options_from_collection_for_select(@find_city, “city”,
“city”)) %>

And it works well. Thank you for your helping. And we need to put


def Create
@find_city = City.find(:all, :order=>“id”)

def Update
@find_city = City.find(:all, :order=>“id”)

def Edit
@find_city = City.find(:all, :order=>“id”)

def New
@find_city = City.find(:all, :order=>“id”)

in models/register.rb , we need to put validation so that ‘Select a
City’ will not stored into database of register.

class Penerbangan < ActiveRecord::Base

belongs_to :citys

validates_presence_of :city_name

end

Thank you for your helping Bro. [Double Thumbs For You]

I got the answer from my trial and Error (^-^)

The answer is :

<%= select_tag ‘register[city]’, “<option
value=”#{@register.city}">#{@register.city}" +
options_from_collection_for_select(@find_city, “city”,
“city”)) %>

Funny Right, I ask and I answer self
(^-^)

This case is closed

I’ll be right back with new question.

Thanks to Fred