具有两个不同控制器和 RESTful 路由的资源的 form_for 标记

发布于 2024-08-05 23:27:04 字数 694 浏览 2 评论 0原文

我有一个带有“Route”资源的 Rails 应用程序和一个“Route”控制器(不要与 Rails 路由混淆)。我已将其设置为站点管理员(且仅管理员)可以通过“路线”控制器管理“路线”资源,而普通用户则使用“Myroute”控制器管理其路线。我希望两个控制器都使用 RESTful 路由,但我在“Myroute”控制器的“编辑”视图中使用 form_for 函数时遇到问题。

我的“Myroute”控制器的“编辑”视图的表单标记当前为:

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

解析为以下内容:

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

这是不正确的,因为表单的操作应该转到“创建”方法,而“编辑”方法仅处理获取请求。通过查看“Route”视图生成的 HTML,我可以看出,表单应该向“/myroutes/44”发出 PUT 请求

如何编写 form_for 标记,以便它使用 RESTful 路由对与模型不同的控制器的“更新”方法发出 PUT 请求?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你对谁都笑 2024-08-12 23:27:04

怎么样:

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

How about:

<% form_for @route, :url => {:action => 'update', :id => @route.id },
 :html => { :method => :put } do |f| %>
缱倦旧时光 2024-08-12 23:27:04

事实证明这也有效:

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

turns out this also works:

  <% form_for @route, :url => myroute_path(@route) do |f| %>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文