没有路由匹配,但我使用routes.rb中的资源

时间:2017-08-06 17:03:15

标签: ruby-on-rails ruby-on-rails-5

在routes.rb中我有

Rails.application.routes.draw do

  get 'password_resets/new'

  get 'password_resets/edit'
  get 'sessions/new'
  get 'account_activation/edit'

  root                     'static_pages#home'
  get       'help'    => 'static_pages#help'
  get       'sign_up' => 'user#new'
  get       'login'   => 'sessions#new'
  post      'login'   => 'sessions#create'
  delete    'logout'  => 'sessions#destroy'
  post      'sign_up',  to: 'user#create'
  resources :user
  resources :account_activation, only: [:edit]
  resources :password_resets, only: [:new, :create, :edit, :update]
  resources :purchases, only: [:create, :destroy]
  # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end

我在user_controller中定义

class UserController < ApplicationController

  before_action :logged_in_user, only: [:show, :edit, :update]
  before_action :correct_user, only: [:show, :edit, :update]


  def show
    @user = User.find_by(id: params[:id])
    @purchases = @user.purchases.paginate(page: params[:page])
    @purchase = current_user.purchases.build
  end

  def index
    if logged_in?
      redirect_to root_url
    else
      redirect_to sign_up_path
    end
  end

  def new
    @user = User.new
  end

  def create
    @user = User.new(user_params)
    if @user.save
      @user.send_account_activation_email
      flash[:info] = 'check'
      redirect_to root_url(@user)
    else
      render 'new'
    end
  end

  def edit
  end

  def chart
  end

  def update
    if @user.update_attributes(user_params)
      flash[:success] = 'updated'
      redirect_to @user
    else
        render 'edit'
    end
  end

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

    def correct_user
      @user = User.find_by(id: params[:id])
      redirect_to(root_url) unless current_user?(@user)
    end
end

他们都有一个视图,但当我输入localhost:300 / user /(ID)/浏览器栏中的图表告诉我没有找到路由匹配,但用户/(ID)/编辑工作

当我使用rake路线时 图表不在路线中显示

我看到MHartl ruby​​onrails教程屏幕演员 他做了这样的一切 问题在哪里?!

1 个答案:

答案 0 :(得分:2)

resources :user do
  member do
    get :chart
  end
end

定义图表操作 阅读rails指南中的routes并查看membercollection

resources :user仅创建7条默认路由,检查2.2 this