我正在尝试在Rails 4中制作一个应用程序。
我试图遵循CyberDude在这篇文章中的建议:
使用 Rolify 定义角色
我卡在用户/视图文件中为用户编制索引的部分。
我收到一个错误,说:
ActiveRecord::RecordNotFound in UsersController#show
Couldn't find User with 'id'=index
Extracted source (around line #69):
67
68
69
70
71
72
private
def set_user
@user = User.find(params[:id])
end
def user_params
我无法理解此错误消息。其他具有类似消息的人似乎在需要 :id 的其他操作(如 #show)中出现问题。我认为索引不需要和id(但我不确定)。
我有:
用户的控制器(我的控制器中还有一个名为"user"的文件夹,它有用于注册和全身份验证回调的控制器):
class UsersController < ApplicationController
before_action :set_user, only: [:index, :show, :edit, :update, :finish_signup, :destroy]
def index
# if params[:approved] == "false"
# @users = User.find_all_by_approved(false)
# else
@users = User.all
# end
end
# GET /users/:id.:format
def show
# authorize! :read, @user
end
# GET /users/:id/edit
def edit
# authorize! :update, @user
end
# PATCH/PUT /users/:id.:format
def update
# authorize! :update, @user
respond_to do |format|
if @user.update(user_params)
sign_in(@user == current_user ? @user : current_user, :bypass => true)
format.html { redirect_to @user, notice: 'Your profile was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
end
# GET/PATCH /users/:id/finish_signup
def finish_signup
# authorize! :update, @user
if request.patch? && params[:user] #&& params[:user][:email]
if @user.update(user_params)
@user.skip_reconfirmation!
sign_in(@user, :bypass => true)
redirect_to root_path, notice: 'Your profile was successfully updated.'
# redirect_to [@user, @user.profile || @user.build_profile]
# sign_in_and_redirect(@user, :bypass => true)
else
@show_errors = true
end
end
end
# DELETE /users/:id.:format
def destroy
# authorize! :delete, @user
@user.destroy
respond_to do |format|
format.html { redirect_to root_url }
format.json { head :no_content }
end
end
private
def set_user
@user = User.find(params[:id])
end
def user_params
# params.require(:user).permit(policy(@user).permitted_attributes)
accessible = [ :first_name, :last_name, :email, :avatar, {role_ids: []} ] # extend with your own params
accessible << [ :password, :password_confirmation ] unless params[:user][:password].blank?
# accessible << [:approved] if user.admin
params.require(:user).permit(accessible)
end
end
我有一个用户索引视图:
<div class="col-xs-10 col-xs-offset-1" style="margin-left:10%; margin-right:10%">
<% Users.each do |user| %>
<div class="row">
<div class="col-xs-5">
<%= link_to "#{user.full_name}", user %>
</div>
<div class="col-xs-5">
<%= link_to "#{user.email}", user %>
</div>
<div class="col-xs-2">
<%= link_to "edit", edit_user_path(user) %>
</div>
</div>
<% end %>
当我尝试此操作时,我收到上面发布的错误。
任何人都可以看到我做错了什么,或者破译错误消息吗?
也只需从波纹管代码中删除:index
操作:
before_action :set_user, only: [:index, :show, :edit, :update, :finish_signup, :destroy]
实际上,在调用控制器中的任何操作之前,会调用一个名为"def set_user"的私有函数,这会导致索引操作出现问题。您可能需要重新考虑您正在处理的逻辑。
def set_user
@user = User.find(params[:id])
end
我认为您的错误是因为:index
before_action :set_user, only: [:index, :show, :edit, :update, :finish_signup, :destroy]
.只需从列表中删除:index
,就可以解决此错误。params
哈希不包含index
路由上的:id
密钥。查看有关 Rails 路由的本指南。
在这种情况下,index
操作用于抓取所有用户,以便他们可以显示在索引页上。例如,在我的一个应用程序中,我有以下内容:
class UsersController < ApplicationController
before_action :set_user, only: [:show, :edit, :update, :destroy]
# GET /users
def index
@users = User.all
end
# omitted code
def set_user
@user = User.find(params[:id])
end
end
帕特里克