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 ... <p><label for="register_city_name">Tujuan</label><br/> <%= select_tag(:city, "<option>-Select a city</option>" + options_from_collection_for_select(@find_city, :city, :city)) %> </p> ... ------------------------------------------------------------------- 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: <p><label for="register_city_name">Tujuan</label><br/> 17: 18: <%= select_tag(:city, "<option>-Select a city</option>" + options_from_collection_for_select(@find_city, :city, :city)) %> 19: 20: </p> 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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
> ------------------------------------------------------------------- > 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 >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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Frederick Cheung 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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 8 Oct 2007, at 02:26, Fransiscus Xaverius wrote:> > 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 blankWe can''t guess what''s in your create action. Fred --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Frederick Cheung wrote:> On 8 Oct 2007, at 02:26, Fransiscus Xaverius 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 ... -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 ... -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Create Script is above and _form.rhtml script is : ... <p><label for="register_city">City</label><br/> <%= select_tag (:city, "<option>-Select a city</option>" + options_from_collection_for_select(@find_city, "id", "city")) %> </p> ... Please let me know what should I do? so the selected city can be saved without blank information. Thanks. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 8 Oct 2007, at 02:46, Fransiscus Xaverius wrote:> > Sorry again, Here is the corrections of action Create script. > > def create > @register = Register.new(params[:register])> <%= select_tag (:city, "<option>-Select a city</option>" + > 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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
I made script _from.rhtml like this <%= select_tag ''register[city]'', "<option value=\"0\">Select a city</option>" + 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] -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
I got the answer from my trial and Error (^-^) The answer is : <%= select_tag ''register[city]'', "<option value=\"#{@register.city}\">#{@register.city}</option>" + 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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---