[英]Rails: Devise - User edit is not generating the HTML I'm expecting
這是我的users / edit.html / erb中的內容
<%= form_for(resource, :as => resource_name, :url => user_path(resource_name), :html => { :method => :put }) do |f| %>
我給用戶的路線:
edit_user GET /users/:id/edit(.:format) users#edit
user GET /users/:id(.:format) users#show
PUT /users/:id(.:format) users#update
method => put,所以使用路由信息,我期望HTML操作為/ users /:id
這是users / edit.html.erb,我希望from的HTML從id為edit_user_id,而class為edit_user
顯示表單時,我實際上是用HTML(使用Firebug)獲得的:
<form id="new_user" class="new_user" method="post" action="/users/user" accept-charset="UTF-8">
HTML不應該使用HTML(假設用戶ID為1):
<form id="edit_user_1" class="edit_user" method="post" action="/users/1" accept-charset="UTF-8">
在我的routes.rb文件中,我有:
resources :users, only: [:show, :edit, :update]
devise_for :users, :controllers => { :registrations => 'registrations', :confirmations => 'confirmations' }, :path => '', :path_names => { :sign_in => "login", :sign_up => "request_invite" }
我確實將用戶幫助更改為:
def resource
@resource ||= User.find(params[:id])
end
因此,根據API文檔,這也應該將/ users /:id作為動作,因此HTML現在應如下所示:
<form id="edit_user_1" class="edit_user" method="post" action="/users/1" accept-charset="UTF-8">
相反,它是:
<form id="edit_user" class="edit_user" method="post" action="/users/user" accept-charset="UTF-8">
因此,當用戶控制器開始執行操作時,將命中以下代碼:
def update
@user = User.find(params[:id])
並且它認為(在表單的操作之前),我傳遞的id是單詞user,因此我希望在單擊update時收到以下錯誤消息:
找不到ID =用戶的用戶
這正是我得到的錯誤。 還有其他見解嗎?
在users_helper.rb中:
def resource
@resource ||= User.find(params[:id])
end
並在users / edit.html.erb中
resource_name,:html => {:method =>:put})做| f | %>簡短的答案是:它取決於您視圖中資源的價值。
http://apidock.com/rails/v3.2.3/ActionView/Helpers/FormHelper/apply_form_for_options ! 表單的ID和類由dom_id和dom_class幫助程序生成,並且僅在以下情況下,“ action”前綴為'edit'而不是'new'。
object.respond_to?(:persisted?) && object.persisted?
對於activerecord對象,當且僅當該對象已保存到db時,此結果才為true。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.