我知道,如果我會寫「redirect_to」而不是「render:action」,我會失去我的@對象的錯誤。對象的錯誤和重定向
我的代碼:
def play
@room = params[:id][0,1]
@current_players = CurrentPlayer.all(:conditions => {:room => @room})
end
def join
@new_player = CurrentPlayer.new(:user_id => current_user.id, :team_id => nil, :room => params[:room], :mode => params[:mode], :ip => request.remote_ip)
if @new_player.save
@game = create_game(params[:room], params[:mode])
if @game
flash[:notice] = 'Game created'
return (redirect_to :action => "game_details", :recent => true)
else
flash[:error] = 'Game not created'
end
else
return render(:action => 'play')
end
redirect_to_back
end
如果用戶點擊「加入」在play.html.erb鏈接,Rails會發送請求「加入」行動,那麼,如果有錯誤,我需要向用戶展示他們。
但我不能寫只是redirect_to時,我的代碼現在:
def join
@new_player = CurrentPlayer.new(:user_id => current_user.id, :team_id => nil, :room => params[:room], :mode => params[:mode], :ip => request.remote_ip)
if @new_player.save
@game = create_game(params[:room], params[:mode])
if @game
flash[:notice] = 'Game created'
return (redirect_to :action => "game_details", :recent => true)
else
flash[:error] = 'Game not created'
end
else
# == Very ugly and not DRY (just copypaste from 'play' action) ==
@room = params[:id][0,1]
@current_players = CurrentPlayer.all(:conditions => {:room => @room})
# ===
return render(:action => 'play')
end
redirect_to_back
end
我怎樣才能避免這種情況的代碼?
這個問題與'error_messages_for'有什麼關係? – 2012-01-02 22:00:24