繁体   English   中英

Rails中的ActiveRecord :: RecordNotFound

[英]ActiveRecord::RecordNotFound in Rails

我有一个带有用户表和连接表的应用程序(将连接视为好友; user_id = current_user,otheruser_id =您正在查看的用户个人资料)。 我在用户个人资料上有一个链接,可让current_user编辑该连接。 这是用户显示页面上的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 %>

但是,单击上面的编辑链接将显示此错误:

ConnectionsController#edit中的ActiveRecord :: RecordNotFound找不到id = 2的Connection [WHERE“ connections ..” user_id“ = 1]

似乎正在使用显示页面上的user_id(在上面的示例中,我正在查看user_id 2)作为connections表中的connection_id。 如何将其更改为正确的ID?

连接控制器:

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

用户控制器(如果需要):

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

如何使用正确的connection_id而不是user_id进行编辑和创建链接?

另外,我注意到的另一个问题是应用程序绕过了if语句中的第一个条件。 即使没有@connection,它跳跃到它假定一个连接的条件。

非常感谢您的宝贵时间。 我真的被困在这里。 谢谢!

编辑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

编辑2:

为了澄清,在上面的错误消息中,我正在查看user_id = 2配置文件,但是现在不存在任何连接(本地清除也是如此),因此我的视图应显示指向创建路径的链接(新发现的问题)。 无论如何,它似乎仍在使用user_id代替connection_id。 即使已经存在连接,它也应该编辑id = 1而不是2的Connection。

到目前为止,感谢大家的帮助。 编辑3:我更新了上述form_for。 我现在有两个问题:

  1. 表单无法识别IF语句“如果connection.blank?” 并进入第二个条件:即使数据库中不存在连接,也要编辑连接。

  2. 当我单击编辑链接时,即使连接不存在,也会出现路由错误:没有路由匹配{:controller =>“ connections”,:action =>“ edit”,:id => nil}。 同样,这可能是因为尚无连接。 在解决此问题之前,我可能需要修复#1。

PS:我愿意为解决方案付钱...我完全被困住了! 再次感谢迄今为止提供帮助的每个人。

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]

您未传递任何ID的原因是编辑链接不起作用,原因是

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

这不是您的主要问题的解决方案,但我认为您可以考虑采用这种建议来改进应用程序:您为什么不习惯设计数据模型,而将用户设计为与自身有关系的模型。

例如:

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

通过此设置,您可以检索@ user.connections和@ user.followers。

暂无
暂无

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

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