Form_for tag for a resource with two different controllers and RESTful routing

I have a Rails application with a Route resource and a Route controller (not to be confused with Rails routes). I configured it so that site administrators (and only administrators) can manage the Route resource through the Route controller, and ordinary users manage their routes using the Myroute controller. I want both controllers to use RESTful routing, but I am having problems with the form_for function in the "edit" view for the controller "Myroute".

My form tag for "editing" the Myroute controller view currently:

<% form_for @route, :url => { :id => @route.id }, :html => { :method => :put } do |f| %> 

Which solves the following:

<form action="/myroutes/44/edit" class="edit_route" id="edit_route_44" method="post">

This is not true, since the form action must go to the create method, and the edit method only processes GET requests. From what I can say, by looking at the HTML generated from the Route views, the form should make a PUT request to "/ myroutes / 44"

How do I write a form_for tag to use RESTful routing to make PUT reqest for a controller update method that does not match the model?

+3
source share
2 answers

What about:

<% form_for @route, :url => {:action => 'update', :id => @route.id },
 :html => { :method => :put } do |f| %>
+8
source

it turns out that this also works:

  <% form_for @route, :url => myroute_path(@route) do |f| %>
+1
source

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


All Articles