2016-04-11 13 views
1

私はRailsを使ってイベントアプリを構築しています。ユーザーは毎日、毎週、毎月、年1回、定期的にイベントを作成する方法を理解する必要があります。 私の研究でこれを行う方法は完全にはっきりしていませんが、ほとんどの道路はice_cube宝石を別のものと共に使用することを指摘しています - スケジュール用かrecurring_selectか。 これが最善の方法ですか?もしそうなら、どちらが最善のルートですか、そうでない場合は、最良の選択肢は何ですか? 最初に、フォームに「定期的」または「1日」のいずれかのオプションを提供します。私はすべてのフォームにsimple_formを使用しています。簡単なフォームとRubyで繰り返しイベントを作成するにはどうすればよいですか?アイスキューブはこれにとって最高の宝石ですか?

現時点でのイベント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> 
+0

私はずっと前にサンプルアイスキューブアプリを作ったhttps://github.com/mzaragoza/rails-fullcalendar-icecube – MZaragoza

答えて

0

マイク。最も速い方法は、ice_cube + schedulable gemsコンボを使用することです。 Readmeを読むと分単位で設定されます。また、past_event_occurrencesfuture_event_occurrencesがあります。

関連する問題