我設計了認證設置和工作,並且在註冊或登錄後立即將用戶重定向到用戶自己的顯示頁面。Rails 4在用戶操作中設計路由錯誤
myapp.com/users/1和 myapp.com/users/1/edit
,因爲我已經設置一個用戶:用戶展示頁面和用戶編輯頁面可以成功地在瀏覽器中訪問控制器 - 現在我需要簡單地採取在通常發生在myapp.com/users/edit
edit_user_registration GET /users/edit(.:format) devise/registrations#edit
... - 實質上是修改用戶帳戶的功能設計出的動作,我需要使它發生,而不是在用戶/:編號/編輯。當我訪問我的用戶的用戶/ 1 /編輯頁面並更新電子郵件或密碼並單擊'更新'時... URL更改爲myapp.com/users/update_account,我得到以下錯誤:
Unknown action
The action 'update' could not be found for UsersController
的config/routes.rb中
devise_for :users
resources :users
resources :users, only: [:edit] do
collection do
patch 'update'
end
end
root 'static_pages#home'
get '/users/:id', :to => 'users#show'
get '/users/:id/edit', :to => 'users#edit'
get '/help', to: 'static_pages#help'
get '/about', to: 'static_pages#about'
耙路線提供:
Prefix Verb URI Pattern Controller#Action
new_user_session GET /users/sign_in(.:format) devise/sessions#new
user_session POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
user_password POST /users/password(.:format) devise/passwords#create
new_user_password GET /users/password/new(.:format) devise/passwords#new
edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
PATCH /users/password(.:format) devise/passwords#update
PUT /users/password(.:format) devise/passwords#update
cancel_user_registration GET /users/cancel(.:format) devise/registrations#cancel
user_registration POST /users(.:format) devise/registrations#create
new_user_registration GET /users/sign_up(.:format) devise/registrations#new
edit_user_registration GET /users/edit(.:format) devise/registrations#edit
PATCH /users(.:format) devise/registrations#update
PUT /users(.:format) devise/registrations#update
DELETE /users(.:format) devise/registrations#destroy
users PATCH /users/update(.:format) users#update
GET /users(.:format) users#index
POST /users(.:format) users#create
new_user GET /users/new(.:format) users#new
edit_user GET /users/:id/edit(.:format) users#edit
user GET /users/:id(.:format) users#show
PATCH /users/:id(.:format) users#update
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy
root GET / static_pages#home
help GET /help(.:format) static_pages#help
我的頭鏈接到用戶配置文件和帳戶設置頁面:
組的意見/佈局/ _navigation_links.html.erb
<% if user_signed_in? %>
<div class="dropdown"">
<li class="dropdown-toggle" id="dropdownMenu1" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">
<%= current_user.email %>
<span class="caret"></span>
</li>
<ul class="dropdown-menu" aria-labelledby="dropdownMenu1">
<li><%= link_to 'My Profile', user_path %></li>
<li><%= link_to 'Account Settings', edit_user_path %></li>
<li><a href="#">Edit Profile</a></li>
<li role="separator" class="divider"></li>
<li><%= link_to 'Log out', destroy_user_session_path, method: :delete %></li>
</ul>
</div>
<% elsif %>
<li><%= link_to "Sign Up", new_user_registration_path %></li>
<li><%= link_to "Sign In", new_user_session_path %></li>
<% end %>
我的用戶控制器:
class UsersController < ApplicationController
before_filter :authenticate_user!
def edit
@user = current_user
end
def update
@user = User.find(current_user.id)
if @user.update_with_password(user_params)
# Sign in the user by passing validation in case their password changed
sign_in @user, :bypass => true
redirect_to @user
else
render "edit"
end
end
private
def user_params
params.require(:user).permit(:email, :password, :password_confirmation, :current_password)
end
end
我的視圖/用戶/ Edit.html.erb
<% provide(:title, "Edit user") %>
<div class="container middle">
<!-- SideBar NEED TO REFACTOR TO A USER LAYOUT FILE -->
<div class="sidebar col-md-3">
</div>
<div class="main-content col-md-9">
<div class="main-breadcrumb">
Some Content
</div>
<div class="section_header">
<h3>Edit Account</h3>
</div>
<div class="row-fluid">
<div class="col-md-6">
<%= form_for(@user, :url => { :action => "update" }) do |f| %>
<div class="field">
<%= f.label :email %><br />
<%= f.email_field :email, autofocus: true %>
</div>
<div class="field">
<%= f.label :password %> <i>(leave blank if you don't want to change it)</i><br />
<%= f.password_field :password, autocomplete: "off" %>
</div>
<div class="field">
<%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation, autocomplete: "off" %>
</div>
<div class="field">
<%= f.label :current_password %> <i>(we need your current password to confirm your changes)</i><br />
<%= f.password_field :current_password, autocomplete: "off" %>
</div>
<div class="actions">
<%= f.submit "Update" %>
</div>
<% end %>
<h3>Cancel my account</h3>
# I removed the devise 'cancel account' functionality for now
<%= link_to "Back", :back %>
</div>
</div>
</div><!-- end main content section -->
</div><!-- end Container -->
任何幫助關於如何解決這個錯誤將不勝感激。
爲什麼要使用'update_account'而不是更標準的'update'?據我可以告訴他們會做同樣的事情... – PJSCopeland