Hi, I''m clearly missing something very basic when it comes to form/field helpers. So far I''ve had no problem with the examples that map form fields to database models (albeit very simple). I''m making my first form that I want to map to a non-model object and I don''t understand how to map the @params hash I receive into my own object. My simple test form looks like: MySQL signup form<p> <%= form_tag :action=>"processmysql" %> <%= text_field ''mysqluser'', ''username'' %><br> <%= password_field ''mysqluser'', ''password'' %> <%= submit_tag "submit" %> <%= end_form_tag %> I defined a class like: class Mysqluser attr_accessor :username attr_accessor :password def initialize(u,p) @username=u @password=p end end and in my controller: def processmysql m = @params[:mysqluser] u= m[:username] p= m[:password] mysqluser = Mysqluser.new(u,p) render_text mysqluser.username + " " + mysqluser.password end Now this works perfectly fine but it seems overly complicated and non-railsey. Is there a simpler way to map the field values in the @params hash into a new mysqluser object? Thanks! Gary H.
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Gary, The following will allow you to expand the attributes in the Mysqluser model without having to touch the controller. The model will now assume that you''re passing it a hash with :username and :password set. class Mysqluser attr_accessor :username, :password def initialize(params) @username = params[:username] @password = params[:password] end end def processmysql mysqluser = Mysqluser.new params[:mysqluser] render :text => "#{mysqluser.username} #{mysqluser.password}" end Hope that''s what you were looking for. - --Jeff On Nov 20, 2005, at 9:43 AM, gary huntress wrote:> Hi, > > I''m clearly missing something very basic when it comes to form/field > helpers. So far I''ve had no problem with the examples that map form > fields to database models (albeit very simple). I''m making my first > form that I want to map to a non-model object and I don''t > understand how > to map the @params hash I receive into my own object. > > My simple test form looks like: > MySQL signup form<p> > <%= form_tag :action=>"processmysql" %> > <%= text_field ''mysqluser'', ''username'' %><br> > <%= password_field ''mysqluser'', ''password'' %> > <%= submit_tag "submit" %> > <%= end_form_tag %> > > I defined a class like: > class Mysqluser > attr_accessor :username > attr_accessor :password > > def initialize(u,p) > @username=u > @password=p > end > end > > and in my controller: > def processmysql > m = @params[:mysqluser] > u= m[:username] > p= m[:password] > mysqluser = Mysqluser.new(u,p) > render_text mysqluser.username + " " + mysqluser.password > > end > > Now this works perfectly fine but it seems overly complicated and > non-railsey. Is there a simpler way to map the field values in the > @params hash into a new mysqluser object? > > Thanks! > > > Gary H. > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails-----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.1 (Darwin) iD8DBQFDgMUEG9xIoXK+giARAmjxAJ9u5PE3D5epWo3WLnFpZn1Ll3aTJgCg/SOR 73HDQVCWRYpm8QVnq7Sp94c=C5Or -----END PGP SIGNATURE-----
On Nov 20, 2005, at 9:43 AM, gary huntress wrote:> Hi, > > I''m clearly missing something very basic when it comes to form/field > helpers. So far I''ve had no problem with the examples that map form > fields to database models (albeit very simple). I''m making my first > form that I want to map to a non-model object and I don''t > understand how > to map the @params hash I receive into my own object. > > My simple test form looks like: > MySQL signup form<p> > <%= form_tag :action=>"processmysql" %> > <%= text_field ''mysqluser'', ''username'' %><br> > <%= password_field ''mysqluser'', ''password'' %> > <%= submit_tag "submit" %> > <%= end_form_tag %> > > I defined a class like: > class Mysqluser > attr_accessor :username > attr_accessor :password > > def initialize(u,p) > @username=u > @password=p > end > end > > and in my controller: > def processmysql > m = @params[:mysqluser] > u= m[:username] > p= m[:password] > mysqluser = Mysqluser.new(u,p) > render_text mysqluser.username + " " + mysqluser.password > > end > > Now this works perfectly fine but it seems overly complicated and > non-railsey. Is there a simpler way to map the field values in the > @params hash into a new mysqluser object? > > Thanks! > > > Gary H. > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/railsNope, the reason the code looks more Rails-like when you''re using ActiveRecord is that AR can accept a has in its constructor or to update things. You might consider rewriting the constructor in your Mysqluser class to do the same thing so you can use u = Mysqluser.new( params[:mysqluser] ) and be done with it. /****************************************************** * Jeremy Voorhis, Lead Architect * PLANET ARGON, Rails Development, Consulting & Hosting * Portland, Oregon | p: 503.351.4730 | f: 815.642.4068 * www.planetargon.com | www.jvoorhis.com * Refactoring Rails | www.refactoringrails.com *******************************************************/ Also Cokemachineglow | www.cokemachineglow.com
Is Mysqluser an ActiveRecord model for a database table? If so... Rails book p 212 "the real reason that new() and create() take a hash of values is that you can construct model objects directly from form parameters" Maybe you could do something like the following. (not tested) <p>MySQL signup form<p> <%= form_tag :action=>"processmysql" %> <%= text_field ''username'' %><br> <!-- CHANGED --> <%= password_field ''password'' %> <!-- CHANGED --> <%= submit_tag "submit" %> <%= end_form_tag %> I defined a class like: class Mysqluser < ActiveRecord::Base end In the controller def processmysql mysqluser = Mysqluser.new(params) end -Peter _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
On Nov 20, 2005, at 10:56 AM, Peter Michaux wrote:> Is Mysqluser an ActiveRecord model for a database table? If so... > > Rails book p 212 "the real reason that new() and create() take a > hash of values is that you can construct model objects directly > from form parameters" > > Maybe you could do something like the following. (not tested) > > <p>MySQL signup form<p> > <%= form_tag :action=>"processmysql" %> > <%= text_field ''username'' %><br> <!-- CHANGED --> > <%= password_field ''password'' %> <!-- CHANGED --> > <%= submit_tag "submit" %> > <%= end_form_tag %> > > I defined a class like: > class Mysqluser < ActiveRecord::Base > end > > In the controller > def processmysql > mysqluser = Mysqluser.new(params) > end > > -Peter > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/railsI have a feelin'' he did not use AR for the Mysqluser class since he bothered to include the attr_accessor statements. /****************************************************** * Jeremy Voorhis, Lead Architect * PLANET ARGON, Rails Development, Consulting & Hosting * Portland, Oregon | p: 503.351.4730 | f: 815.642.4068 * www.planetargon.com | www.jvoorhis.com * Refactoring Rails | www.refactoringrails.com *******************************************************/ Also Cokemachineglow | www.cokemachineglow.com
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Not to be a dick, but his opening paragraph says: "I''m making my first form that I want to map to a non-model object..." I don''t think AR''s the route to take. On Nov 20, 2005, at 11:02 AM, Jeremy Voorhis wrote:> On Nov 20, 2005, at 10:56 AM, Peter Michaux wrote: > >> Is Mysqluser an ActiveRecord model for a database table? If so... >> >> Rails book p 212 "the real reason that new() and create() take a >> hash of values is that you can construct model objects directly >> from form parameters" >> >> Maybe you could do something like the following. (not tested) >> >> <p>MySQL signup form<p> >> <%= form_tag :action=>"processmysql" %> >> <%= text_field ''username'' %><br> <!-- CHANGED --> >> <%= password_field ''password'' %> <!-- CHANGED --> >> <%= submit_tag "submit" %> >> <%= end_form_tag %> >> >> I defined a class like: >> class Mysqluser < ActiveRecord::Base >> end >> >> In the controller >> def processmysql >> mysqluser = Mysqluser.new(params) >> end >> >> -Peter >> _______________________________________________ >> Rails mailing list >> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >> http://lists.rubyonrails.org/mailman/listinfo/rails > > I have a feelin'' he did not use AR for the Mysqluser class since he > bothered to include the attr_accessor statements. > > /****************************************************** > * Jeremy Voorhis, Lead Architect > * PLANET ARGON, Rails Development, Consulting & Hosting > * Portland, Oregon | p: 503.351.4730 | f: 815.642.4068 > * www.planetargon.com | www.jvoorhis.com > * Refactoring Rails | www.refactoringrails.com > *******************************************************/ > > Also Cokemachineglow | www.cokemachineglow.com > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails-----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.1 (Darwin) iD8DBQFDgMq5G9xIoXK+giARAtQfAJ92PraStWrUlvTwFYpIkc69wkmAJQCgzN0e iyrAYEPZsswDSIMvKOjrsDs=uC36 -----END PGP SIGNATURE-----
On Nov 20, 2005, at 11:12 AM, Jeff Smick wrote:> -----BEGIN PGP SIGNED MESSAGE----- > Hash: SHA1 > > Not to be a dick, but his opening paragraph says: > "I''m making my first form that I want to map to a non-model object..." > > I don''t think AR''s the route to take. > > On Nov 20, 2005, at 11:02 AM, Jeremy Voorhis wrote: > >> On Nov 20, 2005, at 10:56 AM, Peter Michaux wrote: >> >>> Is Mysqluser an ActiveRecord model for a database table? If so... >>> >>> Rails book p 212 "the real reason that new() and create() take a >>> hash of values is that you can construct model objects directly >>> from form parameters" >>> >>> Maybe you could do something like the following. (not tested) >>> >>> <p>MySQL signup form<p> >>> <%= form_tag :action=>"processmysql" %> >>> <%= text_field ''username'' %><br> <!-- CHANGED --> >>> <%= password_field ''password'' %> <!-- CHANGED --> >>> <%= submit_tag "submit" %> >>> <%= end_form_tag %> >>> >>> I defined a class like: >>> class Mysqluser < ActiveRecord::Base >>> end >>> >>> In the controller >>> def processmysql >>> mysqluser = Mysqluser.new(params) >>> end >>> >>> -Peter >>> _______________________________________________ >>> Rails mailing list >>> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >>> http://lists.rubyonrails.org/mailman/listinfo/rails >> >> I have a feelin'' he did not use AR for the Mysqluser class since >> he bothered to include the attr_accessor statements. >> >> /****************************************************** >> * Jeremy Voorhis, Lead Architect >> * PLANET ARGON, Rails Development, Consulting & Hosting >> * Portland, Oregon | p: 503.351.4730 | f: 815.642.4068 >> * www.planetargon.com | www.jvoorhis.com >> * Refactoring Rails | www.refactoringrails.com >> *******************************************************/ >> >> Also Cokemachineglow | www.cokemachineglow.com >> >> >> _______________________________________________ >> Rails mailing list >> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >> http://lists.rubyonrails.org/mailman/listinfo/rails > > -----BEGIN PGP SIGNATURE----- > Version: GnuPG v1.4.1 (Darwin) > > iD8DBQFDgMq5G9xIoXK+giARAtQfAJ92PraStWrUlvTwFYpIkc69wkmAJQCgzN0e > iyrAYEPZsswDSIMvKOjrsDs> =uC36 > -----END PGP SIGNATURE----- > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/railsCould he be making RailsMyAdmin?? I hope so! /****************************************************** * Jeremy Voorhis, Lead Architect * PLANET ARGON, Rails Development, Consulting & Hosting * Portland, Oregon | p: 503.351.4730 | f: 815.642.4068 * www.planetargon.com | www.jvoorhis.com * Refactoring Rails | www.refactoringrails.com *******************************************************/ Also Cokemachineglow | www.cokemachineglow.com
I''ve been trying to use the excellent Gruff package, however, I''m unable to make it work on OS X Tiger 10.4.3 although ruby and rails seem to be running fine. I got Typo running with MySQL on FCGI on the Powerbook using Tony Arnold''s installer, and running fixrbconfig. I also installed rmagick and Imagemagick through selfupdated darwinports. I''m a total ruby, rails, and OS X n00b. So, forgive me if this isn''t the appropriate venue for this email. So, when I run this script (which works on RHEL4) http://involution.com/browsershare.rb.txt I get this error on OS X: /usr/lib/ruby/gems/1.8/gems/gruff-0.0.5/lib/gruff/area.rb:8: uninitialized constant Gruff (NameError) from /usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:18:in `require__'' from /usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:18:in `require'' from /usr/lib/ruby/gems/1.8/gems/gruff-0.0.5/lib/gruff.rb:4 from /usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:18:in `require__'' from /usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:18:in `require'' from /usr/lib/ruby/site_ruby/1.8/rubygems.rb:175:in `activate'' from /usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:23:in `require'' from browsershare.rb:9 On RHEL4, I get this perty graph: http://involution.com/images/browsershare.png I was thinking that maybe somehow gruff couldn''t find base, but that appears to not be the case as I tried adding one of there: begin require ''gruff/base'' rescue LoadError puts "could not find gruff/base" end And I don''t see the puts message on the terminal, still the error. I know this is probably some very stupid that''s happening, but since I''m such a ruby n00b, I can''t figure it out. Tony
On 11/20/05, Jeff Smick wrote:> Not to be a dick, but his opening paragraph says: > "I''m making my first form that I want to map to a non-model object..."Oops _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Hi, I had similar problems earlier this evening on windows, found this in the forum at rubyforge relating to base.rb: " begin require ''rmagick'' rescue LoadError require ''RMagick'' # capitalized on Windows end If you replace those lines with if RUBY_PLATFORM != ''i386-mswin32'' require ''rmagick'' else require ''RMagick'' # capitalized on Windows end " it fixed my issues on windows. Tony Perrie wrote:> I''ve been trying to use the excellent Gruff package, however, I''m > unable to make it work on OS X Tiger 10.4.3 although ruby and rails > seem to be running fine. I got Typo running with MySQL on FCGI on the > Powerbook using Tony Arnold''s installer, and running fixrbconfig. I > also installed rmagick and Imagemagick through selfupdated > darwinports. I''m a total ruby, rails, and OS X n00b. So, forgive me > if this isn''t the appropriate venue for this email. > > So, when I run this script (which works on RHEL4) > > http://involution.com/browsershare.rb.txt > > I get this error on OS X: > > /usr/lib/ruby/gems/1.8/gems/gruff-0.0.5/lib/gruff/area.rb:8: > uninitialized constant Gruff (NameError) > from /usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:18:in > `require__'' > from > /usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:18:in `require'' > from /usr/lib/ruby/gems/1.8/gems/gruff-0.0.5/lib/gruff.rb:4 > from > /usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:18:in `require__'' > from > /usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:18:in `require'' > from /usr/lib/ruby/site_ruby/1.8/rubygems.rb:175:in `activate'' > from > /usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:23:in `require'' > from browsershare.rb:9 > > On RHEL4, I get this perty graph: > > http://involution.com/images/browsershare.png > > I was thinking that maybe somehow gruff couldn''t find base, but that > appears to not be the case as I tried adding one of there: > > begin > require ''gruff/base'' > rescue LoadError > puts "could not find gruff/base" > end > > And I don''t see the puts message on the terminal, still the error. > > I know this is probably some very stupid that''s happening, but since > I''m such a ruby n00b, I can''t figure it out. > > Tony > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > >
Thanks to all of those that replied. I understand much better now. The funny thing is that it has taken me so much longer to learn RoR than even perl specifically because of what I mentally call "Rails Magic". So much great stuff is done for me by Rails, that when things like marshalling @params into a class do not happen magically, I question myself doing something wrong before it even occurs to me that Rails won''t do it for me! Gary ----- Original Message ----- From: "Jeff Smick" <rails_lists-m0qWdWgHGwnKl/7hFL4KYti2O/JbrIOy@public.gmane.org> To: <rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org> Sent: Sunday, November 20, 2005 1:48 PM Subject: Re: [Rails] extra-newbie help with form helpers> -----BEGIN PGP SIGNED MESSAGE----- > Hash: SHA1 > > Gary, > > The following will allow you to expand the attributes in the Mysqluser > model without having to touch the controller. The model will now assume > that you''re passing it a hash with :username and :password set. > > class Mysqluser > attr_accessor :username, :password > > def initialize(params) > @username = params[:username] > @password = params[:password] > end > end > > def processmysql > mysqluser = Mysqluser.new params[:mysqluser] > render :text => "#{mysqluser.username} #{mysqluser.password}" > end > > Hope that''s what you were looking for. > > - --Jeff > > On Nov 20, 2005, at 9:43 AM, gary huntress wrote: > >> Hi, >> >> I''m clearly missing something very basic when it comes to form/field >> helpers. So far I''ve had no problem with the examples that map form >> fields to database models (albeit very simple). I''m making my first >> form that I want to map to a non-model object and I don''t understand how >> to map the @params hash I receive into my own object. >> >> My simple test form looks like: >> MySQL signup form<p> >> <%= form_tag :action=>"processmysql" %> >> <%= text_field ''mysqluser'', ''username'' %><br> >> <%= password_field ''mysqluser'', ''password'' %> >> <%= submit_tag "submit" %> >> <%= end_form_tag %> >> >> I defined a class like: >> class Mysqluser >> attr_accessor :username >> attr_accessor :password >> >> def initialize(u,p) >> @username=u >> @password=p >> end >> end >> >> and in my controller: >> def processmysql >> m = @params[:mysqluser] >> u= m[:username] >> p= m[:password] >> mysqluser = Mysqluser.new(u,p) >> render_text mysqluser.username + " " + mysqluser.password >> >> end >> >> Now this works perfectly fine but it seems overly complicated and >> non-railsey. Is there a simpler way to map the field values in the >> @params hash into a new mysqluser object? >> >> Thanks! >> >> >> Gary H. >> >> _______________________________________________ >> Rails mailing list >> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >> http://lists.rubyonrails.org/mailman/listinfo/rails > > -----BEGIN PGP SIGNATURE----- > Version: GnuPG v1.4.1 (Darwin) > > iD8DBQFDgMUEG9xIoXK+giARAmjxAJ9u5PE3D5epWo3WLnFpZn1Ll3aTJgCg/SOR > 73HDQVCWRYpm8QVnq7Sp94c> =C5Or > -----END PGP SIGNATURE----- > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
On 20.11.2005, at 21.46, Tony Perrie wrote:> I''ve been trying to use the excellent Gruff package, however, I''m > unable to make it work on OS X Tiger 10.4.3 although ruby and rails > seem to be running fine. I got Typo running with MySQL on FCGI on > the Powerbook using Tony Arnold''s installer, and running > fixrbconfig. I also installed rmagick and Imagemagick through > selfupdated darwinports. I''m a total ruby, rails, and OS X n00b. > So, forgive me if this isn''t the appropriate venue for this email. > > So, when I run this script (which works on RHEL4) > > http://involution.com/browsershare.rb.txt > > I get this error on OS X: > > /usr/lib/ruby/gems/1.8/gems/gruff-0.0.5/lib/gruff/area.rb:8: > uninitialized constant Gruff (NameError) > from /usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:18:in > `require__'' > from /usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb: > 18:in `require'' > from /usr/lib/ruby/gems/1.8/gems/gruff-0.0.5/lib/gruff.rb:4 > from /usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb: > 18:in `require__'' > from /usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb: > 18:in `require'' > from /usr/lib/ruby/site_ruby/1.8/rubygems.rb:175:in `activate'' > from /usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb: > 23:in `require'' > from browsershare.rb:9 > > On RHEL4, I get this perty graph: > > http://involution.com/images/browsershare.png > > I was thinking that maybe somehow gruff couldn''t find base, but > that appears to not be the case as I tried adding one of there: > > begin > require ''gruff/base'' > rescue LoadError > puts "could not find gruff/base" > end > > And I don''t see the puts message on the terminal, still the error. > > I know this is probably some very stupid that''s happening, but > since I''m such a ruby n00b, I can''t figure it out.Tony, Make sure you have rmagick installed on your PowerBook. I didn''t and that seemed to cause the exact problems you describe here. //jarkko -- Jarkko Laine http://jlaine.net http://odesign.fi _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails