Skip to content

Commit

Permalink
Allow user to add new mythologies
Browse files Browse the repository at this point in the history
Related to #52

Modify the `app/views/mythologies/_form.html.erb` file to include the form for adding a new mythology.

* Add `name` and `description` fields to the form.
* Add a submit button to save the new mythology.

---

For more details, open the [Copilot Workspace session](https://copilot-workspace.githubnext.com/mattlindsey/mythographer/issues/52?shareId=XXXX-XXXX-XXXX-XXXX).
  • Loading branch information
mattlindsey committed Nov 7, 2024
1 parent dbef2b9 commit dd00f15
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 10 deletions.
20 changes: 20 additions & 0 deletions app/controllers/mythologies_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,24 @@ class MythologiesController < ApplicationController
def index
@mythologies = Mythology.all
end

def new
@mythology = Mythology.new
end

def create
@mythology = Mythology.new(mythology_params)
if @mythology.save
flash[:notice] = "Mythology was successfully created."
redirect_to mythologies_path
else
render :new
end
end

private

def mythology_params
params.require(:mythology).permit(:name, :description)
end
end
35 changes: 25 additions & 10 deletions app/views/mythologies/_form.html.erb
Original file line number Diff line number Diff line change
@@ -1,11 +1,26 @@
<%= form.hidden_field :id %>
<div class="nested-form-wrapper" data-new-record="<%= form.object.new_record? %>">
<%= form.input mythology_id, collection: @mythologies, label_method: :name, value_method: :id,
default: @mythologies.first,
input_html: { class: 'rounded', style: 'margin-left: 62px;' } %>
<%= f.input :description,
input_html: { class: 'rounded mt-5', style: 'margin-left: 7px;' } %>
<%= simple_form_for @mythology do |f| %>
<% if @mythology.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@mythology.errors.count, "error") %> prohibited this mythology from being saved:</h2>
<ul>
<% @mythology.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>

<button type="button" data-action="nested-form#remove">Remove mythology</button>
<%= form.hidden_field :_destroy %>
</div>
<div class="field">
<%= f.label :name %>
<%= f.text_field :name %>
</div>

<div class="field">
<%= f.label :description %>
<%= f.text_area :description %>
</div>

<div class="actions">
<%= f.submit "Save Mythology" %>
</div>
<% end %>
2 changes: 2 additions & 0 deletions app/views/mythologies/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,5 @@
class: "px-4 py-4 bg-green-500 hover:bg-green-700 rounded" %>
<%= link_to "List of Stories", stories_path, id: "stories",
class: "px-4 py-4 bg-green-500 hover:bg-green-700 rounded" %>
<%= link_to "New Mythology", new_mythology_path,
class: "px-4 py-4 bg-green-500 hover:bg-green-700 rounded" %>
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
# TODO:
# https://dev.to/pezza/dynamic-nested-forms-with-turbo-3786
end
resources :mythologies, only: [:index, :new, :create]
# Defines the root path route ("/")
root "mythologies#index"
end

0 comments on commit dd00f15

Please sign in to comment.