I'm working on a simple app that lets a User make a Donation to a Project. My Project model uses FriendlyId. It's all going well. Users can create projects, etc. But I can't seem to get the Donations controller to resolve the project id.
Here's my Donation controller:
class DonationsController < ApplicationController
before_filter :load_project
def new
@donation = Donation.new
end
def create
@donation = Donation.new(donation_params)
if @donation.save
flash[:success] = 'Donation was successfully created.'
redirect_to project_path(@project)
else
flash.now[:error] = 'Unable to create donation.'
flash.now[:errors] = @donation.errors.messages
render action: 'new'
end
end
private
def load_project
@project = Project.find(params[:project_id])
end
def donation_params
donation_params = params.require(:donation).permit(:amount)
donation_params.merge!({project_id: @project.id, user_id: current_user.id})
end
end
When I submit a new donation I get Couldn't find Project with 'id'=the-friendly-id-of-the-project
If I change the load_project to use the friendly id to
def load_project
@project = Project.friendly.find(params[:project_id])
end
I get undefined method 'id' for nil:NilClass
I know I must be missing something obvious here... but what?