I''m trying to figure out how to update multiple records in one method from a hash sent by a view, and i''m stuck. Here''s what i''m sending: <% form_tag :controller => ''todoitems'', :action => ''toggle_todo'', :id => @id do -%> <% if @todoitems then @todoitems.each do |t| -%> <div class="rev_todo_box"> <%= check_box("to_toggle", t.id, {:checked => t.done}, TRUE, FALSE) %> <%= t.description %> </div> <% end -%> <% end -%> <%= submit_tag ''Update'' %> <% end %> This is what gets sent: Parameters: {"commit"=>"Update", "to_toggle"=>{"6"=>"true", "7"=>"false", "8"=>"false", "9"=>"true"}, "action"=>"toggle_todo", "id"=>"8", "controller"=>"todoitems"} And this is the controller def that *should* modify the records for me, but it doesn''t do anything. I commented it out so people can see what I''m trying to do: def toggle_todo #takes the hash out of params and assigns it to @toggle @toggle = params[:to_toggle] if @toggle #iterate over the hash @toggle.each do |id, value| #find the record @todoitem = Todoitem.find_by_id(id) #change the record''s "done" column to the right value @todoitem.done = value end end #redirect redirect_to :controller => ''showproject'', :action => ''show'', :id => params[:id] end Each todoitem has a "done" column that''s a boolean to show if it''s done or not. Anybody see what I''m missing? 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 -~----------~----~----~----~------~----~------~--~---
sof.ltd.plat-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-Jun-07 01:54 UTC
Re: Updating records from a hash
add this in ur loop: @todoitem.done = value @todoitem.save You forgot to save the value that you are changing. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Whoops. Didn''t know I had to do that, actually. Now I do! 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 -~----------~----~----~----~------~----~------~--~---
One thing but, you might want to wrap that in a transaction, if one of your loops fails, you''ll end up in a strange database state. Cam On Jun 7, 12:28 pm, Sean Colquhoun <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Whoops. Didn''t know I had to do that, actually. > > Now I do! > > Thanks! > > -- > 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 -~----------~----~----~----~------~----~------~--~---
Sean Colquhoun wrote:> I''m trying to figure out how to update multiple records in one method > from a hash sent by a view, and i''m stuck....snip...> > def toggle_todo > #takes the hash out of params and assigns it to @toggle > @toggle = params[:to_toggle] > if @toggle > #iterate over the hash > @toggle.each do |id, value| > #find the record > @todoitem = Todoitem.find_by_id(id) > #change the record''s "done" column to the right value > @todoitem.done = value > end > end > #redirect > redirect_to :controller => ''showproject'', :action => ''show'', :id => > params[:id] > endWhy not skip iterating over the hash and just use the ActiveRecord::Base#update method? Your code would look a lot neater: def toggle_todo @toggle = params[:to_toggle] if @toggle Todoitem.update(@toggle.keys, @toggle.values) end redirect_to :controller => ''showproject'', :action => ''show'', :id => params[:id] end Cheers, Josh -- 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 -~----------~----~----~----~------~----~------~--~---
Josh, I wrote into the http://railsforum.com yesterday and heard no reply on a topic related to this. When Rails programmers'' deploy a ''naked'' application to the web before it has any user activity, it is VERY unattractive to search engines. I thought it would be wise to embed one''s ''keywords'' into a few text fields while generating ''placebo'' records upon initial deployment. I''d like to write a simple record generation routine that reads from a KEYWORD hash and at various intervals substitutes these keywords with a variable. Could/would you point me in the right direction of getting started with this. Thank you, David On Jun 6, 11:19 pm, Joshua Ballanco <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Sean Colquhoun wrote: > > I''m trying to figure out how to update multiple records in one method > > from a hash sent by a view, and i''m stuck. > > ...snip... > > > > > > > > > def toggle_todo > > #takes the hash out of params and assigns it to @toggle > > @toggle = params[:to_toggle] > > if @toggle > > #iterate over the hash > > @toggle.each do |id, value| > > #find the record > > @todoitem = Todoitem.find_by_id(id) > > #change the record''s "done" column to the right value > > @todoitem.done = value > > end > > end > > #redirect > > redirect_to :controller => ''showproject'', :action => ''show'', :id => > > params[:id] > > end > > Why not skip iterating over the hash and just use the > ActiveRecord::Base#update method? Your code would look a lot neater: > > def toggle_todo > @toggle = params[:to_toggle] > if @toggle > Todoitem.update(@toggle.keys, @toggle.values) > end > redirect_to :controller => ''showproject'', :action => ''show'', :id => > params[:id] > end > > Cheers, > Josh > > -- > Posted viahttp://www.ruby-forum.com/.- Hide quoted text - > > - Show quoted text ---~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
BraveDave wrote:> Josh, > I wrote into the http://railsforum.com yesterday and heard no reply on > a topic related to this. > When Rails programmers'' deploy a ''naked'' application to the web before > it has any user activity, it is VERY unattractive to search engines. I > thought it would be wise to embed one''s ''keywords'' into a few text > fields while generating ''placebo'' records upon initial deployment. > I''d like to write a simple record generation routine that reads from a > KEYWORD hash and at various intervals substitutes these keywords with > a variable. > Could/would you point me in the right direction of getting started > with this. > Thank you, > DavidHmm...interesting. Well, I can definitely see why something like this might be useful. I guess I''m just not sure exactly how you''re looking to implement this. Do you want temporary records that will eventually be replaced as your web app grows? I think the best way to import test/temporary records is via a migration. The Pragmatic Programmers do this in _Agile Web Development with Rails_ (which, if you don''t have already, is an excellent resource). You can find the example code at http://www.pragmaticprogrammer.com/titles/rails/code.html. You could then add a "temp_item" column to your model, and every time a real record comes in, remove one of the temp items until they''re all gone. Not sure if this is what you''re looking for... -Josh -- 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 -~----------~----~----~----~------~----~------~--~---
Josh, I''ve thorough searched the web for any Rails code relating to this and find none. To me, it''s just another example of how Rails programmers'' are decoupled from the world of business. I''d expire records based on time. These ''keyword'' rich records would be groomed out after reaching a certain age. If I could see a chunck of code that read an input record (as you describe in AWD, it would then run a ''block'' that would run through a ''hash'' and replaced words like REPLACEME with the next ''keyword'' in the hash. I''m making great strides in developing the ''mother'' of all web applications, but am religated to find and copy code as I don''t have the intellect/experience to create my own. Obviously, I''m the business side of this, too. I am happy to share with the group, whatever I come up with. David On Jun 7, 10:59 am, Joshua Ballanco <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> BraveDave wrote: > > Josh, > > I wrote into thehttp://railsforum.comyesterday and heard no reply on > > a topic related to this. > > When Rails programmers'' deploy a ''naked'' application to the web before > > it has any user activity, it is VERY unattractive to search engines. I > > thought it would be wise to embed one''s ''keywords'' into a few text > > fields while generating ''placebo'' records upon initial deployment. > > I''d like to write a simple record generation routine that reads from a > > KEYWORD hash and at various intervals substitutes these keywords with > > a variable. > > Could/would you point me in the right direction of getting started > > with this. > > Thank you, > > David > > Hmm...interesting. Well, I can definitely see why something like this > might be useful. I guess I''m just not sure exactly how you''re looking to > implement this. Do you want temporary records that will eventually be > replaced as your web app grows? I think the best way to import > test/temporary records is via a migration. The Pragmatic Programmers do > this in _Agile Web Development with Rails_ (which, if you don''t have > already, is an excellent resource). You can find the example code athttp://www.pragmaticprogrammer.com/titles/rails/code.html. You could > then add a "temp_item" column to your model, and every time a real > record comes in, remove one of the temp items until they''re all gone. > Not sure if this is what you''re looking for... > > -Josh > > -- > 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 -~----------~----~----~----~------~----~------~--~---