Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>One solution would be to make two conditional routes with the following code:</p> <pre><code>map.contact 'contact', :controller =&gt; 'messages', :action =&gt; 'new', :conditions =&gt; { :method =&gt; :get } map.connect 'contact', :controller =&gt; 'messages', :action =&gt; 'create', :conditions =&gt; { :method =&gt; :post } # Notice we are using 'connect' here, not 'contact'! See bottom of answer for explanation </code></pre> <p>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.)</p> <p>Now, in the form where you are creating the message object change</p> <pre><code>&lt;%= form_for @message do |f| %&gt; </code></pre> <p>to</p> <pre><code>&lt;%= form_for @message, :url =&gt; contact_url do |f| %&gt; </code></pre> <p>(The form helper will automatically choose the post request type, because that is default when creating new objects.)</p> <p>Should solve your troubles.</p> <p>(This also won't cause the addressbar to flicker the other address. It never uses another address.)</p> <p>.</p> <ul> <li>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.</li> </ul> <p>.</p> <p><strong>EDIT</strong></p> <p>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.</p> <pre><code>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 =&gt; '#{controller}', :action =&gt; '#{action}', :conditions =&gt; { :method =&gt; :#{request.to_s} }") first = false end end </code></pre> <p>And then use it like this</p> <pre><code>map.connect_different_actions_to_same_path('contact', 'messages', {:get =&gt; 'new', :post =&gt; 'create'}) </code></pre> <p>I prefer the original method though...</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload