What is the best way to route a static controller in Rails?

Posted by yuval on Stack Overflow See other posts from Stack Overflow or by yuval
Published on 2010-04-04T00:22:13Z Indexed on 2010/04/04 0:23 UTC
Read the original article Hit count: 506

I have a static_controller that is in charge of all the static pages in the site and works as follows in routes.rb:

map.connect ':id', :controller => 'static', :action => 'show'

I have a static page called about that among other information, has a contact form. I currently have a contacts_controller that is in charge of inserting the contact information to the database. Inside my routes.rb file, I have:

map.resources :contacts

My contact form (simplified) looks like this:

<% form_for @contact do |f| %>
    <p class="errors"><%= f.error_messages %></p>  

    <p>
        <%= f.label :first_name %>
        <%= f.text_field :first_name %>
    </p>


    <p class="buttons"><%= f.submit %></p>
<% end %>

Which in turn submits to the create action of my contacts_controller. My create action looks like this:

def create
    @contact = Contact.new(params[:contact])
    if @contact.save
      flash[:notice] = "Email delivered successfully."
    end
    redirect_to "about"
end

The problem is, is the that when I redirect back to my about page the error_messages for the form get lost (since the error_messages for the form only exist for one request, and that request ends upon redirect). How would I go about preserving the error_messages and still linking the users back to the about static url? Would a session/flash be sufficient (if so, what code would I use to pass error messages) or am I going about this whole thing wrong?

Thanks!

© Stack Overflow or respective owner

Related posts about contact-form

Related posts about ruby-on-rails