2016-07-07 4 views
0

私はイベントサイトを構築しています。インデックスページには、すべてのイベント(画像、タイトル、日付で表されます)が表示されます。インデックスページからイベントを削除するためにどのコードを書く必要があるかを知りたいと思います。検証方法を書いていますか?それはモデル/コントローラの方法ですか?Rails - インデックスページのイベント情報を削除します(発生後)

ここに私の現在のイベントモデルとコントローラのコードだ - 私はorder.I降順を経由して、イベントを設定しているコントローラで

Event.rb

class Event < ActiveRecord::Base 

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.all.order("created_at DESC") 
    else 
     @category_id = Category.find_by(name: params[:category]).id 
     @events = Event.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 

それが起こったらインデックスページからイベントを削除するコードを書く必要があります。また、作成したすべてのユーザーイベントを表示するユーザープロファイルページもあります。これにより、すべてのイベントのアーカイブとして機能します。削除が必要なインデックスページのみです。

答えて

1

timeという名前のdatetimeフィールドを追加します。イベントを作成するときは、:time属性をイベントが発生するタイミングに設定するようにしてください。

これを済ませたら、イベントモデルにスコープを追加できます。あなたのindexアクションで

# You can call the scope whatever. 
# :happened is the boolean field on the events table. 
scope :not_yet_done, -> { where('time >= ?', Time.current) } 

は:

def index 
    if params[:category].blank? 
    @events = Event.not_yet_done.order("created_at DESC") 
    else 
    @category_id = Category.find_by(name: params[:category]).id 
    @events = Event.not_yet_done.where(category_id: @category_id).order("created_at DESC") 
    end 
end 
+0

このように、add_happened_to_eventsのような移行を行う必要があります:ブール値を超えてから、これを実行しますか? –

+0

デフォルト値:falseを追加して、ゼロ値を避けることもできます。 – siopao

+0

nullを追加する必要がありますか?移行にもfalseですか? –

0

あなたはちょうどそれと同じ方法であり、余分な引数を渡すと、それを達成することができます。日付または時刻が挿入されたカテゴリid(現在の日付または時刻よりも大きい)のイベントがチェックされます。

def index 
    if params[:category].blank? 
     @events = Event.all.order("created_at DESC") 
    else 
     @category_id = Category.find_by(name: params[:category]).id 
     @events = Event.where(category_id: @category_id, time: ">= Time.now.zone").order("created_at DESC") 
    end 
end 
+0

こんにちは、このコードは私に構文エラーを教えてくれますか? =>に焦点を当てていますか? –

+0

これを修正しましたが、私は上記のコードが必要なことをしないのではないかと心配しています。 –

+0

更新しました。はい !だから、私は他の答えのように別の移行が必要だと思います。 – tworitdash

関連する問題