如何在 Rails 中使用 RESTful 路由创建删除表单。

发布于 2024-11-28 22:29:05 字数 1161 浏览 2 评论 0原文

我正在尝试创建一个表单来确认控制器上的 destroy 方法。在我的路线中,我有:

resources :leagues do
  get 'delete', :on => :member
end

在我的delete.html.erb中,我有以下内容:

<% form_for current_league, :html => {:method => :delete} do |form| %>
  <%= form.submit 'Yes' %>
  <%= form.submit 'No', :name => 'cancel' %>
<% end %>

current_league 是由以下定义的辅助函数:

def current_league
  @current_league ||= League.find_by_id(params[:id])
end

所以问题是生成的表单仅编辑联赛模型,如表单方法所示=“帖子”。

<form accept-charset="UTF-8" action="/leagues/1" class="edit_league" id="edit_league_1" method="post">
  <div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="&#x2713;" />
    <input name="_method" type="hidden" value="delete" />
    <input name="authenticity_token" type="hidden" value="abcdefg" />
  </div>  
  <input id="league_submit" name="commit" type="submit" value="Yes" /> 
  <input id="league_submit" name="cancel" type="submit" value="No" /> 
</form> 

我该如何解决这个问题?

I am trying to create a form that serves as confirmation for the destroy method on a controller. In my routes I have:

resources :leagues do
  get 'delete', :on => :member
end

In my delete.html.erb, I have the following for:

<% form_for current_league, :html => {:method => :delete} do |form| %>
  <%= form.submit 'Yes' %>
  <%= form.submit 'No', :name => 'cancel' %>
<% end %>

current_league is a helper function defined by:

def current_league
  @current_league ||= League.find_by_id(params[:id])
end

So the problem is that the form that is generated only edits the league model, as seen by the form method="post".

<form accept-charset="UTF-8" action="/leagues/1" class="edit_league" id="edit_league_1" method="post">
  <div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="✓" />
    <input name="_method" type="hidden" value="delete" />
    <input name="authenticity_token" type="hidden" value="abcdefg" />
  </div>  
  <input id="league_submit" name="commit" type="submit" value="Yes" /> 
  <input id="league_submit" name="cancel" type="submit" value="No" /> 
</form> 

How can I fix this?

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

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

发布评论

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

评论(1

苦妄 2024-12-05 22:29:05

我认为这已经行得通了。如 Rails 指南 http://guides.rubyonrails.org/form_helpers.html#how-do-forms-with-patch-put-or-delete-methods-work-questionmark

但是,大多数浏览器在提交表单时不支持“GET”和“POST”以外的方法。

Rails 通过使用名为“_method”的隐藏输入来模拟 POST 上的其他方法来解决此问题,该输入被设置为反映所需的方法:

正如您在输出表单中看到的那样

<form accept-charset="UTF-8" action="/leagues/1" class="edit_league" id="edit_league_1" method="post">
    ....
    <input name="_method" type="hidden" value="delete" />
    ....
</form>

,当读取此变量时,rails 知道这是一个DELETE 方法,而不是 POST 方法。即使它本身是一个 POST 形式。

I think that will already work. As in rails guide http://guides.rubyonrails.org/form_helpers.html#how-do-forms-with-patch-put-or-delete-methods-work-questionmark

However, most browsers don’t support methods other than “GET” and “POST” when it comes to submitting forms.

Rails works`around this issue by emulating other methods over POST with a hidden input named "_method", which is set to reflect the desired method:

as you see in the output form there is

<form accept-charset="UTF-8" action="/leagues/1" class="edit_league" id="edit_league_1" method="post">
    ....
    <input name="_method" type="hidden" value="delete" />
    ....
</form>

When reading this variable, rails understood that this is a DELETE method, not a POST method. even though the form it self is a POST.

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