Rails没有找到我的路线之一[补丁]

时间:2016-08-04 20:55:45

标签: ruby-on-rails ruby routes config

我有一个用户编辑表单,每当我尝试提交表单时,都会收到以下错误:

    ActionController::RoutingError (No route matches [PATCH] "/users/1/edit")

这是我的用户控制器:

class UsersController < ApplicationController
  before_action :logged_in_user, only: [:edit, :update]
  def new
    @user = User.new
  end

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

  def create
    @user = User.new(user_params)
    if @user.save
      flash[:success] = 'Signup Successful!'
      log_in @user
      redirect_to @user
    else
      render 'new'
    end
  end

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

  def update
    @user = User.find(params[:id])
    if @user.update_attributes(user_params)
      flash[:success] = "Profile Updated!"
      redirect_to @user
    else
      render 'edit'
    end

  end

  private

    def user_params
      params.require(:user).permit(:name, :email, :password,
                                 :password_confirmation)
    end

    def logged_in_user
      if !(logged_in?)
        flash[:danger] = "Please log in"
        redirect_to login_url
      end
    end

  end

我的routes.rb文件:

Rails.application.routes.draw do
  get 'sessions/new'
  root 'static_pages#home'
  get '/help', to: 'static_pages#help'
  get '/about', to: 'static_pages#about'
  get '/signup', to: 'users#new'
  post '/signup', to: 'users#create'
  get '/login', to: 'sessions#new'
  post '/login', to: 'sessions#create'
  delete '/logout', to: 'sessions#destroy'
  resources :users
end

我还运行了rake路由,以确保我拥有所需的所有路由,并且我找到了用户的PATCH路由:

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

我试图找到这个错误两个小时,但无济于事(这就是我在这里发布的原因)

这是我的表格部分:

<%= form_for(@user, url: url) do |f| %>
    <%= render 'shared/error_messages', object: f.object %>

    <%= f.label :name %>
    <%= f.text_field :name, class: 'form-control' %>

    <%= f.label :email %>
    <%= f.email_field :email, class: 'form-control' %>

    <%= f.label :password %>
    <%= f.password_field :password, class: 'form-control' %>

    <%= f.label :password_confirmation, "Confirmation" %>
    <%= f.password_field :password_confirmation, class: 'form-control' %>

    <%= f.submit yield(:button_text), class: "btn btn-primary" %>
<% end %>

这是我的edit.html.erb

<%provide(:title, 'Edit')%>
<%provide(:button_text, 'Save Changes')%>
<h1>Edit</h1>

<div class = 'row'>
    <div class = 'col-md-6 col-md-offset-3' >
        <%=render 'form', url: edit_user_path(@user)%>
        <div class = "gravatar-edit">
            <%=gravatar_for @user%>
            <a href = "http://gravatar.com/emails", target = '_blank'>Change</a>
        </div>
    </div>
</div>
  • 提前致谢

2 个答案:

答案 0 :(得分:0)

您尝试访问的路由的REST动词是GET,因为通常是&#34;编辑&#34; action用于呈现表单以编辑对象(在本例中为用户)。如果你想使用PATCH动词更新用户,你应该在你的情况下PATCH到&#34; / users / 1&#34;

答案 1 :(得分:0)

好的,我想我已经找到了问题。

所以基本上,正如Max Pleaner指出的那样,我应该[PATCH] "/users/1/edit"时调用它PATCH /users/1.所以,我在edit.html.erb文件中更改了我的URL参数

从:

<%=render 'form', url: edit_user_path(@user)%>

为:

<%=render 'form', url: user_path(@user)%>