before_action使用示例
module Admin
class InvitesController < Admin::ApplicationController
# 调用 show edit update destroy 方法前调用set_invite 方法
before_action :set_invite, only: [:show, :edit, :update, :destroy]
def index
@invites = Invite.order('id desc')
@invites = @invites.paginate(page: params[:page], per_page: 30)
end
def show
@subscriptions = @invite.subscriptions.paginate(page: params[:page], per_page: 30)
end
def new
@invites = Invite.new
end
def edit
end
def create
@invites = Invite.new(params[:admin_invites_path].permit!)
if @invite.save
redirect_to(admin_invites_url, notice: '创建成功。')
else
render action: "new"
end
end
def update
if @invite.update_attributes(params[:invite].permit!)
redirect_to(admin_invites_url, notice: '更新成功。')
else
render action: "edit"
end
end
def destroy
@invite.destroy
redirect_to(admin_invites_url, notice: "删除成功。")
end
private
def set_invite
@invite = Invite.find(params[:id])
end
end
end
梦里不知身是客,一晌贪欢。