Hello, I am new to Rails and Ruby so this question is probably very basic. I checked the Pickaxe but couldn''t really nail down what I was looking for. According to a stacktrace, I have the following params: Parameters: {"delete"=>{"4"=>"Delete"}} How can I extract that number ''4'' in my controller for use? What is the correct syntax? I tried something like this to try to print the thing to the console: puts params[:delete.keys[0]] But that didn''t work... Can someone point me in the right direction? Thanks, Hunter
On Sun, 2005-07-17 at 15:31 -0700, Hunter Hillegas wrote:> Hello, > > I am new to Rails and Ruby so this question is probably very basic. > > I checked the Pickaxe but couldn''t really nail down what I was looking for. > > According to a stacktrace, I have the following params: > > Parameters: {"delete"=>{"4"=>"Delete"}}Hunter, @params[:delete] will give you access to the hash {4 => delete}, and @params[:delete]["4"] will extract "delete". In either case, I''m pretty certain that will lead you to uncover deeper problems with your code. How did you write the link_to or form that generated this parameter?> > How can I extract that number ''4'' in my controller for use? What is the > correct syntax? > > I tried something like this to try to print the thing to the console: > > puts params[:delete.keys[0]] > > But that didn''t work... > > Can someone point me in the right direction? > > Thanks, > Hunter > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails
I''m actually looking to get the number "4" (key), not "delete" (value). Any thoughts there? This was implemented as a suggestion as to how I could deal with multiple submit buttons in a single form. To recap, I have an unordered-list of rows in a form. Each row must have a button that has a value of ''Delete'' and the button is supposed to delete the row. Right now the code looks like this: <%= submit_tag "Delete", options = {:class => "del", :name => "delete[" + user_account.id.to_s + "]"} %> The idea was I could get this array and it would let me have all my buttons use the same value. This is a port of a WebObjects application and having the buttons all have the same visible label is a requirement. If there is a better way to accomplish the larger goal, I''d love to hear alternatives. Thanks for responding. Cheers, Hunter> From: Lord Khaos <lordkhaos-Wuw85uim5zDR7s880joybQ@public.gmane.org> > Reply-To: <lordkhaos-Wuw85uim5zDR7s880joybQ@public.gmane.org>, <rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org> > Date: Sun, 17 Jul 2005 18:48:34 -0400 > To: <rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org> > Subject: Re: [Rails] Getting the Key From a Hash in Params > > On Sun, 2005-07-17 at 15:31 -0700, Hunter Hillegas wrote: >> Hello, >> >> I am new to Rails and Ruby so this question is probably very basic. >> >> I checked the Pickaxe but couldn''t really nail down what I was looking for. >> >> According to a stacktrace, I have the following params: >> >> Parameters: {"delete"=>{"4"=>"Delete"}} > Hunter, > @params[:delete] will give you access to the hash {4 => delete}, and > @params[:delete]["4"] will extract "delete". In either case, I''m pretty > certain that will lead you to uncover deeper problems with your code. > How did you write the link_to or form that generated this parameter? > >> >> How can I extract that number ''4'' in my controller for use? What is the >> correct syntax? >> >> I tried something like this to try to print the thing to the console: >> >> puts params[:delete.keys[0]] >> >> But that didn''t work... >> >> Can someone point me in the right direction? >> >> Thanks, >> Hunter >> >> >> _______________________________________________ >> Rails mailing list >> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >> http://lists.rubyonrails.org/mailman/listinfo/rails > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails
On Jul 18, 2005, at 12:52 AM, Hunter Hillegas wrote:> I''m actually looking to get the number "4" (key), not > "delete" (value). Any > thoughts there?You can use params["delete"].keys to get an array of all the keys in the {"4" => "delete"} hash. So if you''re certain that that hash will always contain just one key, you could use params["delete"].keys[0] to get "4". - Marten
On 7/17/05, Hunter Hillegas <lists-HAWAbpnI61OZ1JSuHaJ1sQC/G2K4zDHf@public.gmane.org> wrote:> Hello, > > I am new to Rails and Ruby so this question is probably very basic. > > I checked the Pickaxe but couldn''t really nail down what I was looking for. > > According to a stacktrace, I have the following params: > > Parameters: {"delete"=>{"4"=>"Delete"}} > > How can I extract that number ''4'' in my controller for use? What is the > correct syntax? > > I tried something like this to try to print the thing to the console: > > puts params[:delete.keys[0]] > > But that didn''t work... > > Can someone point me in the right direction?Martin gave you the answer already, but I think you should probably restructure your code somehow. I assume you have a form that the user can use to delete things. I''d rather have a delete array that contained an array of all the element ids to delete. So, {"delete" => ["4", "0"]}, or maybe just {"delete => "4"} if there''s just one of them.
Joe Van Dyk wrote:>On 7/17/05, Hunter Hillegas <lists-HAWAbpnI61OZ1JSuHaJ1sQC/G2K4zDHf@public.gmane.org> wrote: > > >>Hello, >> >>I am new to Rails and Ruby so this question is probably very basic. >> >>I checked the Pickaxe but couldn''t really nail down what I was looking for. >> >>According to a stacktrace, I have the following params: >> >>Parameters: {"delete"=>{"4"=>"Delete"}} >> >>How can I extract that number ''4'' in my controller for use? What is the >>correct syntax? >> >>I tried something like this to try to print the thing to the console: >> >>puts params[:delete.keys[0]] >> >>But that didn''t work... >> >>Can someone point me in the right direction? >> >> > >Martin gave you the answer already, but I think you should probably >restructure your code somehow. I assume you have a form that the user >can use to delete things. I''d rather have a delete array that >contained an array of all the element ids to delete. > >So, {"delete" => ["4", "0"]}, or maybe just {"delete => "4"} if >there''s just one of them. >______________________________________________ > >Can someone point me to how you can do this? As far as I can tell with rails conversions of parameters, everything gets made into a hash... _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
On 7/18/05, Joe Van Dyk <joevandyk-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 7/17/05, Hunter Hillegas <lists-HAWAbpnI61OZ1JSuHaJ1sQC/G2K4zDHf@public.gmane.org> wrote: > > Hello, > > > > I am new to Rails and Ruby so this question is probably very basic. > > > > I checked the Pickaxe but couldn''t really nail down what I was looking for. > > > > According to a stacktrace, I have the following params: > > > > Parameters: {"delete"=>{"4"=>"Delete"}} > > > > How can I extract that number ''4'' in my controller for use? What is the > > correct syntax? > > > > I tried something like this to try to print the thing to the console: > > > > puts params[:delete.keys[0]] > > > > But that didn''t work... > > > > Can someone point me in the right direction? > > Martin gave you the answer already, but I think you should probably > restructure your code somehow. I assume you have a form that the user > can use to delete things. I''d rather have a delete array that > contained an array of all the element ids to delete. > > So, {"delete" => ["4", "0"]}, or maybe just {"delete => "4"} if > there''s just one of them.I''m probably not telling anyone anything new, but here goes ... You can also use a <button type="submit"> tag instead of an <input type="submit"> tag. Rails doesn''t offer a helper to create such a tag---probably because IE doesn''t know what to do with <button>s, but if IE compatibility is not a requirement, you can refactor your code as <button type="submit" class="del" name="delete[<%= user_account.id %>]">Delete</button> Of course, if you were to really do this, you''d better create a helper called button_tag or submit_button_tag or something. But, IE always submits every <button type="submit"> wether it is pressed or not. This makes it unusable where IE compatibility is a must. BTW, the easier way to implement your requirements would be to simply use a link. You could even hide the link behind something like <input type="button" onclick="window.location=/user_account/delete/14" value="Delete" /> An actual submit button is only really required if you''re submitting anything beyond the submit button. If you want to delete just one random row, I think a link is much more logical. You UI might improve, though, if you use checkboxes to select which rows to delete and then use your submit button to delete the selected rows. - Rowan -- Morality is usually taught by the immoral.
> >An actual submit button is only really required if you''re submitting >anything beyond the submit button. If you want to delete just one >random row, I think a link is much more logical. You UI might improve, >though, if you use checkboxes to select which rows to delete and then >use your submit button to delete the selected rows. > > >But it''s generally regarded as bad practice to change state with a GET (hyperlink); instead it should be via POST (which usually requried clicking a button). Read the horror stories about search spiders indexing pages and following every link ... and deleting every row.
un-authenticated users should NEVER have the ability to delete stuff from your site. id blame that one on stupidity, not GET or the search spider. On 8/14/05, Steve Downey <sldowney-TVLZxgkOlNX2fBVCVOL8/A@public.gmane.org> wrote:> > > > >An actual submit button is only really required if you''re submitting > >anything beyond the submit button. If you want to delete just one > >random row, I think a link is much more logical. You UI might improve, > >though, if you use checkboxes to select which rows to delete and then > >use your submit button to delete the selected rows. > > > > > > > > But it''s generally regarded as bad practice to change state with a GET > (hyperlink); instead it should be via POST (which usually requried > clicking a button). > > Read the horror stories about search spiders indexing pages and > following every link ... and deleting every row. > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Zachery Hostens <zacheryph-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
Zachery Hostens wrote:>un-authenticated users should NEVER have the ability to delete stuff >from your site. id blame that one on stupidity, not GET or the search >spider. > >Did you hear the one about Google rolling back all the Wikis?
That seemed true until the world met the: Google Web Accelerator (http://webaccelerator.google.com/) which happily spiders every GET link as an authorized user. this is the hack from DHH to disable it - but you know there will be more copycat accelerators to follow: http://www.bigbold.com/snippets/posts/show/261 Zachery Hostens wrote:> un-authenticated users should NEVER have the ability to delete stuff > from your site. id blame that one on stupidity, not GET or the search > spider. > > > On 8/14/05, Steve Downey <sldowney-TVLZxgkOlNX2fBVCVOL8/A@public.gmane.org> wrote: > >>>An actual submit button is only really required if you''re submitting >>>anything beyond the submit button. If you want to delete just one >>>random row, I think a link is much more logical. You UI might improve, >>>though, if you use checkboxes to select which rows to delete and then >>>use your submit button to delete the selected rows. >>> >>> >>> >> >>But it''s generally regarded as bad practice to change state with a GET >>(hyperlink); instead it should be via POST (which usually requried >>clicking a button). >> >>Read the horror stories about search spiders indexing pages and >>following every link ... and deleting every row. >>_______________________________________________ >>Rails mailing list >>Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >>http://lists.rubyonrails.org/mailman/listinfo/rails >> > > >
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Sometimes spiders are authenticated users. Google created a web accelerator that followed GET links, figuring that they were safe, and all hell broke loose. http://radar.oreilly.com/archives/2005/05/ google_web_acce_1.html I make no claims here as to whether the problems were caused by Google''s over-zealousness or the web application designers failure to follow standards. I only caution that if Google did it, there are probably a lot more people with ideas on following hrefs, especially now that tools like Greasemonkey made browser reprogramming so easy. Be careful. George - -- George Hotelling GPG: 0x8175D485 ] http://george.hotelling.net ] _ _ _ ___ _ _ _/ On Aug 14, 2005, at 1:23 PM, Zachery Hostens wrote:> un-authenticated users should NEVER have the ability to delete stuff > from your site. id blame that one on stupidity, not GET or the search > spider.-----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.1 (Darwin) iD8DBQFDABVtgXVRXIF11IURAq0KAJ9QEVTmnB2v2z262gUkAFF85NtYSwCgnGx6 vIa94S2W0bqHWs7fGYlw4ak=E1+4 -----END PGP SIGNATURE-----