hi friends, I am new to ruby and i need a help from any one of u. I have got four tables and all i have to get the thing is i need to take the Id from one table and pass it in to the another table. Similarly in the descending order, one to two, to three, to four. ie , first tables primary key is the second one''s foreign key. second table,first table primary key is the third tables primary key and so on.... Now i have to get that functionality in the manner like, in one form i need to get four labels where in i can provide and store in to the database.... and on submitting, it will lead us to the listing page, where in i had maintained drop down list. But the thing is i am getting all the contents. I have to get only those first table id contents from four tables , through foreign reference. In the database also it is not passing that id values in to the child tables. def new @country = Country.new @state = State.new @city = City.new @category = Category.new end def create @country=Country.new(params[:country]) @state=State.new(params[:state]) #@country.states.create(params[:id]) @city=City.new(params[:city]) #@country.cities.create(params[:id]) #@state.cities.create(params[:id]) @category=Category.new(params[:category]) #@country.categories.create(params[:id]) #@state.categories.create(params[:id]) #@city.categories.create(params[:id]) @country.save @state.save @city.save @category.save redirect_to :action =>''list'' end def list end and the models are class Country < ActiveRecord::Base has_many :states has_many :cities end class State < ActiveRecord::Base belongs_to :country has_many :cities end class City < ActiveRecord::Base belongs_to :state belongs_to :country end class Category < ActiveRecord::Base belongs_to :state belongs_to :country belongs_to : city end and the view files are: list.rhtml <align> <form name="combo"> <h4>Select Country<br><%= select(''name'', ''id'', Country.find_all.collect {|country| country.name}) %><br> <h4>Select State<br><%= select(''name'', ''id'', State.find_all.collect {|state| state.name}) %><br> <h4>Select City<br><%= select(''name'', ''id'', City.find_all.collect {|city| city.name}) %><br> <h4>Select Category<br><%= select(''name'', ''id'', Category.find_all.collect {|category| category.name}) %><br> new.rhtml <%= start_form_tag :action => ''create'' %> <h4>Country<br><%= text_field ''country'', ''name'' %></br> <h4>State<br><%= text_field ''state'', ''name'' %></br> <h4>City<br><%= text_field ''city'', ''name'' %></br> <h4>Category<br><%= text_field ''category'', ''name'' %></br> <%= submit_tag "Add" %> <%= end_form_tag %> please have a look in to my coding and help me out with some suggestions or modifications Expecting a help from rubians thank u -- 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 am not sure I follow, but: There is no :id being passed to the controller from the form. The create method could be: def create @country=Country.find_or_create_by name(params[:country]) @state=State.find_or_create_by_name(params[:state]) @state.country_id = @country.id @state.save @city=City.create(params[:city]) @city.country_id = @country.id @city.state_id = @state.id @city.save @category=Category.find_or_create_by_name(params[:category]) @category.country_id = @country.id @category.state_id = @state.id @categort.city_id = @city.id @category.save redirect_to :action =>''list'' end Anyone have another way? --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
askegg wrote:> I am not sure I follow, but: > > There is no :id being passed to the controller from the form. > > The create method could be: > > def create > @country=Country.find_or_create_by name(params[:country]) > > @state=State.find_or_create_by_name(params[:state]) > @state.country_id = @country.id > @state.save > > @city=City.create(params[:city]) > @city.country_id = @country.id > @city.state_id = @state.id > @city.save > > @category=Category.find_or_create_by_name(params[:category]) > @category.country_id = @country.id > @category.state_id = @state.id > @categort.city_id = @city.id > @category.save > > redirect_to :action =>''list'' > > end > > > Anyone have another way?hi askegg we are really thankful for givimg this code. but there was a small problem we are getting the things like ---!map: HashWithIndifferentAccess name:----- for the country ,state and category fields only. we are getting the data saved with ids and displayed in thew dropdown box with the value in that name: ---- area -- 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 -~----------~----~----~----~------~----~------~--~---
Opps. Move the SQL logic out of your view - it''s bad practice. Much more readable if its in the controller, or even better pushed down to the model. Try this in the list view: <%= form_tag :action => "update" do %> <h4>Select Country<br> <% @countries.each do |country| %> <%= select(''name'', country) %><br> <% end %> <h4>Select State<br> <% @states.each do |state| %> <%= select(''name'', state) %> <% end %> <h4>Select City<br> <% @cities.each do |city| %> <%= select(''name'', city) %><br> <% end %> <h4>Select Category<br> <% @categories.each do |category| %> <%= select(''name'', category) %><br> <% end %> <%= submit_tag %> <%= end %> and in the controller: def list @countries = Country.find(:all) @states = State.find(:all) @cities = City.find(:all) @categories = Category.find(:all) end * The list view code may not work and it''s ugly, but you get the idea. Can anyone tell us if you can do something like: <%= select(''name'', @categories) %> ?? --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Chandoo Reddy wrote:> askegg wrote: >> I am not sure I follow, but: >> >> There is no :id being passed to the controller from the form. >> >> The create method could be: >> >> def create >> @country=Country.find_or_create_by name(params[:country]) >> >> @state=State.find_or_create_by_name(params[:state]) >> @state.country_id = @country.id >> @state.save >> >> @city=City.create(params[:city]) >> @city.country_id = @country.id >> @city.state_id = @state.id >> @city.save >> >> @category=Category.find_or_create_by_name(params[:category]) >> @category.country_id = @country.id >> @category.state_id = @state.id >> @categort.city_id = @city.id >> @category.save >> >> redirect_to :action =>''list'' >> >> end >> >> >> Anyone have another way? >+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ > Thank you daa..The code u sent worked after some small small modifications. I think you may had got the view about what we asked yesterday. now i will say it clearly yaaaaaar Select Country ------------------- Add Country ------------------- Select State ------------------- Add State ------------------- Select City ------------------- Add City ------------------- Select Category ------------------- Add Category ------------------- go Add New -------------------------------------------------------------------------------- Thats our view page, where in we get the drop down for the selection -------------------------------------------------------------------------------- The Add New in the bottom, takes us to another page, where in we have four text boxes for the four entries. The Add country also lead us to the same page again and working fine. But when we click the link Add State, it should lead us to the page where in i have only text boxes for adding state ,city, category. And so on for the next two links ie,add city has city, category and add category has just category. Now, we are getting the error like "Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id", when we click the link Add states/cities/category. Thats because , we are not passing the country id in their states.rhtml ----------------------------------------------------------------- <%= start_form_tag :action => ''create'' %> <h4>State<br><%= text_field ''state'', ''name'' %></br> <h4>City<br><%= text_field ''city'', ''name'' %></br> <h4>Category<br><%= text_field ''category'', ''name'' %></br> <%= submit_tag "Add" %> <%= end_form_tag %> ------------------------------------------------------------------ cities.rhtml ------------------------------------------------------------------ <%= start_form_tag :action => ''create'' %> <h4>City<br><%= text_field ''city'', ''name'' %></br> <h4>Category<br><%= text_field ''category'', ''name'' %></br> <%= submit_tag "Add" %> <%= end_form_tag %> ------------------------------------------------------------------ Category.rhtml ------------------------------------------------------------------ <%= start_form_tag :action => ''create'' %> <h4>Category<br><%= text_field ''category'', ''name'' %></br> <%= submit_tag "Add" %> <%= end_form_tag %> ------------------------------------------------------------------ and in the only controller, we are using some thing like this --------------------------------------------------------- def new @country = Country.new @state = State.new @city = City.new @category = Category.new end def create @country=Country.create(params[:country]) @state=State.create(params[:state]) @state.country_id = @country.id @state.save @city=City.create(params[:city]) @city.country_id = @country.id @city.state_id = @state.id @city.save @category=Category.create(params[:category]) @category.country_id = @country.id @category.state_id = @state.id @category.city_id = @city.id @category.save redirect_to :action =>''list'' #@country=Country.new(params[:country]) #@state=State.new(params[:state]) #@country.states.create(params[:id]) #@city=City.new(params[:city]) #@country.cities.create(params[:id]) #@state.cities.create(params[:id]) #@category=Category.new(params[:category]) #@country.categories.create(params[:id]) #@state.categories.create(params[:id]) #@city.categories.create(params[:id]) #@country.save #@state.save #@city.save #@category.save #redirect_to :action =>''list'' end def state @state=State.create(params[:state]) @state.country_id = @country.id @state.save redirect_to :action =>''list'' end def city @city=City.create(params[:city]) @city.country_id = @country.id @city.state_id = @state.id @city.save redirect_to :action =>''list'' end def category @category=Category.create(params[:category]) @category.country_id = @country.id @category.state_id = @state.id @category.city_id = @city.id @category.save redirect_to :action =>''list'' end def list end ------------------------------------------------------------- Actually we need some thing likethe way, when i select one country, the states of that country must be displayed in the states drop down list and when i select a state, the cities must be be displayed in its respective drop down list of that concerned state and so for the categoriy drop down too Ie, Just like , i should select a country and i should get that country''s states in the state dropdown list. I should get that state''s city drop down list when i select city, the concerned city''s categories in the category drop down list. And when i Have my country not being present in the country list, i should add it through the Add Country link and so on for state,city and category. If the country is present and if the state is not there in the list, i should add it through Add State link with the Id of the country selected in the Country drop down list and so on... Thats the total view we have to provide my dear. ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -- 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 -~----------~----~----~----~------~----~------~--~---
OK, so you have the same basic problem iterated a few times. I began to type a few answers to this problem, but kept coming into trouble. Example 1 - Creating a state: def state @state=State.create(params[:state]) @state.country_id = @country.id @state.save redirect_to :action =>''list'' end Should be: def state @state=State.create(params[:state]) @state.country_id = Country.find_by_name(params[:country]).id @state.save redirect_to :action =>''list'' end BUT when the final form is submitted it will create YET ANOTHER state in the table.... OK, how about we create some dynamic drop down boxes that change content based on previous selections? Doable, but a little messy and probably a bit too much effort at this point. What about this? In your main form have a drop down list *and* a text field. The user can either select the country/state/city/category from the list, and if they do not find it can type one in. Then check for the values submitted and act appropriately: no drop down selected, no text = throw an error drop down, no text = use drop down value drop down + text = text overrides dropdown(?? or vice versa?) <= User must be informed prior no drop down + text = Use text. Along the right track? On Dec 15, 4:48 pm, Chandoo Reddy <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Chandoo Reddy wrote: > > askegg wrote: > >> I am not sure I follow, but: > > >> There is no :id being passed to the controller from the form. > > >> The create method could be: > > >> def create > >> @country=Country.find_or_create_by name(params[:country]) > > >> @state=State.find_or_create_by_name(params[:state]) > >> @state.country_id = @country.id > >> @state.save > > >> @city=City.create(params[:city]) > >> @city.country_id = @country.id > >> @city.state_id = @state.id > >> @city.save > > >> @category=Category.find_or_create_by_name(params[:category]) > >> @category.country_id = @country.id > >> @category.state_id = @state.id > >> @categort.city_id = @city.id > >> @category.save > > >> redirect_to :action =>''list'' > > >> end > > >> Anyone have another way? > >+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ > > Thank you daa..The code u sent worked after some small small modifications. > I think you may had got the view about what we asked yesterday. > now i will say it clearly yaaaaaar > > Select Country > ------------------- Add Country > ------------------- > Select State > ------------------- Add State > ------------------- > Select City > ------------------- Add City > ------------------- > Select Category > ------------------- Add Category > ------------------- > go > > Add New > > -------------------------------------------------------------------------------- > Thats our view page, where in we get the drop down for the selection > -------------------------------------------------------------------------------- > > The Add New in the bottom, takes us to another page, where in we have > four text boxes for the four entries. > The Add country also lead us to the same page again and working fine. > > But when we click the link Add State, it should lead us to the page > where in i have only text boxes for adding state ,city, category. And so > on for the next two links ie,add city has city, category and add > category has just category. > > Now, we are getting the error like "Called id for nil, which would > mistakenly be 4 -- if you really wanted the id of nil, use object_id", > when we click the link Add states/cities/category. Thats because , we > are not passing the country id in their > > states.rhtml > ----------------------------------------------------------------- > <%= start_form_tag :action => ''create'' %> > <h4>State<br><%= text_field ''state'', ''name'' %></br> > <h4>City<br><%= text_field ''city'', ''name'' %></br> > <h4>Category<br><%= text_field ''category'', ''name'' %></br> > <%= submit_tag "Add" %> > <%= end_form_tag %> > ------------------------------------------------------------------ > > cities.rhtml > ------------------------------------------------------------------ > <%= start_form_tag :action => ''create'' %> > <h4>City<br><%= text_field ''city'', ''name'' %></br> > <h4>Category<br><%= text_field ''category'', ''name'' %></br> > <%= submit_tag "Add" %> > <%= end_form_tag %> > ------------------------------------------------------------------ > > Category.rhtml > ------------------------------------------------------------------ > <%= start_form_tag :action => ''create'' %> > <h4>Category<br><%= text_field ''category'', ''name'' %></br> > <%= submit_tag "Add" %> > <%= end_form_tag %> > ------------------------------------------------------------------ > > and in the only controller, we are using some thing like this > --------------------------------------------------------- > def new > @country = Country.new > @state = State.new > @city = City.new > @category = Category.new > end > def create > > @country=Country.create(params[:country]) > > @state=State.create(params[:state]) > @state.country_id = @country.id > @state.save > > @city=City.create(params[:city]) > @city.country_id = @country.id > @city.state_id = @state.id > @city.save > > @category=Category.create(params[:category]) > @category.country_id = @country.id > @category.state_id = @state.id > @category.city_id = @city.id > @category.save > > redirect_to :action =>''list'' > > #@country=Country.new(params[:country]) > #@state=State.new(params[:state]) > #...@country.states.create(params[:id]) > #@city=City.new(params[:city]) > #...@country.cities.create(params[:id]) > #...@state.cities.create(params[:id]) > #@category=Category.new(params[:category]) > #...@country.categories.create(params[:id]) > #...@state.categories.create(params[:id]) > #...@city.categories.create(params[:id]) > #...@country.save > #...@state.save > #...@city.save > #...@category.save > #redirect_to :action =>''list'' > end > def state > @state=State.create(params[:state]) > @state.country_id = @country.id > @state.save > redirect_to :action =>''list'' > end > def city > @city=City.create(params[:city]) > @city.country_id = @country.id > @city.state_id = @state.id > @city.save > redirect_to :action =>''list'' > end > def category > @category=Category.create(params[:category]) > @category.country_id = @country.id > @category.state_id = @state.id > @category.city_id = @city.id > @category.save > redirect_to :action =>''list'' > end > def list > end > ------------------------------------------------------------- > > Actually we need some thing likethe way, when i select one country, the > states of that country must be displayed in the states drop down list > and when i select a state, the cities must be be displayed in its > respective drop down list of that concerned state and so for the > categoriy drop down too > > Ie, Just like , i should select a country and i should get that > country''s states in the state dropdown list. I should get that state''s > city drop down list when i select city, the concerned city''s categories > in the category drop down list. > > And when i Have my country not being present in the country list, i > should add it through the Add Country link and so on for state,city and > category. > > If the country is present and if the state is not there in the list, i > should add it through Add State link with the Id of the country selected > in the Country drop down list and so on... > > Thats the total view we have to provide my dear. > > ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ > > -- > Posted viahttp://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 -~----------~----~----~----~------~----~------~--~---