Skip to content

Instantly share code, notes, and snippets.

@willmanduffy
Last active December 29, 2015 00:19
Show Gist options
  • Select an option

  • Save willmanduffy/7584767 to your computer and use it in GitHub Desktop.

Select an option

Save willmanduffy/7584767 to your computer and use it in GitHub Desktop.
Proposal Creation
class Project < ActiveRecord::Base
# Associations
has_and_belongs_to_many :users
has_many :proposals, dependent: :destroy
end
class Proposal < ActiveRecord::Base
# Associations
belongs_to :project
belongs_to :user
end
class ProposalsController < ApplicationController
before_filter :find_project, only: [:new]
def create
@proposal = Proposal.create(params[:proposal])
end
# I am making the assumption here that you want a form to make a new proposal, this is that page
def new
@proposal = current_user.proposals.new(:project => @project)
end
private
def find_project
# This ensures the user has access to the project
@project = current_user.projects.find(params[:project_id])
end
end
resources :projects do
# This will create a route /projects/[:project_id]/proposals/new
# Note, [:project_id] may actually be [:id]. If it is, just substitute that in the proposals controller
resources :proposals, only: [:create, :new]
end
# I'm changing around your schema a bit I think, but I saw you wanted to allow multiple users to create proposals for projects in the future
class User < ActiveRecord::Base
# Associations
has_and_belongs_to_many :projects # Can also do a has_many :projects_users, :has_many :projects, through: :projects_users
has_many :proposals, dependent: :destroy
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment