私はイベントサイトを構築しています。インデックスページには、すべてのイベント(画像、タイトル、日付で表されます)が表示されます。インデックスページからイベントを削除するためにどのコードを書く必要があるかを知りたいと思います。検証方法を書いていますか?それはモデル/コントローラの方法ですか?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
それが起こったらインデックスページからイベントを削除するコードを書く必要があります。また、作成したすべてのユーザーイベントを表示するユーザープロファイルページもあります。これにより、すべてのイベントのアーカイブとして機能します。削除が必要なインデックスページのみです。
このように、add_happened_to_eventsのような移行を行う必要があります:ブール値を超えてから、これを実行しますか? –
デフォルト値:falseを追加して、ゼロ値を避けることもできます。 – siopao
nullを追加する必要がありますか?移行にもfalseですか? –