Rails 4设计用户操作的路由错误

时间:2016-02-24 02:05:53

标签: ruby-on-rails ruby-on-rails-4 devise routing rails-routing

我设置了Devise身份验证设置并且正常工作,并且我在注册或登录后立即将用户重定向到用户自己的显示页面。用户显示页面和用户编辑页面可以在浏览器中成功访问:

myapp.com/users/1和 myapp.com/users/1/edit

因为我已经设置了用户控制器 - 现在我需要简单地采取通常在myapp.com/users/edit上发布的设置

edit_user_registration GET /users/edit(.:format)  devise/registrations#edit

...这实际上是编辑用户帐户的功能,我需要让它出现在users /:id / edit。当我访问用户的users / 1 / edit页面并更新电子邮件或密码并点击“更新”...网址更改为myapp.com/users/update_account时出现以下错误:

Unknown action
The action 'update' could not be found for UsersController

配置/ 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'

rake routes提供:

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 -->

如何解决此错误的任何帮助将不胜感激。

1 个答案:

答案 0 :(得分: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 - 我认为之前所有标准路线。