I’ve searched my problem high and low on here and haven’t quite found the right answer. I think it might be a simple routes issue. On my app, a user can create a project and follow “plants” through their project before ever having to officially sign up. The routes for updating and editing the project itself work fine. The error I get “couldn’t find Project without an ID” only occurs after I try accessing the CREATE action in my “Prelationships” controller to build a relationship between the “Project” and the “Plants.” My code is below:
My routes.rb
resources :sessions, :only => [:new, :create, :destroy]
resources :microposts, :only => [:create, :destroy]
resources :relationships, :only => [:create, :destroy]
resources :prelationships, :only => [:create]
resources :plants
resources :projects do
member do
get :pfollowing, :pfollowers
end
end
My “Prelationships” controller
class PrelationshipsController < ApplicationController
def create
@project = Project.find(params[:project_id])
@plant = Plant.find(params[:prelationship][:pfollowed_id])
@project.pfollow!(@plant)
respond_to do |format|
format.html { redirect_to @project }
format.js { redirect_to @project }
end
end
end
My “Prelationships” model
class Prelationship < ActiveRecord::Base
attr_accessible :pfollowed_id
belongs_to :pfollower, :class_name => "Project"
belongs_to :pfollowed, :class_name => "Plant"
validates :pfollower_id, :presence => true
validates :pfollowed_id, :presence => true
end
My “Projects” model
class Project < ActiveRecord::Base
attr_accessible :title, :address, :latitude, :longitude, :state
belongs_to :user
has_many :prelationships, :foreign_key => "pfollower_id",
:dependent => :destroy
has_many :pfollowing, :through => :prelationships, :source => :pfollowed
def pfollowing?(pfollowed)
prelationships.find_by_pfollowed_id(pfollowed)
end
def pfollow!(pfollowed)
prelationships.create!(:pfollowed_id => pfollowed.id)
end
end
My “Plants” model
class Plant < ActiveRecord::Base
has_many :prelationships, :foreign_key => "pfollowed_id",
:class_name => "Prelationship"
has_many :pfollowers, :through => :reverse_prelationships,
:source => :pfollower
end
My partial for building the relationship on the @project ‘show’ page
<%= form_for @project.prelationships.build(:pfollowed_id =>
@project_id) do |f| %>
<%= collection_select(:prelationship, :pfollower_id, Plant.all, :id, :name, options =
{:prompt => "Select your plants"}, :class => "listselect") %>
<div class="actions">
<%= f.submit "Pfollow" %>
</div>
<% end %>
The error message is “Could not find Project without an ID” once I hit submit on the partial.
app/controllers/prelationships_controller.rb:4:in `create'
and the Parameters:
{"utf8"=>"✓",
"authenticity_token"=>"NKqa1f0M2yPLQDHbRLnxl3SiwBeTus/1q1hpZjD7hgY=",
"prelationship"=>{"pfollower_id"=>"5"},"commit"=>"Pfollow"}
If this is a routes issue, I can’t seem to find the right language to fix it. I’ve built a User model with the ability to follow other users and did not have these problems, which leads me to believe I might have to create a session for my project?
Help this newbie, please!
The simple way:
And the right way would be creating a nested route on projects, but you can do this refactoring as an exercise for later.