From 8185939b2629581dd9264dbbc37f859e19436f31 Mon Sep 17 00:00:00 2001 From: Matthew Lemon Date: Mon, 18 Dec 2023 19:38:44 +0000 Subject: wip: stuck on notnull error... --- app/controllers/events_controller.rb | 2 +- app/controllers/organisations_controller.rb | 70 ++++++++++++++++++++++ app/helpers/organisations_helper.rb | 2 + app/models/event.rb | 2 + app/models/organisation.rb | 3 + app/views/events/_form.html.erb | 5 ++ app/views/organisations/_form.html.erb | 22 +++++++ app/views/organisations/_organisation.html.erb | 7 +++ .../organisations/_organisation.json.jbuilder | 2 + app/views/organisations/edit.html.erb | 10 ++++ app/views/organisations/index.html.erb | 14 +++++ app/views/organisations/index.json.jbuilder | 1 + app/views/organisations/new.html.erb | 9 +++ app/views/organisations/show.html.erb | 10 ++++ app/views/organisations/show.json.jbuilder | 1 + 15 files changed, 159 insertions(+), 1 deletion(-) create mode 100644 app/controllers/organisations_controller.rb create mode 100644 app/helpers/organisations_helper.rb create mode 100644 app/models/organisation.rb create mode 100644 app/views/organisations/_form.html.erb create mode 100644 app/views/organisations/_organisation.html.erb create mode 100644 app/views/organisations/_organisation.json.jbuilder create mode 100644 app/views/organisations/edit.html.erb create mode 100644 app/views/organisations/index.html.erb create mode 100644 app/views/organisations/index.json.jbuilder create mode 100644 app/views/organisations/new.html.erb create mode 100644 app/views/organisations/show.html.erb create mode 100644 app/views/organisations/show.json.jbuilder (limited to 'app') diff --git a/app/controllers/events_controller.rb b/app/controllers/events_controller.rb index 058486d..d819632 100644 --- a/app/controllers/events_controller.rb +++ b/app/controllers/events_controller.rb @@ -67,6 +67,6 @@ class EventsController < ApplicationController # Only allow a list of trusted parameters through. def event_params - params.require(:event).permit(:date, :name) + params.require(:event).permit(:date, :name, :organisation_id) end end diff --git a/app/controllers/organisations_controller.rb b/app/controllers/organisations_controller.rb new file mode 100644 index 0000000..dbff777 --- /dev/null +++ b/app/controllers/organisations_controller.rb @@ -0,0 +1,70 @@ +class OrganisationsController < ApplicationController + before_action :set_organisation, only: %i[ show edit update destroy ] + + # GET /organisations or /organisations.json + def index + @organisations = Organisation.all + end + + # GET /organisations/1 or /organisations/1.json + def show + end + + # GET /organisations/new + def new + @organisation = Organisation.new + end + + # GET /organisations/1/edit + def edit + end + + # POST /organisations or /organisations.json + def create + @organisation = Organisation.new(organisation_params) + + respond_to do |format| + if @organisation.save + format.html { redirect_to organisation_url(@organisation), notice: "Organisation was successfully created." } + format.json { render :show, status: :created, location: @organisation } + else + format.html { render :new, status: :unprocessable_entity } + format.json { render json: @organisation.errors, status: :unprocessable_entity } + end + end + end + + # PATCH/PUT /organisations/1 or /organisations/1.json + def update + respond_to do |format| + if @organisation.update(organisation_params) + format.html { redirect_to organisation_url(@organisation), notice: "Organisation was successfully updated." } + format.json { render :show, status: :ok, location: @organisation } + else + format.html { render :edit, status: :unprocessable_entity } + format.json { render json: @organisation.errors, status: :unprocessable_entity } + end + end + end + + # DELETE /organisations/1 or /organisations/1.json + def destroy + @organisation.destroy! + + respond_to do |format| + format.html { redirect_to organisations_url, notice: "Organisation was successfully destroyed." } + format.json { head :no_content } + end + end + + private + # Use callbacks to share common setup or constraints between actions. + def set_organisation + @organisation = Organisation.find(params[:id]) + end + + # Only allow a list of trusted parameters through. + def organisation_params + params.require(:organisation).permit(:name) + end +end diff --git a/app/helpers/organisations_helper.rb b/app/helpers/organisations_helper.rb new file mode 100644 index 0000000..1b84554 --- /dev/null +++ b/app/helpers/organisations_helper.rb @@ -0,0 +1,2 @@ +module OrganisationsHelper +end diff --git a/app/models/event.rb b/app/models/event.rb index 9dea7b8..aaa4e6e 100644 --- a/app/models/event.rb +++ b/app/models/event.rb @@ -2,6 +2,8 @@ class Event < ApplicationRecord validates :name, presence: true, length: { maximum: 50 } validates :date, presence: true + belongs_to :organisation, optional: true + def month_year_check(year, month) if self.date.year == year && self.date.month == month true diff --git a/app/models/organisation.rb b/app/models/organisation.rb new file mode 100644 index 0000000..aa67564 --- /dev/null +++ b/app/models/organisation.rb @@ -0,0 +1,3 @@ +class Organisation < ApplicationRecord + has_many :events +end diff --git a/app/views/events/_form.html.erb b/app/views/events/_form.html.erb index 2a41eec..98dd972 100644 --- a/app/views/events/_form.html.erb +++ b/app/views/events/_form.html.erb @@ -36,6 +36,11 @@ <%= form.label "Event name", style: "display: block" %> <%= form.text_field :name %> + +
+ <%= form.label "Organisation", style: "display: block" %> + <%= form.select :organisation, Organisation.all.collect { |o| [ o.name, o.id ] }, include_blank: true %> +
<%= form.submit style: "background: green; color: white; margin-top: 1.1em;"%> diff --git a/app/views/organisations/_form.html.erb b/app/views/organisations/_form.html.erb new file mode 100644 index 0000000..ebe8ea3 --- /dev/null +++ b/app/views/organisations/_form.html.erb @@ -0,0 +1,22 @@ +<%= form_with(model: organisation) do |form| %> + <% if organisation.errors.any? %> +
+

<%= pluralize(organisation.errors.count, "error") %> prohibited this organisation from being saved:

+ +
    + <% organisation.errors.each do |error| %> +
  • <%= error.full_message %>
  • + <% end %> +
+
+ <% end %> + +
+ <%= form.label :name, style: "display: block" %> + <%= form.text_field :name %> +
+ +
+ <%= form.submit %> +
+<% end %> diff --git a/app/views/organisations/_organisation.html.erb b/app/views/organisations/_organisation.html.erb new file mode 100644 index 0000000..c5ea553 --- /dev/null +++ b/app/views/organisations/_organisation.html.erb @@ -0,0 +1,7 @@ +
+

+ Name: + <%= organisation.name %> +

+ +
diff --git a/app/views/organisations/_organisation.json.jbuilder b/app/views/organisations/_organisation.json.jbuilder new file mode 100644 index 0000000..e4d8f92 --- /dev/null +++ b/app/views/organisations/_organisation.json.jbuilder @@ -0,0 +1,2 @@ +json.extract! organisation, :id, :name, :created_at, :updated_at +json.url organisation_url(organisation, format: :json) diff --git a/app/views/organisations/edit.html.erb b/app/views/organisations/edit.html.erb new file mode 100644 index 0000000..95bea56 --- /dev/null +++ b/app/views/organisations/edit.html.erb @@ -0,0 +1,10 @@ +

Editing organisation

+ +<%= render "form", organisation: @organisation %> + +
+ +
+ <%= link_to "Show this organisation", @organisation %> | + <%= link_to "Back to organisations", organisations_path %> +
diff --git a/app/views/organisations/index.html.erb b/app/views/organisations/index.html.erb new file mode 100644 index 0000000..45d5b16 --- /dev/null +++ b/app/views/organisations/index.html.erb @@ -0,0 +1,14 @@ +

<%= notice %>

+ +

Organisations

+ +
+ <% @organisations.each do |organisation| %> + <%= render organisation %> +

+ <%= link_to "Show this organisation", organisation %> +

+ <% end %> +
+ +<%= link_to "New organisation", new_organisation_path %> diff --git a/app/views/organisations/index.json.jbuilder b/app/views/organisations/index.json.jbuilder new file mode 100644 index 0000000..4490dc6 --- /dev/null +++ b/app/views/organisations/index.json.jbuilder @@ -0,0 +1 @@ +json.array! @organisations, partial: "organisations/organisation", as: :organisation diff --git a/app/views/organisations/new.html.erb b/app/views/organisations/new.html.erb new file mode 100644 index 0000000..a749bc8 --- /dev/null +++ b/app/views/organisations/new.html.erb @@ -0,0 +1,9 @@ +

New organisation

+ +<%= render "form", organisation: @organisation %> + +
+ +
+ <%= link_to "Back to organisations", organisations_path %> +
diff --git a/app/views/organisations/show.html.erb b/app/views/organisations/show.html.erb new file mode 100644 index 0000000..3de432b --- /dev/null +++ b/app/views/organisations/show.html.erb @@ -0,0 +1,10 @@ +

<%= notice %>

+ +<%= render @organisation %> + +
+ <%= link_to "Edit this organisation", edit_organisation_path(@organisation) %> | + <%= link_to "Back to organisations", organisations_path %> + + <%= button_to "Destroy this organisation", @organisation, method: :delete %> +
diff --git a/app/views/organisations/show.json.jbuilder b/app/views/organisations/show.json.jbuilder new file mode 100644 index 0000000..c228d36 --- /dev/null +++ b/app/views/organisations/show.json.jbuilder @@ -0,0 +1 @@ +json.partial! "organisations/organisation", organisation: @organisation -- cgit v1.2.3