1

我正在使用 Rails 构建一个事件应用程序,我需要弄清楚用户如何创建将重复发生的事件 - 每天、每周、每月、每年,如果他们愿意的话。从我的研究中尚不完全清楚如何做到这一点,但大多数道路似乎都指向使用 ice_cube gem 和另一个 - 可调度或 recurring_select 用于表单。这是最好的前进方式吗?如果是,哪条是最好的路线,如果不是,最好的选择是什么?最初,我希望表单提供“重复”或“一日”事件的非此即彼的选项。我对所有表单都使用了 simple_form。

这是目前的 Event MVC 位置。

事件.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>
4

1 回答 1

0

麦克风。最快的方法是使用ice_cube + schedulable gems组合。阅读自述文件,您将在几分钟内完成设置。它还具有past_event_occurrencesfuture_event_occurrences.

于 2017-05-30T13:44:12.247 回答