You need to use the @record object. @record is: in update, the current record being updated, and in create, it's a new object, so in both cases it exist. I think you should put something like :selected => @record.state_id, or use the object itself, but that depends on your way you declare your "select" method (and which method). Check rails api, or the original AS partials.
On Feb 10, 9:52 pm, Rafael <[email protected]> wrote: > I already posted, its the post from Tue, 10 Feb 2009 05:10:35. > > On 10 fev, 17:02, Kenny Ortmann <[email protected]> wrote: > > > can you show the contents of the overrides please? > > > On Tue, Feb 10, 2009 at 10:07 AM, Rafael Maus <[email protected]> wrote: > > > Thanks for the reply, > > > > I have 2 partials... > > > > app/views/companies/_city_form_column.html.erb > > > app/views/companies/_state_form_column.html.erb > > > > On create, when i choose a state it updates the city <select>, then i can > > > choose a city and save the record. > > > But when i want to edit a record the choosen state/city for that record is > > > not selected on the <select>. > > > Is it possible to have these fields overriden for create only? > > > If you did not understand what im trying to explain, please reply. > > > > 2009/2/10 Kenny Ortmann <[email protected]> > > > > You need to look at the html of the select after it is updated. > > >> make sure that the name of the select is the same before and after your > > >> dynamic update. > > > >> You said that on edit it doesn't set the proper values on the select. > > >> which select are you talking about, the state select, or are you talking > > >> about the cities select after you choose a state? > > > >> what is the name of the partial that you created? > > > >> On Tue, Feb 10, 2009 at 7:10 AM, Rafael <[email protected]> wrote: > > > >>> Hello, > > > >>> I found a way to make it work =). > > >>> The partial stays like this: > > > >>> <dt><label for="record_state_">Estado</label></dt> > > >>> <dd> > > >>> <%= collection_select(nil, nil, State.find(:all), :id, :name, > > >>> {:prompt => "Selecione..."}, > > >>> {:onchange => remote_function(:url => {:action => > > >>> "on_state_id_change"}, > > >>> :with => "'state_id='+value"), > > >>> :class => "state-input", > > >>> :id => "record_state_", > > >>> :name => "record[state][id]"}) > > >>> %> > > >>> </dd> > > > >>> Then the method on the controller: > > >>> def on_state_id_change > > >>> render :update do |page| > > >>> cities = State.find(params[:state_id]).cities > > >>> page.replace 'select_city', > > >>> "<div id='select_city'><dt><label > > >>> for='record_city_'>Cidade</label></dt><dd>" + > > >>> collection_select(nil, nil, cities, :id, :name, > > >>> {:prompt => "Selecione..."}, > > >>> {:class => "city-input", > > >>> :id => "record_city_", > > >>> :name => "record[city][id]"}) + "</dd></div>" > > >>> end > > > >>> Works for create/show, but when i edit a record it doesn't set the > > >>> proper values on the select, > > >>> my guess is that it happens because of the partials, can anyone help > > >>> me here? > > >>> What should be done? > > > >>> Thanks, > > > >>> Rafael > > > >>> On 5 fev, 17:17, Rafael <[email protected]> wrote: > > >>> > I'm trying to do this based on this post > > > >>>http://pullmonkey.com/2008/3/30/dynamic-select-boxes-ruby-on-rails > > >>> > I created 2 partials > > > >>> > _state_form_column.rhtml > > > >>> > <label>Estado</label> > > >>> > <%= collection_select(nil, :state_id, states, :id, :name, > > >>> > {:prompt => "Selecione um Estado"}, > > >>> > {:onchange => "#{remote_function(:url => > > >>> > {:action => "update_cities"}, > > >>> > :with => > > >>> > "'state_id='+value")}"}) %> > > > >>> > and > > > >>> > _city_form_column.rhtml > > >>> > <label>Cidade</label> > > >>> > <%= collection_select(nil, :city_id, cities, :id, :name, > > >>> > {:prompt => "Selecione uma cidade"}) %> > > > >>> > And on the controller i have a method: > > >>> > def update_cities > > >>> > states = State.find(params[:state_id]) > > >>> > cities = states.cities > > > >>> > render :update do |page| > > >>> > page.replace_html 'cities', :partial => > > >>> > 'state_form_column', :object => cities > > >>> > end > > >>> > end > > > >>> > Webrick boots fine, but when i try to insert a new record i get this > > >>> > error on development.log > > > >>> > ActionView::TemplateError (undefined local variable or method `states' > > >>> > for #<ActionView::Base:0xb6e2dd9c>) on line #2 of app/views/ > > >>> > neighborhoods/_state_form_column.rhtml: > > >>> > 1: <label>Estado</label> > > >>> > 2: <%= collection_select(nil, :state_id, states, :id, :name, > > >>> > 3: {:prompt => "Selecione um Estado"}, > > >>> > 4: {:onchange => "#{remote_function(:url => > > >>> > {:action => "update_cities"}, > > >>> > 5: :with => > > >>> > "'state_id='+value")}"}) %> > > > >>> > Looks pretty obvious, but i don't know how to fix this. > > >>> > Am i on the right way? > > > >>> > Rafael > > > >>> > On 5 fev, 09:51, Rafael <[email protected]> wrote: > > > >>> > > Hello, > > > >>> > > Thanks for the quick replies. I think in my case > > >>> > > AJAX will be more suitable, so Wes if you can > > >>> > > share that code i'll appreciate. > > > >>> > > Rafael > > > >>> > > On 3 fev, 23:56, Wes Gamble <[email protected]> wrote: > > > >>> > > > I have done something like this using AJAX calls to generate the > > >>> > > > dependent select's options. > > > >>> > > > If the Railscast doesn't help, let me know and I can send you some > > >>> code. > > > >>> > > > Wes > > > >>> > > > Rafael wrote: > > >>> > > > > Hi, > > > >>> > > > > How can i make a dynamic select? Suppose i have 3 models > > >>> > > > > Country, > > >>> > > > > State and City. > > >>> > > > > City belongs_to State and State belongs_to Country. > > >>> > > > > Then i have a model that belongs_to the 3 models above. > > >>> > > > > What i want to do is when i select a country it updates the > > >>> > > > > State > > >>> > > > > select with states from that country, > > >>> > > > > then i select a State and it updates the city select with cities > > >>> from > > >>> > > > > that State. > > >>> > > > > Any ideas? examples? > > > >>> > > > > Thanks, > > --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "ActiveScaffold : Ruby on Rails plugin" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/activescaffold?hl=en -~----------~----~----~----~------~----~------~--~---
