0

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?

Dennis Best
  • 3,614
  • 3
  • 20
  • 31
  • 1
    If you run `Project.friendly.find("the-friendly-id-of-the-project")` manually in the Rails console, does it give you the proper object? It's likely that `current_user` is `nil`. – tangrs Jan 31 '15 at 01:11
  • Oops. I was so focused on the Project, that I forgot about the User. – Dennis Best Jan 31 '15 at 01:25
  • Fixed by requiring a login, thereby getting a currrent_user. Double ooops. – Dennis Best Jan 31 '15 at 01:40

0 Answers0