简体   繁体   English

Rails中的ActiveRecord :: RecordNotFound

[英]ActiveRecord::RecordNotFound in Rails

I have an app with a Users table and a Connections table (think of a connection as a friend; user_id = current_user and otheruser_id = the user profile you're viewing). 我有一个带有用户表和连接表的应用程序(将连接视为好友; user_id = current_user,otheruser_id =您正在查看的用户个人资料)。 I have a link on user profiles that lets the current_user edit that connection. 我在用户个人资料上有一个链接,可让current_user编辑该连接。 Here is the form_for on the user show page: (UPDATED CODE): 这是用户显示页面上的form_for :(更新代码):

<% unless current_user == @user %>
<% if @connection.blank? %>
How do you know <%= @user.first_name %>? (<%= link_to "edit contact", { :controller => 'connections', :action => 'create', :id => :connection } %> )
<% else %>
<%= @user.first_name %> is your <%= @connection.description %> (<%= link_to "edit contact", { :controller => 'connections', :action => 'edit', :id => @connection.id } %> )
<% end %>

However, clicking on the edit link above shows this error: 但是,单击上面的编辑链接将显示此错误:

ActiveRecord::RecordNotFound in ConnectionsController#edit Couldn't find Connection with id=2 [WHERE "connections"."user_id" = 1] ConnectionsController#edit中的ActiveRecord :: RecordNotFound找不到id = 2的Connection [WHERE“ connections ..” user_id“ = 1]

It seems as if it's using the user_id on the show page (in the above example I'm looking at user_id 2) as the connection_id in the connections table. 似乎正在使用显示页面上的user_id(在上面的示例中,我正在查看user_id 2)作为connections表中的connection_id。 How do I change it to the correct id? 如何将其更改为正确的ID?

Connections controller: 连接控制器:

class ConnectionsController < ApplicationController

 def update
  @connection = current_user.connections.find(params[:id])
    if @connection.update_attributes(params[:connection])
      flash[:notice] = "Contact relationship updated"
      redirect_to @user
    end
  end

  def edit
    @connection = current_user.connections.find(params[:id])
    redirect_to @user
  end

  def create
    #@user = User.find(params[:user_id])
    @connection = current_user.connections.build(params[:connection])
    if @connection.save
      flash[:success] = "Contact relationship saved!"
      redirect_to @user
    else
      render @user
    end
  end
end

User controller if needed: 用户控制器(如果需要):

def show
    @user = User.find(params[:id])
    @connection = current_user.connections.build(:otheruser_id => @user)
  end

How can I have the edit and create links use the correct connection_id instead of the user_id? 如何使用正确的connection_id而不是user_id进行编辑和创建链接?

Also, another problem I noticed is that the app is bypassing the first condition in the if statement. 另外,我注意到的另一个问题是应用程序绕过了if语句中的第一个条件。 Even if there is no @connection, it's skipping to the condition where it assumes that there is a connection. 即使没有@connection,它跳跃到它假定一个连接的条件。

Your time is greatly appreciated. 非常感谢您的宝贵时间。 I'm really stuck here. 我真的被困在这里。 Thanks! 谢谢!

EDIT 1: Adding routes just in case: 编辑1:添加路由以防万一:

authenticated :user do
    root :to => 'activities#index'
  end
  root :to => "home#index"
  devise_for :users, :controllers => { :registrations => "registrations" }
  resources :users do
    member do
      get :following, :followers, :posts, :comments, :activities, :works, :contributions, :connections
    end
  end
  resources :works do
    resources :comments
  end
  resources :relationships, only: [:create, :destroy]
  resources :posts
  resources :activities
  resources :reposts
  resources :comments do
    member do
      put :toggle_is_contribution
    end
    end
  resources :explore
  resources :connections
end

EDIT 2: 编辑2:

Just to clarify, in the error message above, I'm viewing the user_id = 2 profile, but no connections exist right now (just did a local purge) so my view should be showing link to the create path (newly discovered problem). 为了澄清,在上面的错误消息中,我正在查看user_id = 2配置文件,但是现在不存在任何连接(本地清除也是如此),因此我的视图应显示指向创建路径的链接(新发现的问题)。 Regardless, it still seems to be using user_id in place of the connection_id. 无论如何,它似乎仍在使用user_id代替connection_id。 Even if a connection already existed, it should be going to edit Connection with id = 1, not 2. 即使已经存在连接,它也应该编辑id = 1而不是2的Connection。

Thanks for the help so far everyone. 到目前为止,感谢大家的帮助。 EDIT 3: I updated the form_for above. 编辑3:我更新了上述form_for。 I have two problems now: 我现在有两个问题:

  1. Form isn't recognizing the IF statement "if connection.blank?" 表单无法识别IF语句“如果connection.blank?” and is going to the second condition: editing the connection even if a connection doesn't exist in the database. 并进入第二个条件:即使数据库中不存在连接,也要编辑连接。

  2. When I click on the edit link, even if the connection doesn't exist, I get a routing error: No route matches {:controller=>"connections", :action=>"edit", :id=>nil}. 当我单击编辑链接时,即使连接不存在,也会出现路由错误:没有路由匹配{:controller =>“ connections”,:action =>“ edit”,:id => nil}。 Again, this may be because no connections exist yet. 同样,这可能是因为尚无连接。 I may need to fix #1 before this issue can be fixed. 在解决此问题之前,我可能需要修复#1。

PS: I'm willing to pay for a solution...I am completely stumped! PS:我愿意为解决方案付钱...我完全被困住了! Thanks again to everyone who helped so far. 再次感谢迄今为止提供帮助的每个人。

However, clicking on the edit link above shows this error:

ActiveRecord::RecordNotFound in ConnectionsController#edit Couldn't find Connection with id=2 [WHERE "connections"."user_id" = 1]

The reason edit link is not working that you are not passing any id , it will be 您未传递任何ID的原因是编辑链接不起作用,原因是

<%= @user.first_name %> is your <%= @connection.description %> (<%= link_to "edit contact", { :controller => 'connections', :action => 'edit', id => @user.id } %> )

This not is a solution to your primary question, but i think that you can consider this sugestion to improve the application: Why do you don't use to designing your data model, user as a model that have a relation to itself. 这不是您的主要问题的解决方案,但我认为您可以考虑采用这种建议来改进应用程序:您为什么不习惯设计数据模型,而将用户设计为与自身有关系的模型。

For example: 例如:

class User < ActiveRecord::Base
    has_many :connections, :class_name => "User",
    :foreign_key => "user_id"
    has_many :followers, :class_name => "User"
end

With this setup, you can retrieve @user.connections and @user.followers. 通过此设置,您可以检索@ user.connections和@ user.followers。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM