Skip to content

Commit

Permalink
Return 404 on ActiveRecord not found instead of 500
Browse files Browse the repository at this point in the history
  • Loading branch information
syndbg committed Oct 14, 2015
1 parent e9d90d3 commit 1d68ecc
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 24 deletions.
7 changes: 3 additions & 4 deletions app/controllers/admin_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@ def dashboard
private

def authenticate
if Rails.env.production?
authenticate_or_request_with_http_basic do |username, password|
username == ENV['ADMIN_USERNAME'] && password == ENV['ADMIN_PASSWORD']
end
return unless Rails.env.production?
authenticate_or_request_with_http_basic do |username, password|
username == ENV['ADMIN_USERNAME'] && password == ENV['ADMIN_PASSWORD']
end
end
end
21 changes: 8 additions & 13 deletions app/controllers/questions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,9 @@ def create
@question.save!

params[:options].each do |option|
if option[:title] != ''
new_option = Option.new
new_option.title = option[:title]
new_option.question_id = @question.id
new_option.save!
end
next unless option[:title] != ''
new_option = Option.new(title: option[:title], question_id: @question.id)
new_option.save!
end

redirect_to "/#{@question.secret}"
Expand All @@ -40,19 +37,17 @@ def check_secret_availability
private

def set_question
@question = Question.where(secret: params[:secret]).first
@question = Question.find_by_secret!(params[:secret])
end

def question_params
params.require(:question).permit(:title, :secret)
end

def check_secret_is_unique
if defined? params[:question][:secret]
if Question.where({ secret: params[:question][:secret] }).exists?
@question = Question.new(question_params)
redirect_to :back, notice: 'Sorry that URL is taken'
end
end
return unless defined? params[:question][:secret] &&
Question.where(secret: params[:question][:secret]).exists?
@question = Question.new(question_params)
redirect_to :back, notice: 'Sorry that URL is taken'
end
end
14 changes: 7 additions & 7 deletions app/controllers/votes_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,20 @@ def update
vote_id = cookies["vote_#{question_id}"]
vote = Vote.where(secret: vote_id).first_or_initialize

question = Question.where(secret: question_id).first
question = Question.find_by_secret!(question_id)
vote.secret = SecureRandom.urlsafe_base64(nil, false) unless vote.secret
vote.question_id = question.id unless vote.question_id
vote.option_id = Option.find(params[:vote][:option_id]).id

Pusher[question_id].trigger('vote', {})

if vote.save!
cookies.permanent["vote_#{question.secret}"] = vote.secret
return unless vote.save!

respond_to do |format|
format.html { redirect_to "/#{question.secret}" }
format.json { render json: {}, status: :created }
end
cookies.permanent["vote_#{question.secret}"] = vote.secret

respond_to do |format|
format.html { redirect_to "/#{question.secret}" }
format.json { render json: {}, status: :created }
end
end

Expand Down

0 comments on commit 1d68ecc

Please sign in to comment.