Ruby-on-rails 找不到 id= 的用户 (ActiveRecord::RecordNotFound)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15693479/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 21:42:22  来源:igfitidea点击:

Couldn't find User with id= (ActiveRecord::RecordNotFound)

ruby-on-rails

提问by winston

With my rails app I can successfully create an object (called work; think of them as blog posts) as current_user. A user has_many works. I can validate that the object is created successfully by checking the database using my postgresql browser. The table also holds the correct user_id that created the work so I know that my create function works in my controller.

使用我的 rails 应用程序,我可以成功创建一个对象(称为工作;将它们视为博客文章)作为 current_user。用户 has_many 工作。我可以通过使用我的 postgresql 浏览器检查数据库来验证对象是否已成功创建。该表还包含创建工作的正确 user_id,因此我知道我的 create 函数在我的控制器中工作。

However, the problem is that when I try to view the work, I get the following error:

但是,问题是当我尝试查看作品时,出现以下错误:

ActiveRecord::RecordNotFound in WorksController#show

ActiveRecord::RecordNotFound in WorksController#show

Couldn't find User with id=23

找不到 id=23 的用户

app/controllers/works_controller.rb:43:in `show'

应用程序/控制器/works_controller.rb:43:in`show'

What's odd is that I can still view works that I created several weeks ago. The error only appears for works that I have created recently.

奇怪的是,我仍然可以查看几周前创作的作品。该错误仅出现在我最近创作的作品中。

Here's the Works controller:

这是 Works 控制器:

class WorksController < ApplicationController
   #before_filter :current_user,   only: [:edit, :update]

  def index
    @works = Work.all

respond_to do |format|
  format.html # index.html.erb
  format.xml  { render :xml => @works }
end
end
def create
    @work = current_user.works.create(params[:work])
    redirect_to current_user
  end

def edit
    @work = current_user.works.find(params[:id])
end

def new
  @work = current_user.works.new
end

def destroy
  @work = current_user.works.find(params[:id]).destroy
  flash[:success] = "Work deleted"
  redirect_to current_user
end

 def update
    @work = current_user.works.find(params[:id])
    if @work.update_attributes(params[:work])
      flash[:success] = "updated"
      redirect_to @work
    else
      render 'edit'
    end
  end


  def show
    @user = User.find(params[:id])
    @work = @user.works.find(params[:id])
    @activities = PublicActivity::Activity.order("created_at DESC").where(trackable_type: "Work", trackable_id: @work).all

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @work }
    end
  end
end

I'm assuming the error is in the Works controller. What do I need to edit in the controller in order to fix the "show" errors?

我假设错误出在 Works 控制器中。我需要在控制器中编辑什么才能修复“显示”错误?

EDIT: If I use the current code above, I can only view other people's works (viewing my own throws an error). However, if I add current_user into the query (such as @works= current_user.works), I can then only see my own works. Viewing other people's works throws an error. How can I fix this so that I can view both my own works and works created by others?

编辑:如果我使用上面的当前代码,我只能查看其他人的作品(查看我自己的作品会引发错误)。但是,如果我将 current_user 添加到查询中(例如@works= current_user.works),那么我只能看到我自己的作品。查看其他人的作品会引发错误。我该如何解决这个问题,以便我可以查看自己的作品和其他人创作的作品?

EDIT 2:

编辑2:

@work = Work.find(params[:id]) works if I remove @user from the controller and the "@user" references in the works show.html view file. However, I need the "@user" reference in the controller because I want to display the name of the user that created the work. How should I go about doing this?

@work = Work.find(params[:id]) 如果我从控制器中删除@user 和works show.html 视图文件中的“@user”引用,则工作正常。但是,我需要控制器中的“@user”引用,因为我想显示创建作品的用户的名称。我该怎么做呢?

EDIT 3:

编辑 3:

FIXED! Thanks again to everyone who contributed their answers! Here's what I did to fix it:

固定的!再次感谢所有提供答案的人!这是我为修复它所做的:

  1. I removed the @user reference from the controller ("show" action). As Fred mentioned below the @user reference is not needed and needed to be removed since I was using :id twice to reference two separate objects.

  2. Edited the "@work" variable to @work = Work.find(params[:id]). This will look for the correct work item based on the id regardless of which user created it.

  3. When I need to display the user data on the work pages, simply use <%= @work.user.name %>on the show.html.erb view page. '@user = User.find(params[:id])' is not needed since I already defined a foreign key relationship on the works model by using 'belongs_to :user'.

  1. 我从控制器中删除了@user 引用(“显示”操作)。正如 Fred 在下面提到的,@user 引用是不需要的,需要删除,因为我使用 :id 两次来引用两个单独的对象。

  2. 将“@work”变量编辑为@work = Work.find(params[:id]). 这将根据 id 查找正确的工作项,而不管它是由哪个用户创建的。

  3. 当我需要在工作页面上显示用户数据时,只需<%= @work.user.name %>在 show.html.erb 视图页面上使用即可。不需要“@user = User.find(params[:id])”,因为我已经使用“belongs_to :user”在作品模型上定义了外键关系。

Thanks again for all of the help! -j

再次感谢大家的帮助!-j

回答by pricey

In your show action, get rid of @user all together and just use:

在你的表演行动中,一起摆脱@user 并使用:

@work = Work.find(params[:id])

This will allow anyone to view any work.

这将允许任何人查看任何作品。

Same goes for your other actions. By saying:

您的其他操作也是如此。通过说:

@work = current_user.works.find(params[:id])

you are searching through all the current_user's works for a work with id == params[:id].

您正在搜索所有 current_user 的作品以寻找与id == params[:id].

回答by Jonathan Cohlmeyer

I found using current_user.works.find_by(id: params[:id]) works returning a nil when nothing is found.

我发现使用 current_user.works.find_by(id: params[:id]) 可以在未找到任何内容时返回 nil。

I think this is a little better as you could now use current_user in your look up to be a little safer as you do not have to rely on your view to not show data that it should not because it never gets it.

我认为这会好一些,因为您现在可以在查找中使用 current_user 来更安全一些,因为您不必依赖您的视图来不显示它不应该显示的数据,因为它永远不会得到它。

回答by Michael Durrant

I suspect that you are using the work_idinstead of user_idwhen looking up the user and so it works sometimes but is the wrong ID.

我怀疑您在查找用户时使用了work_id而不是,user_id因此它有时会起作用,但 ID 是错误的。

You are in the works controller. params[:id] is the work.

您在作品控制器中。params[:id] 是工作。

so this:

所以这:

@user = User.find(params[:id])
@work = @user.works.find(params[:id])

should probably be this:

应该是这样的:

@user = User.find(current_user)
@work = @user.works.find(params[:id])

If you just want all the works use

如果你只是想要所有的作品使用

@works = Work.all

since you can use the same params[:id] as te id to look up both user and work. If they are the same ID it's just a coincidence.

因为您可以使用与 te id 相同的 params[:id] 来查找用户和工作。如果他们是同一个ID,那只是巧合。

回答by Ethan Hayon

It looks like you are deleting a user and are left with the dangling references to works

看起来您正在删除用户并留下对 works

By querying for works with

通过查询作品

@work = @user.works.find(params[:id])

@work = @user.works.find(params[:id])

you are looking for all of @user's works which belong to the @user (redundant)

您正在寻找属于@user 的所有@user 作品(冗余)

If you instead use @work = @user.worksthose dangling works references won't be touched since @user.works will return an empty response.

如果您改为使用@work = @user.works那些悬空的作品引用,则不会触及,因为 @user.works 将返回一个空响应。

This is really not the best solution though, you need to better handle the destruction of dependencies when a user is deleted.

不过,这确实不是最好的解决方案,您需要更好地处理删除用户时依赖项的破坏。

EDIT: Sorry, I used current_user where I should have used @user, comment above has same error.

编辑:对不起,我在应该使用@user 的地方使用了 current_user,上面的评论有同样的错误。