Как создать повторяющиеся события с простой формой и Ruby? Является ли кубик льда лучшим украшением для этого?
Я создаю приложение для событий, используя Rails, и мне нужно выяснить, как пользователи могут создавать события, которые будут повторяться - ежедневно, еженедельно, ежемесячно, ежегодно, если они того пожелают. Из моего исследования не совсем понятно, как это сделать, но большинство дорог, похоже, указывают на использование камня ice_cube вместе с другим - либо планируемым, либо recurring_select для формы. Это лучший путь вперед? Если это так, какой маршрут лучше, а если нет, то какой вариант лучше? Первоначально я хочу, чтобы форма предлагала или / или вариант "повторяющегося" или "однодневного" события. Я использую simple_form для всех моих форм.
Вот позиция Event MVC на данный момент.
Event.rb -
class Event < ActiveRecord::Base
# You can call the scope whatever.
# the focus below is on the :date attribute from the events table - see also controller for index method/action
scope :not_yet_happened, -> { where('date >= ?', Date.current) }
belongs_to :category
belongs_to :user
has_many :bookings
has_many :comments
has_attached_file :image, styles: { medium: "300x300>" }
validates_attachment_content_type :image, :content_type => /\Aimage\/.*\Z/
validates :title, :description, :location, :date, :time, :number_of_spaces, :price_pennies, :category_id, presence: true
validates :title, length: { minimum: 4 }
validates :description, length: { maximum: 250, too_long: "%{count} characters is the maximum allowed" }
monetize :price_pennies
# required for money-rails gem to function
end
events_controller.rb -
class EventsController < ApplicationController
before_action :find_event, only: [:show, :edit, :update, :destroy,]
# the before_actions will take care of finding the correct event for us
# this ties in with the private method below
before_action :authenticate_user!, except: [:index, :show]
# this ensures only users who are signed in can alter an event
def index
if params[:category].blank?
@events = Event.not_yet_happened.order("created_at DESC")
else
@category_id = Category.find_by(name: params[:category]).id
@events = Event.not_yet_happened.where(category_id: @category_id).order("created_at DESC")
end
# The above code = If there's no category found then all the events are listed
# If there is then it will show the EVENTS under each category only
end
def show
end
def new
@event = current_user.events.build
# this now builds out from a user once devise gem is added
# after initially having an argument of Event.new
# this assigns events to users
end
# both update and create actions below use event_params as their argument with an if/else statement
def create
@event = current_user.events.build(event_params)
# as above this now assigns events to users
# rather than Event.new
if @event.save
redirect_to @event, notice: "Congratulations, you have successfully created a new event."
else
render 'new'
end
end
def edit
# edit form
# @edit = Edit.find(params[:id])
@event = current_user.events.find(params[:id])
end
def update
if @event.update(event_params)
redirect_to @event, notice: "Event was successfully updated!"
else
render 'edit'
end
end
def destroy
@event.destroy
redirect_to root_path
end
private
def event_params
params.require(:event).permit(:title, :location, :date, :time, :description, :number_of_spaces, :is_free, :price, :organised_by, :url, :image, :category_id)
# category_id added at the end to ensure this is assigned to each new event created
end
def find_event
@event = Event.find(params[:id])
end
end
_form.html.erb (события) -
<%= simple_form_for(@event, html: { :class => "form-inline" }) do |f| %>
<% if @event.errors.any? %>
<h2><%= pluralize(@event.errors.count, "error") %> prevented this Event from saving:</h2>
<ul>
<% @event.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
<% end %>
<div class="form-group">
<%= f.collection_select :category_id, Category.all, :id, :name, {prompt: "Choose a category"} %>
<!-- The above code loop assigns a category_id to each event -->
</div>
<div class="form-group">
<%= f.input :image, as: :file, label: 'Image', class: "form-control" %>
</div>
<div class="form-group">
<%= f.input :title, label: false, placeholder: 'Title', class: "form-control" %>
</div>
<div class="form-group">
<%= f.text_field :location, id: 'geocomplete', class: "form-control" %>
</div>
<div class="form-group">
<%= f.text_field :date, placeholder: 'Date', id: 'datepicker', class: "form-control" %>
</div>
<div class="form-group">
<%= f.input :time, label: 'What time does the event start?', class: "form-control" %>
</div>
<div class="form-group">
<%= f.label :description %>
<%= f.input_field :description, label: false, class: "form-control" %>
</div>
<div class="form-group">
<%= f.label :number_of_spaces %>
<%= f.text_field :number_of_spaces, label: 'Number of spaces', class: "form-control" %>
</div>
<div class="form-group">
<%= f.input :is_free, label: 'Tick box if Event is free of charge', class: "form-control" %>
<!--f.input :currency, :collection => [['£GBP - British Pounds',1],['$USD - US Dollars',2],['€EUR - Euros',3]] -->
</div>
<div class="form-group">
<%= f.label :cost_per_person %>
<%= f.input :price, label: '(leave blank if free of charge)', class: "form-control" %>
</div>
<div class="form-group">
<%= f.input :organised_by, class: "form-control" %>
</div>
<div class="form-group">
<%= f.input :url, label: "My Website", class: "form-control" %>
</div>
<div class="form-group">
<%= f.button :submit, label: 'Submit', class: "btn btn-primary" %>
<% end %>
</div>
1 ответ
Майк. Самый быстрый способ - использовать комбо ice_cube + schedulable gems. Пройдите через файл Readme, и вы настроитесь за считанные минуты. Это также показывает past_event_occurrences
а также future_event_occurrences
,