Rails 4错误-Rails找不到'id'= index的用户 [英] Rails 4 error - rails Couldn't find User with 'id'=index
问题描述
我正在尝试在Rails 4中制作一个应用.
I am trying to make an app in Rails 4.
我在这篇文章中尝试遵循CyberDude的建议:
I'm trying to follow CyberDude's advice in this post:
我陷入了在用户/视图文件中为用户建立索引的部分.
I'm getting stuck at the part where I make an index for users in the user/views file.
我收到一条错误消息:
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(但是我不确定).
I can't understand this error message. Others with similar messages seem to have problems arising in other actions (like #show) where an :id is required. I think index doesn't need and id (but I'm not sure about that).
我有:
用户的控制器(我的控制器中还有一个名为用户"的文件夹,它具有用于注册和omniauth回调的控制器):
User's controller (I also have a folder in my controllers called "user" and it has controllers for regirations and omniauth callbacks):
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
我有一个用户索引视图:
I have a users index view:
<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 %>
尝试此操作时,出现上面发布的错误.
When I try this, I get the error I posted above.
任何人都可以看到我做错了什么,或者破译了错误消息吗?
Can anyone see what I've done wrong, or decipher the error message?
推荐答案
我认为您正在发生错误,因为:index
位于before_action :set_user, only: [:index, :show, :edit, :update, :finish_signup, :destroy]
中.只需从列表中删除:index
,即可解决此错误. params
哈希在index
路由上不包含:id
键.查阅本指南有关Rails路由.
I think that your error is occuring because :index
is in before_action :set_user, only: [:index, :show, :edit, :update, :finish_signup, :destroy]
. Simply remove :index
from the list and that should take care of this error. The params
hash does not contain an :id
key on the index
route. Check out this guide on Rails routing.
在这种情况下,使用index
动作来捕获所有用户,以便它们可以显示在索引页面上.例如,在我的一个应用程序中,我具有以下内容:
The index
action is used, in this case, to grab all of the users so that they can be displayed on your index page. For example in one of my apps I have the following:
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
帕特里克
这篇关于Rails 4错误-Rails找不到'id'= index的用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!