4

In my routes.rb I have the following:

get "contact" => "inquiries#new"

So that when I go to /contact in the browswer, it calls the InquiriesController's new action.

Now when I try to call render "new" in the create action inside InquiriesController:

def create
    …
    render "new"
end

The resulting url in the browser is /inquiries.

Is there a way besides calling redirect_to to render "new" but have the url as /contact in the browser?

user2345093
  • 623
  • 6
  • 13
  • I'm agree that you should redirect_to if you want your address to be /contact but why do this ? i think there is no problem for your visitors to see the link change from contact to inquiries ! – medBouzid Aug 06 '13 at 11:40

4 Answers4

13

Short answer is No. And here's why:

render is different from redirect_to. When you write redirect_to :action, you are initiating an entirely new browser request. The rails stack is hit, again routes are searched for and the corresponding action is executed. Its exactly the same as entering the url in address bar and pressing enter.

On the other hand, when you use render, you are telling which view to use for the current request. As such, the address in the address bar will generally be of the action in which you are calling render. That's because you put an address and then you tell rails to display a different page in that same request.

In a nutshell, while redirect_to begins an entirely new request cycle, render simply replaces the default view with what you choose in the same request cycle.

So if you want the address bar to change, you will have to initiate a new request to the address you want. It can be by manually entering the address, clicking a link to that address or redirecting to it from rails.

Hope this helps.

Akash
  • 5,153
  • 3
  • 26
  • 42
  • 1
    Is it not possible to call some javascript code to change that ? Turbolinks is is already doing similar stuff with browser history and AJAX requests. That `No` is most likely outdated. – Cyril Duchon-Doris Apr 17 '16 at 14:37
5

The solution is to use custom routes, if you use Restful routing, you can simply add this line to your routes.rb :

resources :inquiries, path: "contact", as: :inquiries, only: [:create]

here you tell rails to change url by default from inquiries to contact when the name of the action is create

if you want other action to match an url which is begin with contact, just add the name of the action to "only", for example : only: [:create, :update ...]

if you want all your actions in that controller (inquiries) to be customized to "contact" just remove only like this :

resources :inquiries, path: "contact", as: :inquiries

and all your routes for inquiries controller will be change from /inquiries to /contact

for more details about how to customizing restful routes please check this link

medBouzid
  • 7,484
  • 10
  • 56
  • 86
3

When using render :new in the create action, it will use the same URL that the form posted to.

Therefore, if you would like to set up both an inquires you can set up your routes like:

get '/contact', 'inquiries#new', as: 'contact'
post '/contact', 'inquiries#create'

You can also use the resources method as medBo references, but I just prefer plain old get and post when I'm doing custom things. Also, these routes can co-exist with your exiting inquiries routes without any ill affects.

Then with those routes sets, you can create your contact from by writing:

<%= form_tag contacts_url do %>
   ...
<% end %>

The important step here, is that we set up the form to post to `/contact' instead of posting to '/inquiries'.

Nicholas DeLuca
  • 633
  • 6
  • 7
2

I think you first need to understand difference between redirect_to & render

For /contact url

change

render "new"

to

redirect_to "/contact"
Community
  • 1
  • 1
Salil
  • 46,566
  • 21
  • 122
  • 156