简体   繁体   中英

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). I have a link on user profiles that lets the current_user edit that connection. Here is the form_for on the user show page: (UPDATED CODE):

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

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. How do I change it to the correct 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?

Also, another problem I noticed is that the app is bypassing the first condition in the if statement. Even if there is no @connection, it's skipping to the condition where it assumes that there is a connection.

Your time is greatly appreciated. I'm really stuck here. Thanks!

EDIT 1: Adding routes just in case:

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:

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). Regardless, it still seems to be using user_id in place of the connection_id. Even if a connection already existed, it should be going to edit Connection with id = 1, not 2.

Thanks for the help so far everyone. EDIT 3: I updated the form_for above. I have two problems now:

  1. Form isn't recognizing the IF statement "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}. Again, this may be because no connections exist yet. I may need to fix #1 before this issue can be fixed.

PS: I'm willing to pay for a solution...I am completely stumped! 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

<%= @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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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