Collecting emails on a static page in rails

I have a static page on my Rails 3.1 site. I have a Visitor model that has only an email attribute (email:string). I would like to collect emails from visitors to my site. They will type their email in a text field and click on submit and then be redirect to a thank you page.

I can do this sort of thing with resources but I don't need that much. Just a submit and a thank you.

How do I do this? Please provide code for the view and the controller.

Thank you.

Answers


What is wrong about using resources? Your visitor is a resource, so why not treat it like one?

In your routes:

resource :visitors, :only => :create # POST to visitor_path to create a visitor
root_to ...

In the VisitorController

def create
  @visitor = Visitor.new(params[:visitor])
  if @visitor.save
    render "pages/comingsoon" # or redirect, or render leave empty to render views/visitors/create.html.erb
  else
    # ...
  end
end

Now you can have a file views/pages/comingsoon.html.erb with content

<h1>Thank you</h1>
<p>Stored your email address <%= @visitor.email %></p>

And for completeness, the form for a new visitor:

<%= form_for Visitor.new do |f| => ...

Need Your Help

npm install https

node.js express

Hopefully a simple question,

Django ModelMultipleChoiceField for another model's many-to-many

django django-models django-forms

I'm working on a DIY application (Django 1.5) and I've reached a roadblock. The main models involved are Guide, Tool, Item, Step. A Guide can have many Tools and Items, and a Tool or Item can bel...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.