簡體   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