rails中的管理功能:错误:找不到具有'id'=的用户

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了rails中的管理功能:错误:找不到具有'id'=的用户相关的知识,希望对你有一定的参考价值。

我正在尝试编写一个更新功能,它允许在用户列表中选择用户并更新该用户以使其成为管理员。控制器中的重要功能应该是def change_admin。

谢谢你的帮助!

我尝试了几个选项,但我遇到了这个错误:

Couldn't find User with 'id'=

我的控制器:

class UsersController < ApplicationController
  before_action :logged_in_user, only: [:index, :edit, :update, :destroy]
  before_action :correct_user,   only: [:edit, :update]
  before_action :admin_user,     only: :destroy


  def new
    @user = User.new
  end

  def index
    @users = User.where(activated: true).paginate(page: params[:page])
  end

  def show
    @user = User.find(params[:id])
    redirect_to root_url and return unless @user.activated
  end

  def edit
  end

  def destroy
    User.find(params[:id]).destroy
    flash[:success] = "Der Nutzer wurde gelöscht"
    redirect_to users_url
  end

  def update
    @user = User.find(params[:id])
    if @user.update_attributes(user_params)
      redirect_to root_path
    else
      render 'edit'
    end
  end

  def create
    @user = User.new(user_params)
    if @user.save
      @user.send_activation_email
      flash[:info] = "Bitte öffnen Sie Ihr E-Mail Postfach, um den Account zu aktivieren."
      redirect_to root_url
    else
      render 'new'
    end
  end

  def admin
    @users = User.where(activated: true).paginate(page: params[:page])
  end

  def change_admin
    @user = User.find(params[:id])
    @user.update_attribute(:admin,true)
    respond_to do |format|
      format.html { redirect_to admin_path }
    end
  end

  # Before filters

  # Confirms the correct user.
    def correct_user
      @user = User.find(params[:id])
      redirect_to(root_url) unless current_user?(@user)
    end

  private

    def user_params
        params.require(:user).permit(:name, :email, :password, :password_confirmation,
                                     :mat_number, :ects, :grade_avg, :enrolled, :matched_institute)
    end

    def admin_user
      redirect_to(root_url) unless current_user.admin?
    end

end

我也试图删除user.find行,但这给了我另一个错误:

undefined method `update_attribute' for nil:NilClass

我的路线档案:

Rails.application.routes.draw do

  resources :preferences
  resources :institutes
  get 'password_resets/new'

  get 'password_resets/edit'
  get '/users_show', to: 'users#show'
  get '/users/new'
  root 'static_pages#home'
  get '/home', to: 'static_pages#home'
  get '/help', to: 'static_pages#help'
  get '/about', to: 'static_pages#about'
  get '/contact', to: 'static_pages#contact'
  get '/matching', to: 'static_pages#matching'

  get '/signup', to: 'users#new'
  post '/signup',  to: 'users#create'
  get '/admin', to: 'users#admin'
  post '/change_admin', :to => 'users#change_admin', as: 'change_admin'

  get '/performance_show', to: 'users#performance_show'
  get '/performance_update', to: 'users#performance_update'

  post 'preferences/create_all', :to => 'preferences#create_all'

  get '/login', to: 'sessions#new'
  post '/login', to: 'sessions#create'
  delete '/logout', to: 'sessions#destroy'

  resources :users
  resources :account_activations, only: [:edit]
  resources :password_resets,     only: [:new, :create, :edit, :update]

  post 'preferences/delete_matching', :to => 'preferences#delete_matching'
  post 'preferences/read_and_show_ofv', :to => 'preferences#read_and_show_ofv'
  post 'preferences/read_matching', :to => 'preferences#read_matching'
  post 'preferences/optimize_matching', :to => 'preferences#optimize_matching'

  post 'preferences/optimize', to: 'preferences#optimize'

end
答案

你的路线状况非常糟糕。你有大量的重复以及缺少:id段的路由使它们工作:

get '/users_show', to: 'users#show'
get '/users/new'
post '/change_admin', :to => 'users#change_admin', as: 'change_admin'

resources :users已宣布适当的展示路线为GET /users/:id和新的/users/new

要添加其他RESTful路由,您应该将块传递给resources

resources :users do
  # this should be PATCH not POST
  patch :change_admin 
end

这将创建/users/:id/change_admin的正确路线。

你也在许多地方使用错误的HTTP动词,例如get '/performance_update', to: 'users#performance_update'。切勿将GET用于创建或更改资源的操作,因为调用最终会出现在浏览器历史记录中并且可能会被缓存。

在路线的路径上添加updatechangecreate应该是一个大红旗,你做错了。

我建议你在添加更多残骸之前彻底阅读the guides

以上是关于rails中的管理功能:错误:找不到具有'id'=的用户的主要内容,如果未能解决你的问题,请参考以下文章

Rails 3 应用程序中的 Sass 导入错误 - “找不到或无法读取要导入的文件:指南针”

Rails:用按钮隐藏记录,找不到ID错误的记录

Rails 4找不到关联has_many,通过:关系错误

Rails 路线有时找不到错误

如何解决 Rails 错误:找不到图像 - ../digest/md5.bundle

Rails 3.1 和 Coffeescript 出现“找不到变量”错误