3

I have a Rails app with a "Route" resource, and a "Route" controller (not to be confused w/ Rails routes). I've set it up so that the site admins (and only the admins) can manage the "Route" resource through the "Route" controller, while regular users manage their routes with a "Myroute" controller. I want both controllers to utilize RESTful routing, but I'm having trouble with the form_for function in the "edit" view for the "Myroute" controller.

My form tag for the "edit" view of the "Myroute" controller is currently:

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

Which resolves to the following:

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

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

How do I write a form_for tag so that it uses RESTful routing to make a PUT reqest to the "update" method of a controller that is not the same as the model?

1
  • Great question! This is really useful when dealing with admin/* routes because the convention of configuration stuff starts to break down there. Hopefully, they'll make this a little more smarter in 3.0
    – aarona
    May 28, 2010 at 7:13

2 Answers 2

8

How about:

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

turns out this also works:

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.