Last active
December 29, 2015 00:19
-
-
Save willmanduffy/7584767 to your computer and use it in GitHub Desktop.
Proposal Creation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Project < ActiveRecord::Base | |
| # Associations | |
| has_and_belongs_to_many :users | |
| has_many :proposals, dependent: :destroy | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Proposal < ActiveRecord::Base | |
| # Associations | |
| belongs_to :project | |
| belongs_to :user | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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