Extra-newbie help with form helpers

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


<%= form_tag :action=>“processmysql” %>
<%= text_field ‘mysqluser’, ‘username’ %>

<%= 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:

MySQL signup form

p= m[:password]

Gary H.


Rails mailing list
[email protected]
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:

MySQL signup form

p= m[:password]

Gary H.


Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails

Nope, 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.

/******************************************************

Also Cokemachineglow | www.cokemachineglow.com

On Nov 20, 2005, at 10:56 AM, Peter M. wrote:

<%= text_field ‘username’ %>

mysqluser = Mysqluser.new(params)
end

-Peter


Rails mailing list
[email protected]
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.

/******************************************************

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:

MySQL signup form

In the controller

I have a feelin’ he did not use AR for the Mysqluser class since he
Also Cokemachineglow | www.cokemachineglow.com


Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (Darwin)

iD8DBQFDgMq5G9xIoXK+giARAtQfAJ92PraStWrUlvTwFYpIkc69wkmAJQCgzN0e
iyrAYEPZsswDSIMvKOjrsDs=
=uC36
-----END PGP SIGNATURE-----

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)

MySQL signup form

<%= form_tag :action=>"processmysql" %> <%= text_field 'username' %>
<%= password_field 'password' %> <%= 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

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 Nov 20, 2005, at 11:12 AM, Jeff S. wrote:

On Nov 20, 2005, at 10:56 AM, Peter M. wrote:

<%= form_tag :action=>“processmysql” %>
def processmysql
he bothered to include the attr_accessor statements.

iyrAYEPZsswDSIMvKOjrsDs=
=uC36
-----END PGP SIGNATURE-----


Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails

Could he be making RailsMyAdmin?? I hope so!

/******************************************************

Also Cokemachineglow | www.cokemachineglow.com

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.

On 11/20/05, Jeff S. 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

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 S.” [email protected]
To: [email protected]
Sent: Sunday, November 20, 2005 1:48 PM
Subject: Re: [Rails] extra-newbie help with form helpers

On 20.11.2005, at 21.46, Tony P. wrote:

http://involution.com/browsershare.rb.txt
from /usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:
http://involution.com/images/browsershare.png
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