Re: How to make a simple Contact Form

Sweet, glad it worked mate! big_smile Great thing about rails is once you've cracked something it's usually simple to repeat it in the future, with PHP I seem to feel like I'm reinventing the wheel every time I do something smile

Re: How to make a simple Contact Form

I totally agree. Even though the learning curve is rough at times, my toolkit is constantly growing. I am even starting to get ideas for plugins and generators at this point... but one battle at a time, right?

Re: How to make a simple Contact Form

i know what you mean, I'm in the middle of extracting a cool access control system I wrote for a pet project into a plugin, it's all pretty new to me at this stage but I feel good knowing once I manage it I'll have a great addition to my knowledge (and a great bit of reusable code!)

good coding dude smile

Re: How to make a simple Contact Form

Here's a little bit of extra credit.

My form is working slendidly, but it does not check that the email is in the correct format. I imagine that this requires some Regex magic, but I could be mistaken.

Edit: FOund something interesting here

http://regexlib.com/REDetails.aspx?regexp_id=711

thing is , where does this belong? in the model or controller?

Last edited by pimpmaster (2007-05-02 18:20:50)

Re: How to make a simple Contact Form

pimpmaster wrote:

Here's a little bit of extra credit.

My form is working slendidly, but it does not check that the email is in the correct format. I imagine that this requires some Regex magic, but I could be mistaken.

Edit: FOund something interesting here

http://regexlib.com/REDetails.aspx?regexp_id=711

thing is , where does this belong? in the model or controller?

I use the following pattern to match email addresses:-

/^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i

There's lots of places you could put it, in javascript for example, but then peeps can always turn that off - off the top of my head I think a good way would be something like the following, in the controller which actually calls the deliver_method_name

def contact_us
  # all the rest of your method
  if params[:form][:email] =~ /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i
    Notifications.deliver_my_fancy_email(address,message)
    redirect_to :action => 'email_sent'
  else
    flash[:notice] = "Email isn't a valid address"
    redirect_to :action=> 'contact_us'
  end
end

note: that's a =~ (equals then tilde) not a =- (equals then minus)  - on my screen it's looking odd in the code blocks because of the font scaling, thought I'd better mention!

obviously you can wrap it in begin rescue blocks, I was keeping this simple - but basically it  sets up a validation like behaviour, in that it returns to the form with a flash notice telling what's wrong. I suppose you could even extend it with some trickery to highlight the field etc etc

hope it helps smile

Re: How to make a simple Contact Form

This is fantastic. My contact form finally feels solid and production-ready.

Thanks again!

Re: How to make a simple Contact Form

no worries mate, glad to help smile

Re: How to make a simple Contact Form

Okay.. last question I swear!

I was wondering if there was a way to raise an error for the invalid email instead of using flash.. this way we get the field highlighting of error_messages_for. Thing is, I am pretty sure that this is an ActiveRecord thing, so I would have to refactor the regex to my model somehow.

In a perfect world, this would be part of Rails core, so we could just do

validates_email_format :email_address

How cool would that be?

Re: How to make a simple Contact Form

Got it!

validates_format_of :email, :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i, :on => :create

Re: How to make a simple Contact Form

For me now, and others reading in the future - where are you putting that?  And what does it enable you to do?

Re: How to make a simple Contact Form

Oops.. I probably should have mentioned that.

I have put this inside my Contact model, at the very top. I also got rid of the conditional flash statement in my controller so that Active Record handles this error now.

This allows me to keep my error messages consistent, and it also takes full advantage of the "fieldWithErrors" class that Rails puts on the problematic input fields.

An Example:

http://www.funkysoulrebels.com/rails/email_error.gif

Re: How to make a simple Contact Form

pimpmaster wrote:

In a perfect world, this would be part of Rails core, so we could just do

validates_email_format :email_address

How cool would that be?

Validating email addresses is a difficult thing, and there's no one-regex-fits-all solution. I'm guessing this is why Rails hasn't put it in core.

Railscasts - Free Ruby on Rails Screencasts

Re: How to make a simple Contact Form

Totally agree, Ryan

At this point I could not be any happier with the current solution... concise AND flexible.

Re: How to make a simple Contact Form

there is a plugin that gives validates_email_format_of - if it's of some use i can dig out the link.

basically it checks an email address conforms to the relevent standard - i think the standard is RFC 2822 or something like that

Re: How to make a simple Contact Form

its all good chris, I actually prefer this method..I get to see and tweak the regex right on the spot if I need to. Pretty darn handy!

Re: How to make a simple Contact Form

yup, i guess the plugin does pretty much the same..

just taken a look at the code and it uses this regex:

/^(([A-Za-z0-9]+_+)|([A-Za-z0-9]+\-+)|([A-Za-z0-9]+\.+)|([A-Za-z0-9]+\++))*[A-Za-z0-9]+@((\w+\-+)|(\w+\.))*\w{1,63}\.[a-zA-Z]{2,6}$/i

Last edited by chris-uk (2007-05-03 21:32:34)

Re: How to make a simple Contact Form

How can I validate my form the best way? The thing is : my form is working fine but the validation isn't(!) and I really want to add validation to it.
I'm working with rails 2.0 and I'd like to do it dry, this is my code:


site controller

  def contact
  end
 
  def send_contact_form
    @contact = params[:contact]
    if request.post?
      Contactsite.deliver_verzenden_mail(params[:contact])
      flash[:notice] = "Uw bericht is succesvol verzonden"
       redirect_to :action => 'index', :permalink => "index"
     else
       render :action => "contact"
    end
  end

site model
class Site < ActiveRecord::Base

      validates_presence_of :name, :email_address, :message
end


and my actionmailer file
class Contactsite < ActionMailer::Base

  def verzenden_mail(contact)
    @recipients =     "my email adress"
    @from =           contact[:email]
    @subject =        contact[:subject]
    @body['name'] =   contact[:name]
    @body['email'] =  contact[:email]
    @body['message'] = contact[:message]
  end
end

Re: How to make a simple Contact Form

Ok, I did diabactz(sp?) code and I'm having one problem that I know is probably really obvious but I don't see why it's doing it.  I'm fairly new to Rails, but til today I thought I was picking it up pretty well.

Here's my form code:

<form action="/emailer/my_mailer" method="post" id="contact_form">
   
   <p>Your name <br/>
   <%= text_field 'comment', 'name' , :size => 36 %></p>
   
   <!-- You might want to make subject a drop down select box -->
   <p>Your inquiry type <br/>
   <%= text_field 'comment', 'subject' , :size => 36 %></p>
   
   <p>Your Email<br/>
   <%= text_field 'comment', 'email' , :size => 36 %></p>
   
   <p>Your Message <br/>
   <%= text_area 'comment', 'message', :cols => 42, :rows => 9  %></p>
  <br/><br/>
   <!-- Before you submit, check if the values are there -->
   <%= hidden_field 'comment', 'author', :value => @classified.email %>
   <%= token_tag %>
   <%= submit_tag 'Report Error', :onclick => 'checkForm();' , :type => 'button' %>
   
   </form>

My first problem is that when I click on the buttom, it takes me to "/emailer" instead of "/emailer/my_mailer".  What gives?

Also, as you can see, I added a hidden field comment[author] because I've got this form embedded in a classified ad page (where you can respond directly to that ad's author).  It should automatically set the recipient address to comment[author].

In emailer_controller.rb I put this:

 def my_mailer
   author = params[:comment][:author]
   from_name = params[:comment][:name]
   from_email = params[:comment][:email]
   the_subject = params[:comment][:subject]
   message = params[:comment][:message]
   
   #...

I added the author field there.  That should work, right?

Re: How to make a simple Contact Form

I used Pixelateur form example posted on http://railsforum.com/viewtopic.php?id=404&p=2 and I get the following error.  Does anyone have a fully working example they can post ?



C:/ruby/lib/ruby/gems/1.8/gems/activesupport-2.3.2/lib/active_support/dependencies.rb:443:in `load_missing_constant': uninitialized constant ActionMailer (NameError)

        from C:/ruby/lib/ruby/gems/1.8/gems/activesupport-2.3.2/lib/active_support/dependencies.rb:80:in `const_missing'

        from C:/ruby/lib/ruby/gems/1.8/gems/activesupport-2.3.2/lib/active_support/dependencies.rb:92:in `const_missing'

        from C:/rubyapps/fcalc/config/environment.rb:11

        from C:/ruby/lib/ruby/gems/1.8/gems/rails-2.3.2/lib/initializer.rb:111:in `run'

        from C:/rubyapps/fcalc/config/environment.rb:9

        from C:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require'

        from C:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `require'

        from C:/ruby/lib/ruby/gems/1.8/gems/activesupport-2.3.2/lib/active_support/dependencies.rb:156:in `require'

        from C:/ruby/lib/ruby/gems/1.8/gems/activesupport-2.3.2/lib/active_support/dependencies.rb:521:in `new_constants_in'

        from C:/ruby/lib/ruby/gems/1.8/gems/activesupport-2.3.2/lib/active_support/dependencies.rb:156:in `require'

        from C:/ruby/lib/ruby/gems/1.8/gems/rails-2.3.2/lib/commands/server.rb:84

        from C:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require'

        from C:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `require'

        from script/server:3




Any help would be appreciated.

Thanks

Mark

Last edited by mp84601 (2009-08-17 14:35:17)