You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
educoder/app/controllers/trustie_hacks_controller.rb

73 lines
1.6 KiB

5 years ago
class TrustieHacksController < ApplicationController
5 years ago
before_action :require_admin, :except => [:index, :entry]
5 years ago
before_action :require_login, :except => [:index]
before_action :find_hackathon
5 years ago
before_action :find_hack, :except => [:create, :index, :edit_hackathon, :update_hackathon]
5 years ago
def index
## 分页参数
page = params[:page] || 1
limit = params[:limit] || 16
search = params[:search]
5 years ago
hacks = @hackathon.trustie_hacks
if search
hacks = hacks.where("name like ?", "%#{search}%")
end
@hackathon_users_count = hacks.blank? ? 0 : hacks.sum(:hack_users_count)
5 years ago
@hacks_count = hacks.count
5 years ago
@hacks = hacks.page(page).per(limit)
end
5 years ago
def edit;end
5 years ago
def create
@hackathon.trustie_hacks.create!(name: params[:name], description: params[:description])
render_ok
end
def update
@hack.update_attributes(name: params[:name], description: params[:description])
5 years ago
render_ok
5 years ago
end
def destroy
@hack.destroy
render_ok
end
5 years ago
def edit_hackathon
end
5 years ago
def update_hackathon
@hackathon.update_attributes(name: params[:name], description: params[:description])
5 years ago
render_ok
5 years ago
end
# 报名入口
5 years ago
def entry
5 years ago
if @hack.hack_users.exists?(user_id: current_user.id)
5 years ago
render_error('已经报名,请勿重复操作')
else
5 years ago
@hack.hack_users.create(user_id: current_user.id)
5 years ago
render_ok
end
end
5 years ago
private
def find_hackathon
@hackathon = TrustieHackathon.first ||
5 years ago
TrustieHackathon.create!(name: params[:name], description: params[:description])
5 years ago
end
def find_hack
@hack = TrustieHack.find params[:id]
end
end