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.
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| => ...