url重定义

This commit is contained in:
huang 2017-04-26 14:20:07 +08:00
parent c98d8cdf1d
commit b3c803341f
7 changed files with 51 additions and 13 deletions

View File

@ -216,7 +216,10 @@ class ChallengesController < ApplicationController
def find_shixun
shixun_id = params[:shixun_id] || (params[:challenge] && params[:challenge][:shixun_id])
@shixun = Shixun.find_by_identifier(shixun_id)
render_404 if @shixun.nil?
if @shixun.nil?
render_404
return
end
rescue ActiveRecord::RecordNotFound
render_404
end

View File

@ -163,7 +163,7 @@ class GamesController < ApplicationController
# 下一关自动开启
if had_done != 1 && @game.status == 2
@game.next_game.update_attribute(:status, 0)
@myshixun.update_attribute(:status => 1)
@myshixun.update_attribute(:status, 1)
end
test_sets = game_challenge.test_sets
unless test_sets.blank?
@ -298,7 +298,12 @@ class GamesController < ApplicationController
end
def find_game
@game = Game.find(params[:id])
# myshixun_id = params[:myshixun_id]
@game = Game.find_by_identifier(params[:id])
if @game.nil?
render_404
return
end
@myshixun = @game.myshixun
rescue ActiveRecord::RecordNotFound
render_404

View File

@ -1,7 +1,7 @@
class Game < ActiveRecord::Base
# stauts 0: can exe 1doing 2successed 3:locked
default_scope :order => 'created_at desc'
attr_accessible :myshixun_id, :user_id, :status, :final_score, :challenge_id, :open_time
attr_accessible :myshixun_id, :user_id, :status, :final_score, :challenge_id, :open_time, :identifier
belongs_to :myshixun,:touch=> true
belongs_to :user
belongs_to :challenge
@ -9,6 +9,12 @@ class Game < ActiveRecord::Base
has_many :test_sets, :dependent => :destroy
has_many :challenge_samples, :dependent => :destroy
# id 转换成 challenge'position
def to_param
identifier
end
def last_game
challenge = self.challenge
last_challenge_id = challenge.last_challenge

View File

@ -1,9 +0,0 @@
class SyncIdentifierToMyshixun < ActiveRecord::Migration
def up
myshixuns = Myshixun.all
myshixuns.each do |myshixun|
identifier = myshixun.shixun.try(:identifier)
myshixun.update_attribute(:identifier, identifier)
end
end
end

View File

@ -0,0 +1,5 @@
class AddIdentifierToGames < ActiveRecord::Migration
def change
add_column :games, :identifier, :string
end
end

View File

@ -0,0 +1,14 @@
class SyncGameIdentifier < ActiveRecord::Migration
CODES = %W(2 3 4 5 6 7 8 9 a b c f e f g h i j k l m n o p q r s t u v w x y z)
def up
games = Game.all
games.each do |game|
code = CODES.sample(12).join
code = CODES.sample(12).join if Game.where(identifier: code).present?
game.update_attribute(:identifier, code)
end
end
def down
end
end

View File

@ -0,0 +1,14 @@
class SyncIdentifierToMyshixun < ActiveRecord::Migration
CODES = %W(2 3 4 5 6 7 8 9 a b c f e f g h i j k l m n o p q r s t u v w x y z)
def up
myshixuns = Myshixun.all
myshixuns.each do |myshixun|
code = CODES.sample(10).join
code = CODES.sample(10).join if Myshixun.where(identifier: code).present?
myshixun.update_attribute(:identifier, code)
end
end
def down
end
end