2016-02-24 84 views
0

我設計了認證設置和工作,並且在註冊或登錄後立即將用戶重定向到用戶自己的顯示頁面。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 --> 

任何幫助關於如何解決這個錯誤將不勝感激。

+0

爲什麼要使用'update_account'而不是更標準的'update'?據我可以告訴他們會做同樣的事情... – PJSCopeland

回答

1

瀏覽器正在發送PATCH /users/update_account,看起來像你想要的。然而,在rake routes相匹配的第一行是:

PATCH /users/:id(.:format) users#update 

...這是你想要什麼。

你在config/routes.rb中有兩次resources :users;你應該只有一次。試試這個:

resources :users do 
    collection do 
    patch 'update_account' 
    end 
end 

這將產生users所有標準路線,爲您的頂線目前的做法,但也/users/update_account - 我認爲之前所有標準的人。

+0

感謝谷輪...你建議我把所有的update_account的回來只是'更新'第一? – BB500

+0

好吧,現在我再看一遍,還有一個區別,即'update_account'不帶':id'參數,只是加載當前用戶 - 這很有道理,所以我認爲它很好。 – PJSCopeland

+1

但是,如果您希望某些用戶能夠編輯其他用戶的帳戶,我會在'#update'中完成大部分工作。那麼'#update_account'就會設置'@user = current_user'並將'update'作爲一個方法而不是一個動作來調用(而'#update'作爲* action *會有一個'before_filter'來通過ID找到用戶) 。 – PJSCopeland

相關問題