找不到'id'= show的用户

时间:2015-06-16 03:03:34

标签: ruby-on-rails ruby devise

今天为您提供Rails / Devise问题。我正在处理网站的用户个人资料,我遇到了这个错误

ActiveRecord :: RecordNotFound位于/ users / show

找不到'id'= show

的用户

我不确定究竟发生了什么,因为之前我还没有碰到过这样的事情。以下是一些需要查看的必要文件 -

users_controller.rb

class UsersController < ApplicationController
  before_action :authenticate_user!, except: [:show]

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

  def update
    if current_user.update_attributes(user_params)
      flash[:notice] = "User information updated"
      redirect_to edit_user_registration_path
    else
      flash[:error] = "Invalid user information"
      redirect_to edit_user_registration_path
    end
  end

  private

  def user_params
    params.require(:user).permit(:name, :avatar)
  end
end

的routes.rb

Rails.application.routes.draw do

  devise_for :users
  resources :users, only: [:show, :update]

  post :incoming, to: 'incoming#create'

  resources :topics do
    resources :bookmarks, except: [:index]
  end

  resources :bookmarks, except: [:index] do
    resources :likes, only: [:create, :destroy]
  end

  get 'about' => 'welcome#about'

  root to: 'welcome#index'
end

user.rb

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable, :confirmable

  mount_uploader :avatar, AvatarUploader
  has_many :topics, dependent: :destroy
  has_many :bookmarks, dependent: :destroy
  has_many :likes, dependent: :destroy

  validates :name, presence: true

  def admin?
    role == 'admin'
  end

  def moderator?
    role == 'moderator'
  end

  def member?
    role == 'member'
  end

  def liked(bookmark)
    likes.where(bookmark_id: bookmark.id).first
  end
end

1 个答案:

答案 0 :(得分:3)

show操作取决于查找id。由于您访问了网址/users/show,因此您认为自己正在尝试使用id show来查找用户。相反,您应该转到/users/1之类的网址来触发show操作。

The Rails guide on routing有一个部分指定GET /photos/:id地图photos#show