Implement creation of announcements

This commit is contained in:
Karina Kwiatek 2020-04-19 20:34:48 +01:00
parent 8a632a09cd
commit e3b89f7346
3 changed files with 35 additions and 2 deletions

View file

@ -4,9 +4,18 @@ class AnnouncementController < ApplicationController
end
def new
@announcement = Announcement.new
end
def create
@announcement = Announcement.new(announcement_params)
@announcement.user = current_user
if @announcement.save
flash[:success] = "Announcement created successfully."
redirect_to action: :index
else
render 'announcement/new'
end
end
def edit
@ -17,4 +26,10 @@ class AnnouncementController < ApplicationController
def destroy
end
private
def announcement_params
params.require(:announcement).permit(:content, :link_text, :link_href, :starts_at, :ends_at)
end
end

View file

@ -1,2 +1,3 @@
class Announcement < ApplicationRecord
belongs_to :user
end

View file

@ -1,2 +1,19 @@
<h1>Announcement#new</h1>
<p>Find me in app/views/announcement/new.html.erb</p>
- provide(:title, generate_title("Add new announcement"))
.container.j2-page
= bootstrap_form_for(@announcement, url: {action: "create"}) do |f|
.row
.col-md-12
= f.text_area :content, label: "Content"
.row
.col-md-6
= f.url_field :link_href, label: "Link URL"
.col-md-6
= f.datetime_field :link_text, label: "Link text"
.row
.col-md-6
= f.datetime_field :starts_at, label: "Start time"
.col-md-6
= f.datetime_field :ends_at, label: "End time"
.row
.col-md-12.text-right
= f.submit class: "btn btn-primary"