Rails 3.0.10,客户路线,post 和 form_for 标签

发布于 2024-12-18 06:32:02 字数 1308 浏览 8 评论 0原文

我的路线中有这个:

resources :events do
  collection do
    post 'moderate'
  end
end

Rake 路线告诉我:

moderate_events POST /events/moderate(.:format) {:controller=>"events", :action=>"moderate"}

我有一个“管理”控制器,它只是列出需要审核的事件:

@modevents = Event.where('moderated <> 1')

到目前为止一切顺利,所有尚未审核的事件都可以显示在视图中:

<%- @modevents.each do |me| -%>
  Display Stuff here
<%- end -%>

我想要将一个表单放入更新调节值的循环中,但对于我的一生,我无法弄清楚要在 form_for 中放入什么 - 我已经尝试过:

<%= form_for me, :url => moderate_events_path do |f| %>
  <%= f.submit %>
<% end %>

返回的 html 是:

<form accept-charset="UTF-8" action="/events/moderate" class="edit_event" id="edit_event_1" method="post">
  <div style="margin:0;padding:0;display:inline">
  <input name="utf8" type="hidden" value="&#x2713;" />
  <input name="_method" type="hidden" value="put" />

当我单击“提交”按钮时,我得到出现如下错误:

Couldn't find Event with ID=moderate

解决办法非常简单,在路由中将“post”更改为“put”:

resources :events do
  collection do
    put 'moderate'
  end
end

现在它可以正常工作了。更新,甚至自定义更新都是“放置”功能。

I have this in my routes:

resources :events do
  collection do
    post 'moderate'
  end
end

Rake routes tells me:

moderate_events POST /events/moderate(.:format) {:controller=>"events", :action=>"moderate"}

I have an "administration" controller that simply lists Events that need moderating:

@modevents = Event.where('moderated <> 1')

So far so good, all the events that haven't been moderated can be displayed in the view:

<%- @modevents.each do |me| -%>
  Display Stuff here
<%- end -%>

I want to put a form in the loop that updated the moderated value but for the life of me I can't work out what to put in the form_for - I have tried:

<%= form_for me, :url => moderate_events_path do |f| %>
  <%= f.submit %>
<% end %>

The html returned is:

<form accept-charset="UTF-8" action="/events/moderate" class="edit_event" id="edit_event_1" method="post">
  <div style="margin:0;padding:0;display:inline">
  <input name="utf8" type="hidden" value="✓" />
  <input name="_method" type="hidden" value="put" />

When I click the "Submit" button I get the following error:

Couldn't find Event with ID=moderate

The solution is very simple, in routes change "post" to "put":

resources :events do
  collection do
    put 'moderate'
  end
end

And now it works as it should. Updates, even custom ones are "put" functions.

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

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

发布评论

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

评论(2

一个人练习一个人 2024-12-25 06:32:02

答案实际上就在上面底部的文字中,但可能并不明显。如果您要更新内容,您应该使用“put”而不是“post”。

The answer is actually in the text above at the bottom but perhaps not obvious. If you are updating stuff you should be using "put" not post.

梦言归人 2024-12-25 06:32:02

您还可以通过指定:来使用 POST,

<%= form_for me, :url => moderate_events_path, :method => :post do |f| %>

但如前所述,需要区分更新和创建。 Rails 中的标准是 update==put,create==post。

You can also use POST by specifying:

<%= form_for me, :url => moderate_events_path, :method => :post do |f| %>

but as said before, the distinction needs to be made between updating and creating. The standard in rails is update==put, and create==post.

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