Skip to content

Instantly share code, notes, and snippets.

@maclover7
Last active August 29, 2015 14:08
Show Gist options
  • Save maclover7/de95a5a14b1fd163437e to your computer and use it in GitHub Desktop.
Save maclover7/de95a5a14b1fd163437e to your computer and use it in GitHub Desktop.
Rails Sub-resources Routing

person has_many :jokes joke belongs_to :person

<%= form_for [:person, @joke] do |f| %>
<% if @joke.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@joke.errors.count, "error") %> prohibited this joke from being saved:</h2>
<ul>
<% @joke.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :body %><br>
<%= f.text_area :body %>
</div>
<%= f.hidden_field :person_id %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
<h1>Listing jokes</h1>
<table>
<thead>
<tr>
<th>Body</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% @jokes.each do |joke| %>
<tr>
<td><%= joke.body %></td>
<!-- Errors begin here -->
<td><%= link_to 'Show', person_joke_path(joke) %></td>
<td><%= link_to 'Edit', edit_person_joke_path(joke) %></td>
<td><%= link_to 'Destroy', joke, method: :delete, data: { confirm: 'Are you sure?' } %></td>
<!-- Errors end here -->
</tr>
<% end %>
</tbody>
</table>
<br>
<%= link_to 'New Joke', new_person_joke_path %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment