dimanche 22 mai 2016

how to run a method multiple times in Rails controller

I'm building a game app and I want my game to move to the next level when the user got the right answer. Now my code can only move from level 1 to level 2, but from level 2, it cannot move to level 3. Here's my code:

class Game1Controller < ApplicationController

    def index
    end

    def play

        @game1 = Game1lv.find(params[:level])

        @game1l = Game1lv.where(:level => @game1).limit(1).pluck(:imagelink)
        @game1a = Game1lv.where(:level => @game1).limit(1).pluck(:answer)
        @game1link = @game1l[0].to_s
        @game1answer = @game1a[0].to_s

        @game1answer_user = params["answer"]

        if @game1answer_user == @game1answer

            redirect_to game1_play_path(@game1.level += 1), :flash => { :success => "You are right! Now try the next question" }

        end

    end

    def instruction
    end

end

I tried to use for loop inside my play method, but rails prompted the error "Render and/or redirect were called multiple times in this action.". Please show me how to solve this. Thanks.

Aucun commentaire:

Enregistrer un commentaire