如何更新rails的方法?

时间:2009-12-05 07:04:05

标签: ruby-on-rails rest routes

edit.html.erb文件上创建了scaffold,我没有看到任何代码指定PUT方法。为什么帖子表单使用PUT方法调用更新操作。

3 个答案:

答案 0 :(得分:4)

form_for函数会最好地猜测发送表单的位置:

以下面的代码为例:

<% form_for @user do |form| %>
  1. 如果@user是新记录,它会向POST发送/users个请求
  2. 如果@user是现有记录,则会向PUT发送/users/12请求(如果12 = @ user.id)

答案 1 :(得分:1)

运行时可以看到编辑方法

rake routes


new_account  GET    /account/new(.:format)  {:action=>"new", :controller=>"accounts"}
edit_account GET    /account/edit(.:format) {:action=>"edit", :controller=>"accounts"}
account      GET    /account(.:format)      {:action=>"show", :controller=>"accounts"}
             PUT    /account(.:format)      {:action=>"update", :controller=>"accounts"} //this is your method needed
             DELETE /account(.:format)      {:action=>"destroy", :controller=>"accounts"}
             POST   /account(.:format)      {:action=>"create", :controller=>"accounts"}

<% form_for @user do |form| %> can be <% form_for :user, :url => user_url(:user), :method => :put do |form| %>

答案 2 :(得分:0)

处理更新的标准Rails方法是使用一个编辑模板(您的edit.html.erb),该模板生成一个由用户填写并随后作为HTTP PUT请求提交的表单。您的Rails应用程序应该具有正在更新的资源的模型,其控制器具有“更新”操作以接受表单参数。

如果您想查看PUT请求中发送的数据,可以使用Firebug

相关问题