Skip to content

Instantly share code, notes, and snippets.

@themoxman
Last active August 29, 2015 13:56
Show Gist options
  • Select an option

  • Save themoxman/8790035 to your computer and use it in GitHub Desktop.

Select an option

Save themoxman/8790035 to your computer and use it in GitHub Desktop.
resources :collections do
resources :notes, :tags
end
def new
@ticket = @project.tickets.build
end
# is basically the same as
def new
@ticket = Ticket.new(project_id: @project.id)
end
# setting @project and @ticket
# note @project is set before @ticket
before_filter :set_project
before_filter :set_ticket, only: [:show, :edit, :update, :destroy]
private
def set_project
@project = Project.find(params[:project_id])
end
def set_ticket
@ticket = @project.tickets.find(params[:id])
end
# note, if you're using the before_filter as an authorization technique,
# you're going to have to decide how to handle ActiveRecord::RecordNotFound
# exceptions when a non-existent Collection, Note, or Tag is requested
# search "ActiveRecord::RecordNotFound" in Rails 4 in Action
# discussion of handling the exception in 4.4 and on as well as 8.4 and on
# here's one approach, see p132
private
def set_project
@project = Project.find(params[:id])
rescue ActiveRecord::RecordNotFound
flash[:alert] = "The project you were looking" +
" for could not be found."
redirect_to projects_path
end
# forms for nested resources
# note the use of @project and @ticket instead of simply @ticket
= form_for [@project, @ticket] do |f|
# spec references
# see p137 for "creating ticket" spec
# see p152 for "viewing tickets" spec. note that two separate projects are created. note use of 'within' in the spec
# note the use of [@project, @ticket] array when creating links
# avoid some_path_to_path stuff.
<ul id='tickets'>
<% @project.tickets.each do |ticket| %>
<li>
#<%= ticket.id %> -
<%= link_to ticket.title, [@project, ticket] %>
</li>
<% end %>
</ul>
# Seed data, check out section 8.9.3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment