4 Replies Latest reply on Oct 31, 2013 9:14 AM by Ruben Ruben

    Avoid redirect to index after create

    Ruben Ruben

      Hi,

       

      I'm building a Rhom model that depending on one of it attributes, should be displayed in different index views. I created two index files: index_first.erb and index_second.erb. This works perfectly, but I also have two new views: new_first.erb and new_second.erb, They both call create to make the entries on the database, but after that, in both cases, I get a 404 telling me that there is no index method or view. After create, it should go to index_first or index_second, depending on the attribute:

       

      if record_type == "first"

           redirect :action => :index_first

      else

           redirect :action => :index_second

       

      Why doesn't it work, and instead, redirect me to index always?

       

      Thanks.

        • Re: Avoid redirect to index after create
          Mark Nongkhlaw

          Can you post your controller code for create?

            • Re: Avoid redirect to index after create
              Ruben Ruben

              # POST /Uurdet/create

                def create

                 

                  # Check if form parameters are valid

                  if !valid?(@params['date'], @params['start'], @params['end'], @params['uurdet']["omschrijving"], @params['uurdet']["record_type"])

                    err_message = ''  

                    @form_validity[:error].each do |err|

                      err_message = err_message + err

                    end

                    Alert.show_popup :title => "Form input error", :message => err_message, :buttons => [{:id => 'cancel', :title => 'Dismiss'}]

                    if @params['uurdet']["record_type"] == "planning"

                      render :action => :new_planning

                    elsif @params['uurdet']["record_type"] == "uurbon"

                      render :action => :new_uurbon

                    end

                  end 

                 

                  # ...

                  # ...  Here goes lots of code to fill new_uurdet (hash containing everything that will go to the local db)

                  # ...

               

                  # Send the data to the local database 

                  @uurdet = Uurdet.create(new_uurdet)

                 

                  # We reset the variables that contain the hour for the form:

                  $choosed = {} 

                 

                  # We check if we came from new_uurbon form or new_planning (They send a hidden input with "planning" or "uurbon")

                  if @params['uurdet']["record_type"] == "planning"  

                    redirect :action => :index_planning

                  else

                    redirect :action => :index_uurbon

                  end

                end

               

              # Doesnt work, It still tries to redirect me to index (I can see on the Web Inspector's console a 404 error telling me it cant find index view or controller method