Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 281145
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T05:10:42+00:00 2026-05-12T05:10:42+00:00

I’ve just added a contact form to my Rails application so that site visitors

  • 0

I’ve just added a contact form to my Rails application so that site visitors can send me a message. The application has a Message resource and I’ve defined this custom route to make the URL nicer and more obvious:

map.contact '/contact', :controller => 'messages', :action => 'new'

How can I keep the URL as /contact when the model fails validation? At the moment the URL changes to /messages upon validation failure.

This is the create method in my messages_controller:

def create
  @message = Message.new(params[:message])

  if @message.save
    flash[:notice] = 'Thanks for your message etc...'
    redirect_to contact_path
  else
    render 'new', :layout => 'contact'
  end
end

Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T05:10:42+00:00Added an answer on May 12, 2026 at 5:10 am

    One solution would be to make two conditional routes with the following code:

    map.contact 'contact', :controller => 'messages', :action => 'new', :conditions => { :method => :get }
    map.connect 'contact', :controller => 'messages', :action => 'create', :conditions => { :method => :post } # Notice we are using 'connect' here, not 'contact'! See bottom of answer for explanation
    

    This will make all get request (direct requests etc.) use the ‘new’ action, and the post request the ‘create’ action. (There are two other types of requests: put and delete, but these are irrelevant here.)

    Now, in the form where you are creating the message object change

    <%= form_for @message do |f| %>
    

    to

    <%= form_for @message, :url => contact_url do |f| %>
    

    (The form helper will automatically choose the post request type, because that is default when creating new objects.)

    Should solve your troubles.

    (This also won’t cause the addressbar to flicker the other address. It never uses another address.)

    .

    • Explanation why using connect is not a problem here
      The map.name_of_route references JUST THE PATH. Therefore you don’t need a new named route for the second route. You can use the original one, because the paths are the same. All the other options are used only when a new request reaches rails and it needs to know where to send it.

    .

    EDIT

    If you think the extra routes make a bit of a mess (especially when you use it more often) you could create a special method to create them. This method isn’t very beautiful (terrible variable names), but it should do the job.

    def map.connect_different_actions_to_same_path(path, controller, request_types_with_actions) # Should really change the name...
      first = true # There first route should be a named route
      request_types_with_actions.each do |request, action|
        route_name = first ? path : 'connect'
        eval("map.#{route_name} '#{path}', :controller => '#{controller}', :action => '#{action}', :conditions => { :method => :#{request.to_s} }")
        first = false
      end
    end
    

    And then use it like this

    map.connect_different_actions_to_same_path('contact', 'messages', {:get => 'new', :post => 'create'})
    

    I prefer the original method though…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I have just tried to save a simple *.rtf file with some websites and
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have a jquery bug and I've been looking for hours now, I can't
Basically, what I'm trying to create is a page of div tags, each has
I have a small JavaScript validation script that validates inputs based on Regex. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.