1. 程式人生 > >Rails的CRUD程式中Action的基本寫法彙總

Rails的CRUD程式中Action的基本寫法彙總

七大action,分別為index、show、edit、update、new、create、destroy

def index
  @events = Event.all
end

def new
  @event = Event.new
end

def create
  @event = Event.new(params[:event])
  @event.save

  redirect_to :action => :index
end

def show
  @event = Event.find(params[:id])
end

def edit
  @event = Event.find(params[:id])
end

def update
  @event = Event.find(params[:id])
  @event.update_attributes(params[:event])

  redirect_to :action => :show, :id => @event
end

def destroy
  @event = Event.find(params[:id])
  @event.destroy

  redirect_to :action => :index
end