Rails 3.0.10,客户路线,post 和 form_for 标签
我的路线中有这个:
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="✓" />
<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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
答案实际上就在上面底部的文字中,但可能并不明显。如果您要更新内容,您应该使用“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.
您还可以通过指定:来使用 POST,
但如前所述,需要区分更新和创建。 Rails 中的标准是 update==put,create==post。
You can also use POST by specifying:
but as said before, the distinction needs to be made between updating and creating. The standard in rails is update==put, and create==post.