Rails 3.0.10,客户路线,post和form_for标签 [英] Rails 3.0.10, customer routes, post and form_for tag

查看:75
本文介绍了Rails 3.0.10,客户路线,post和form_for标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的路线上有这个

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

耙道告诉我:

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 -%>

我想在循环中添加一个表单来更新已调整的值,但是为了我的生命,我无法计算出要放在form_for中的内容-我已经尝试过:

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 %>

返回的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" />

当我单击提交按钮时,出现以下错误:

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

Couldn't find Event with ID=moderate

解决方案非常简单,在路由中将 post更改为 put:

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

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

现在它可以正常工作了。更新,甚至是自定义更新也是 put函数。

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

推荐答案

您还可以通过指定以下内容来使用POST:

You can also use POST by specifying:

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

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

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

这篇关于Rails 3.0.10,客户路线,post和form_for标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆