This is also for studying Ruby on Rails. If you have any suggestions, such as something wrong or something that should be improved, please let me know. The reference book is "Ruby on Rails 5 Quick Learning Practice Guide that can be used in the field". Let's do our best.
OS:MacOS Catalina10.15.5 Ruby:2.6.3 Rails:5.2.4
$ rails new Mark -d postgresql
$ rails g model Event
Rails models consist of two main components
-Ruby class corresponding to the model
-Database table corresponding to the model
Class names and table names have the following naming conventions. -The database table name is a plural form of the model class name. ・ The model class name is camel case, and the table name is snake case.
Meaning of attributes | Attribute name / column name | Data type |
---|---|---|
title | title | string |
The beginning | start | datetime |
the end | end | datetime |
All day | allday | boolean |
Calendar color | color | string |
For the time being, like this.
db/migrate/*******_create_events.rb
class CreateEvents < ActiveRecord::Migration[6.0]
def change
create_table :events do |t|
t.string :title, null: false
t.datetime :start, null: false
t.datetime :end, null: false,
t.boolean :allday, null: false, default: false
t.string :color, null: false
t.timestamps
end
end
end
$ rails db:migrate
Apply migration to database.
$bin/rails g controller events index show new edit
The controller name is the plural of the model. Add the required action name after that.
config/routes.rb
Rails.application.routes.draw do
root to: 'events#index'
resources :events
end
Added to Gemfile.
gem 'fullcalendar-rails'
gem 'momentjs-rails'
$ bundle install
Added to application.js and application.css
assets/javascripts/application.js
//= require jquery
//= require moment
//= require fullcalendar
//= require_tree .
assets/stylesheets/application.css
*= require fullcalendar
*/
Write the code in application.js.
assets/javascripts/application.js
$(document).ready(function() {
$('#calendar').fullCalendar({
events: '/events.json'
});
});
Added so that it can be displayed in view.
app/view/events/index.html.erb
<div id="calendar"></div>
Try to start it with rails s.
Oh, it's done. I've just displayed it, so let's make the contents from now on. ..
https://qiita.com/sasasoni/items/fb0bc1644ece888ae1d4 https://qiita.com/ShoutaWATANABE/items/3d0cddafadb4f275991e https://fullcalendar.io/