Reputation: 3100
I'm getting the following error on a page that contains a link to credit/edit an object:
Routing Error No route matches {:controller=>"connections", :action=>"edit", :id=>nil}
My app has user profile pages (#show action in users_controller.rb) with a link to create or edit an existing "connection" (a relationship between users like Facebook or LinkedIn). If the connection does not exist, the link will send the user to create a new connection. If the connection already exists, the link will send the user to edit the existing connection. My create functionality works fine. However, once the connection has been created, visiting the user page throws the routing error. It clearly does not like my link_to for the edit action.
Here is the link_to on the user page:
<% unless current_user == @user %>
<% if @contact.present? %>
<%= @user.first_name %> is your <%= @connection.description %> (<%= link_to "edit contact", { :controller => 'connections', :action => 'edit', :id => @connection.id} %> )
<% else %>
How do you know <%= @user.first_name %>? (<%= link_to "edit contact", { :controller => 'connections', :action => 'create', :user_id => @user.id }, :method => 'post' %> )
<% end %>
The error also specifically states that :id is set to nil (null). The problem must be here. How can I my app to refer to the correct connection? My instance variable should have done the trick.
Here's the connection controller:
class ConnectionsController < ApplicationController
def index
end
def update
@connection = current_user.connections.find(params[:id])
if @connection.update_attributes(params[:connection])
flash[:notice] = "Contact relationship updated"
render 'activities'
end
end
def edit
@connection = current_user.connections.find(params[:id])
end
def create
#@user = User.find(params[:user_id])
@connection = current_user.connections.build(params[:connection])
@user = User.find(params[:user_id])
if @connection.save
flash[:success] = "Contact relationship saved!"
else
render @user
end
end
end
And here's the User controller show action (where the link_to exists):
def show
@user = User.find(params[:id])
@connection = current_user.connections.build(:otheruser_id => @user)
@contact = current_user.connections.where(user_id: current_user, otheruser_id: @user)
end
routes.rb:
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
Any ideas on how to fix the link_to? Thanks!
EDIT 1: relevant rake routes:
connections GET /connections(.:format) connections#index
POST /connections(.:format) connections#create
new_connection GET /connections/new(.:format) connections#new
edit_connection GET /connections/:id/edit(.:format) connections#edit
connection GET /connections/:id(.:format) connections#show
PUT /connections/:id(.:format) connections#update
DELETE /connections/:id(.:format) connections#destroy
Upvotes: 0
Views: 1512
Reputation: 3266
Your problem is in:
@connection = current_user.connections.build(:otheruser_id => @user)
That instantiates a connection object and assigns it to @connection. This object hasn't been saved, therefore it doesn't have an id. Conceptually it's wrong too, how can you edit something that doesn't exist yet? If you want to send the user to create a new connection, Connections#new is what you're after.
Upvotes: 1