Forum: Ruby on Rails contact form/no db table?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
blazar (Guest)
on 2005-11-14 00:20
(Received via mailing list)
Apologies because I know this has come up before but I am simply having
no luck search archives or Google other than this wiki article:
http://wiki.rubyonrails.com/rails/pages/HowToUseRa....

What I want to do is have a contact form in my app. It doesn't need a db
table, just the fields, but I do want to use ActionMailer to send the
messages. I generated the mailer and thought a simple model class,
controller and form would do the trick. Controller and form were (if I
did this right) okay but not getting anywhere with the model. Controller
has new and send_message actions, new shows the form and send_message,
which is the form's action, calls the generated mailer.

Could some kind soul help with the model? The fields are :name, :email,
:subject, :message, :ipaddress, :c_date (the last two are set by
:initialize in the model).

If I get this done I'll update that wiki article with the lesson
learned.

Thanks,
Bill
Daniel -. (Guest)
on 2005-11-14 01:08
(Received via mailing list)
Hi Bill,

Is it possible just to make your model so that it does not inherit from
ActiveRecord? It would only be a volotile object, but from what your
suggesting in your email, I think this is what your after.

eg
class Contact

I hope this is what your looking for
blazar (Guest)
on 2005-11-14 01:32
(Received via mailing list)
Yes, that's exactly what I have in mind. Probably I'm too new but how to
write the model class is confusing me, if someone could get me started
that would be a big help.

Bill
Nshbrown N. (Guest)
on 2005-11-14 01:50
(Received via mailing list)
Hey Bill,

When you generate your model, simply remove the "< ActiveRecord::Base"
within the class definition,

Inside /app/models/my_new_model.rb could contain..

class MyNewModel
    def method
        # put your logic inside here
    end
End

You will not get all the nice features of ActiveRecord, such as find,
before_filter, etc. But from what it sounds like you are after, you
don't
really need most of them anyways.

Your controller will function just as it would normally, so create
inside
/app/controllers/my_new_model.rb the following:

class MyNewModelController < ApplicationController
    def index
    end
end

And put your view inside the views/my_action_model/index.rhtml which can
contain any X/HTML you want

Warmest regards,
Nathan.
blazar (Guest)
on 2005-11-14 02:11
(Received via mailing list)
Nathan,

Thanks! That's more or less what I did but I'm not sure what to put in
the model methods so the fields are proper instance variables. I'm a
noob and AWD doesn't seem to cover this.

Bill
ezra (Guest)
on 2005-11-14 03:14
(Received via mailing list)
Bill-

	You could use a Struct to make your non AR model act like an Active
Record. Here is a little irb session that will make this hopefully
more clear:

 >> MyModel = Struct.new(:name,
?>     :email, :subject, :message, :ipaddress, :c_date)
=> MyModel
 >> @a = MyModel.new('Jimmy', 'removed_email_address@domain.invalid', 'Foo B.', 
'Hello
World!', '120.0.0.1', '11/14/2005')
=> #<struct MyModel name="Jimmy", email="removed_email_address@domain.invalid", 
subject="Foo
Bar", message="Hello World!", ipaddress="120.0.0.1",
c_date="11/14/2005">
 >> @a.name
=> "Jimmy"
 >> @a.message
=> "Hello World!"

So define your model like this:

my_model.rb

MyModel = Struct.new
(:name,  :email, :subject, :message, :ipaddress, :c_date)


and now you can use it almost just like an Active Record in your
controller:

def create_mailer
    @content = MyModel.new(params[:name], params[:email], params
[:subject], params[:message], params[:ipaddress], params[:c_date])
    Mailer.deliver_notification(@content)
end

Or whatever you want to use your new struct model for. Let me know if
this works for you. I think I understand what you are trying to do
and this is the way that I have been doing email only forms that
don't need to have an AR model backing them.


Cheers-

-Ezra



On Nov 13, 2005, at 3:28 PM, Bill Lazar wrote:

>> what your suggesting in your email, I think this is what your after.
>>     What I want to do is have a contact form in my app. It doesn't
>>     Could some kind soul help with the model? The fields are :name,
> http://lists.rubyonrails.org/mailman/listinfo/rails
>

-Ezra Z.
WebMaster
Yakima Herald-Republic Newspaper
removed_email_address@domain.invalid
509-577-7732
Daniel -. (Guest)
on 2005-11-14 03:20
(Received via mailing list)
Bill,

I believe that the instance variables are set as you use them.
Alternativley
you could use the *attr_accessible *macro...

attr_accessible :email, :subject, :message, :ipaddress, :c_date

This will create the required get and set methods so that you can access
the
variables as per a normal AR model. You can over write these methods if
you
want (say for the ipaddress) or you could just instantiate a model like
an
AR

@my_model = Contact.new(params[:contact])

I'm not sure if this works tho...
blazar (Guest)
on 2005-11-14 04:26
(Received via mailing list)
Thanks for the help. I tried this suggestion but get an error still:

wrong number of arguments (1 for 0)

on this line:

@contact = Contact.new(@params[:contact])

Same error (except 6 for 1) if I use:

@contact = Contact.new(params[:name], params[:email], params[:subject],
params[:message], params[:ipaddress], params[:c_date] )

Bill
blazar (Guest)
on 2005-11-14 04:32
(Received via mailing list)
Thanks for the suggestion but this didn't do the trick.

Bill
ezra (Guest)
on 2005-11-14 04:41
(Received via mailing list)
You could try it like this:

def send_message
    @contact = Contact.new
    @contact.name = params[:name]
    @contact.email = params[:email]
    and so on...
end

	See if that works for you. Or if you still have problems then fire
up script/console and play with your Contact model to get a feel for
how it works. I'm sure their is just something simple missing that is
giving you those errors.


Cheers-
-Ezra

On Nov 13, 2005, at 6:23 PM, Bill Lazar wrote:

> @contact = Contact.new(params[:name], params[:email], params
>> ?>     :email, :subject, :message, :ipaddress, :c_date)
>> So define your model like this:
>> Or whatever you want to use your new struct model for. Let me know
>>> Bill
>>>> I hope this is what your looking for
>>>>     a db table, just the fields, but I do want to use
>>>>     generated mailer.
>> WebMaster
>> Yakima Herald-Republic Newspaper
>> removed_email_address@domain.invalid
>> 509-577-7732
>
> _______________________________________________
> Rails mailing list
> removed_email_address@domain.invalid
> http://lists.rubyonrails.org/mailman/listinfo/rails
>

-Ezra Z.
WebMaster
Yakima Herald-Republic Newspaper
removed_email_address@domain.invalid
509-577-7732
ezra (Guest)
on 2005-11-14 04:41
(Received via mailing list)
Oh, also make sure that the only line in your contact.rb model file
is this:

Content = Struct.new
(:name, :email, :subject, :message, :ipaddress, :c_date


-Ezra


On Nov 13, 2005, at 6:23 PM, Bill Lazar wrote:

> @contact = Contact.new(params[:name], params[:email], params
>> ?>     :email, :subject, :message, :ipaddress, :c_date)
>> So define your model like this:
>> Or whatever you want to use your new struct model for. Let me know
>>> Bill
>>>> I hope this is what your looking for
>>>>     a db table, just the fields, but I do want to use
>>>>     generated mailer.
>> WebMaster
>> Yakima Herald-Republic Newspaper
>> removed_email_address@domain.invalid
>> 509-577-7732
>
> _______________________________________________
> Rails mailing list
> removed_email_address@domain.invalid
> http://lists.rubyonrails.org/mailman/listinfo/rails
>

-Ezra Z.
WebMaster
Yakima Herald-Republic Newspaper
removed_email_address@domain.invalid
509-577-7732
blazar (Guest)
on 2005-11-14 05:21
(Received via mailing list)
Thanks for the continuing help! I tried a bunch of variations, including
using the console, but all result in the same error: undefined method
`name=' for #<Contact:0x38440d8>. I know you said to only have the
Struct.new line in the model class file but I wonder--is the controller
is finding the model?

model is app/models/contact.rb
controller is app/controllers/contact_controller.rb

Bill
ezra (Guest)
on 2005-11-14 06:00
(Received via mailing list)
OK Bill-

	Lets turn this around then. If you just need this per request and
don't need to save anything then how about this:


in your controller:

def whatever
    Contact = Struct.new
(:name,  :email, :subject, :message, :ipaddress, :c_date)
    @contact = Contact.new(params[:name], params[:email], etc.... )
    do whatever with @contact
end

Does that work? Without any model at all?

Cheers
-Ezra



On Nov 13, 2005, at 7:04 PM, Bill Lazar wrote:

>
>> feel for  how it works. I'm sure their is just something simple
>>> @contact = Contact.new(@params[:contact])
>>>> Bill-
>>>> ipaddress="120.0.0.1",  c_date="11/14/2005">
>>>> def create_mailer
>>>> On Nov 13, 2005, at 3:28 PM, Bill Lazar wrote:
>>>>>> Is it possible just to make your model so that it does not
>>>>>>     having no luck search archives or Google other than this wiki
>>>>>> and  form
>>>>>>     set by :initialize in the model).
>>>> 509-577-7732
>
> _______________________________________________
> Rails mailing list
> removed_email_address@domain.invalid
> http://lists.rubyonrails.org/mailman/listinfo/rails
>

-Ezra Z.
WebMaster
Yakima Herald-Republic Newspaper
removed_email_address@domain.invalid
509-577-7732
blazar (Guest)
on 2005-11-14 06:51
(Received via mailing list)
Ezra,

Phew! Thanks so much. I finally got this up. The answer turned out to be
using the correct keyword in the model class:

  attr_accessor :name, :email, :subject, :message, :ipaddress, :c_date

I was hoping to use the model so I could include in the
ActiveRecord::Validations for later.

Bill
This topic is locked and can not be replied to.