2013-06-25 85 views
0

我有一個用戶表和一個連接表的應用程序(把連接想象成朋友; user_id = current_user和otheruser_id =你正在查看的用戶配置文件)。我在用戶配置文件上有一個鏈接,讓current_user編輯該連接。下面是用戶展示頁面上的form_for: (更新後的代碼):Rails的ActiveRecord :: RecordNotFound

<% 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 %> 

但是,單擊上方編輯鏈接顯示了這個錯誤:

的ActiveRecord :: RecordNotFound在ConnectionsController#編輯 Couldn '找到連接id = 2 [WHERE「connections」。「user_id」= 1]

看起來好像在顯示頁面上使用user_id(在上面的示例中,我正在查看user_id 2)作爲連接表中的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,它也會跳到假設有連接的情況。

您的時間非常感謝。我真的被困在這裏。謝謝!

EDIT 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.

感謝迄今爲止所有人的幫助。 編輯3:我更新了上面的form_for。我現在有兩個問題:「如果connection.blank」

  1. 形式是不承認的IF語句並且正在進入第二個條件:即使數據庫中不存在連接,也要編輯連接。

  2. 當我點擊編輯鏈接,即使連接不存在,我得到一個路由錯誤:沒有路由匹配{:controller =>「connections」,:action =>「edit」,:id =>零}。同樣,這可能是因爲還沒有連接。在解決此問題之前,我可能需要修復#1。

PS:我願意付出解決方案......我完全難倒了!再次感謝迄今爲止幫助過的所有人。

+1

使用「的before_filter的authenticate_user!」在連接控制器,那麼它不會繞過if條件,如果你沒有登錄,current_user是零。除非你使用to_params方法,否則不能改變編輯鏈接的默認映射。 – sunny1304

+0

感謝您的回覆。我在控制器中添加了上述錯誤。我更新了我的問題以澄清錯誤。 – winston

回答

-1

這不是你的主要問題的解決方案,但我認爲你可以考慮這個消解來改進應用程序: 爲什麼你不用它來設計你的數據模型,用戶是一個有關係的模型自己。

例如:

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。

+1

這沒有任何意義。沒有辦法只用一個表來建模。除了他想存儲兩個用戶之間關係的描述外,「連接」也是非常有意義的。 – Mischa

1
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] 

原因編輯鏈接無法正常工作,你不傳遞任何標識,這將是

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

非常感謝您的幫助。這是有道理的。待我晚點回家時,我會試試這個。我認爲在解決這個問題後,我的下一個問題是if語句無法正常工作。即使@連接從未設置,它總是加載編輯操作 – winston

+0

這似乎不起作用。我改變:ID爲@ connection.id(因爲我想編輯連接,而不是用戶),但我得到了路由錯誤:沒有路由匹配{:controller =>「connections」,:action =>「edit」, :ID =>零}。 – winston

+0

更新了問題 – winston

相關問題