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: Peak Obsession.
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.
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.
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.
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
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.
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
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 [email protected]
509-577-7732
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.
?> :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 [email protected]
509-577-7732
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
?> :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 [email protected]
509-577-7732
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