Rails,在尝试使用put http方法的表单中遇到麻烦

2 ruby forms rest ruby-on-rails

<% form_ tag user_path(@user), :method => :put do %>
Run Code Online (Sandbox Code Playgroud)

这是我的表单,所以我希望它访问我的UsersController的更新方法,我设置了map.resources:users,以及生成的RESTful路径:

users     GET    /users(.:format)          {:action=>"index", :controller=>"users"}          
          POST   /users(.:format)          {:action=>"create",:controller=>"users"}
new_ user GET    /users/new(.:format)      {:action=>"new", :controller=>"users"}
edit_user GET    /users/:id/edit(.:format) {:action=>"edit", :controller=>"users"}
user      GET    /users/:id(.:format)      {:action=>"show", :controller=>"users"}
          PUT    /users/:id(.:format)      {:action=>"update", :controller=>"users"}
          DELETE /users/:id(.:format)      {:action=>"destroy", :controller=>"users"}
Run Code Online (Sandbox Code Playgroud)

所以我尝试使用PUT HTTP方法发送到user_path(@user),它返回:

Unknown action

No action responded to 1. Actions: create, destroy, edit, index, logged?, new, show and update
Run Code Online (Sandbox Code Playgroud)

显然我不知道如何做这项工作,所以提前谢谢.

小智 5

如果您正在使用RESTful资源(并且您应该使用),请尝试使用form_fornot form_tag...使用如下完整设置:

<% form_for :user, @user, :url=>user_path(@user), :html=>{:method=>:put} do |f| %>

  #this scopes the form elements to the @user object, eg.
  <%= f.text_field :first_name %>

<% end %>
Run Code Online (Sandbox Code Playgroud)

查看API文档了解更多信息.