"inquiries#new" So, when I go to /contact in br...">

Rails display "action" as custom URL

In my routes.rb , I have the following:

 get "contact" => "inquiries#new" 

So, when I go to /contact in browswer, it invokes the InquiriesController new action.

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

 def create … render "new" end 

The resulting URL in the browser /inquiries .

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

+4
source share
4 answers

Short answer no. And that's why:

render is different from redirect_to . When you write redirect_to :action , you initiate a completely new browser request. The rails stack hits, iterates through the routes again and takes the appropriate action. Its just like typing url in the address bar and pressing enter.

On the other hand, when you use render , you specify which view to use for the current request. So the address in the address bar will usually have an action in which you call render . This is because you put the address and then indicated the rails to display another page in the same request.

In a nutshell, and redirect_to begins a whole new cycle of requests, render simply replaces the default view with what you select in the same request.

So, if you want to change the address bar, you will need to initiate a new request for the address you want. This can be by manually entering an address, clicking a link to this address or redirecting to it from rails.

Hope this helps.

+10
source

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 specify the rails for changing the default URL from inquiries to contact when the create action name

if you want another action to match the URL starting with contact , just add the action name to " only ", for example: only: [:create, :update ...]

if you want all your actions in that controller (request) to be set to "contact" , simply remove only as follows:

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

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

For more information on setting up quiet routes, see the link

+5
source

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

Therefore, if you want to configure both queries, you can configure your routes as follows:

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

You can also use the resources method as medBo links, but I just prefer the plain old get and post when I do the usual things. In addition, these routes can coexist with your query routes without any negative consequences.

Then with these sets of routes you can create your contact by writing:

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

The important step here is that we configure the form to submit to `/ contact 'instead of posting to / requests.

+3
source

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

For /contact url

change

 render "new" 

to

 redirect_to "/contact" 
+2
source

Source: https://habr.com/ru/post/1495500/


All Articles