Rails 5没有使用form_for方法“patch”,默认为“get”

时间:2016-10-05 22:04:33

标签: ruby-on-rails

我需要更新用户的信息,我正在使用form_for。当我导航到http://localhost:3000/users/1/edit时,我的错误是找不到[GET]“/ users / 1 / edit”的路线。为什么Rails使用GET html动词?

我的users_controller,路线和视图:

def edit
  @user = User.find(params[:id])
end

--------

resource :users, only: [:new, :create, :edit, :update]

    Prefix Verb  URI Pattern           Controller#Action
     users POST  /users(.:format)      users#create
 new_users GET   /users/new(.:format)  users#new
edit_users GET   /users/edit(.:format) users#edit
           PATCH /users(.:format)      users#update
           PUT   /users(.:format)      users#update
--------

<%= form_for(@user, url: edit_users_path(@user), html: {method: "patch"}) do |f| %>
  Username: <%= f.text_field :username %><br>
  Email:    <%= f.text_field :email %><br>
  Password: <%= f.text_field :password %><br>
            <%= f.submit %>
<% end %>

1 个答案:

答案 0 :(得分:0)

routes.rb中,将resource更改为resources

    resources :users, only: [:new, :create, :edit, :update]

目前,您的路线并不接受任何参数。您的路径转到/users/edit,而不是/users/:id/edit

     Prefix Verb   URI       Pattern           Controller#Action
     edit_users    GET   /users/edit(.:format)    users#edit