Jump to content

The ultimate community for Ruby on Rails developers.


Photo

redirect_to / set session using params problems

rails forms redirect_to views controllers

  • Please log in to reply
No replies to this topic

#1 Rails_Driver

Rails_Driver

    Passenger

  • Members
  • 5 posts
  • LocationSan Antonio, Texas

Posted 08 January 2014 - 04:42 AM

I have been working with Rails for a couple of months now, so I am still pretty new to it. I would appreciate any help with the issue I am experiencing.
 
I have a rails app, running Rails 3.2.14, and Ruby 2.0.0-p353. This rails app also uses twitter-bootstrap-rails (although this is not related to my issue).
 
I have been asked to "add" some functionality to the app (I am not the original developer), specifically, to add language choices (Español or English) as language options. Currently, the selections in the form show up as query string parameters. Based on setting the params[:lang] => "Español" or "English", I want to set the session[:language] accordingly. 
 
I am looking to set a session-based cookie variable called ":language" to be used throughout the course of the session. Based on session[:language], the app will render the corresponding language text. 
 
I am trying to add a separate form / controller to allow the end user to select their language preference.
 
Here are some details that are of importance to accomplish this:
 
At present, the employee receives a URL via e-mail which they click on, which takes them to the evaluations page (so they can complete the evaluation). IF the end-user's evaluation was flagged to enable multi-language (Spanish / English) support, then @evaluation_template.supports_spanish = 1 (it's in the model). 
 
What I am looking to accomplish is the following:
 
Using the existing controller (DeploymentsController) for the evaluation (deployments#show), I want to redirect the end-user if @evaluation_template.supports_spanish AND if session[:language] (or params[:lang] - whichever is better) has not been set.
 
Also, FYI: params[] is already being used in this app (which why I was thinking about setting params[:lang], and then setting session[:language] based on params[:lang]) - but it isn't a requirement.
 
DeploymentsController #show method: 
  def show
    options = Hash.new
  
    if !params[:job_type].nil?
      options[:job_type] = params[:job_type]
      @job_type_name = params[:job_type]
    end

    @deployment = Deployment.find_by_uuid(params[:id]) || Deployment.find(params[:id])
    @evaluation_template = @deployment.evaluation_instances.first.evaluation_template
    @questions = @evaluation_template.all_questions(options).shuffle

    # I added these lines of code to provide the
    # redirect_to functionality
    if params[:lang].nil? && @evaluation_template.supports_spanish
      redirect_to :controller => 'languages', :action => 'show' and return
    end

    # I added these lines of code to prevent the app from looping
    # between the DeploymentsController and the LanguagesController
    if !params[:lang].nil?
      session[:language] = params[:lang]
    end

    if params.has_key? :exclude
      @filtered_instances = @deployment.evaluation_instances.find_all {|ei| !params[:exclude].include?(ei.team_member.id.to_s)}
    else
      @filtered_instances = @deployment.evaluation_instances
    end

    @is_closed = true
    @deployment.evaluation_instances.each {|instance| @is_closed = false if !instance.is_closed}    

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @deployment }
    end
  end

I have created a new controller (LanguagesController), that has the following:
class LanguagesController < ApplicationController
  # makes this publicly accessible
  skip_before_filter :authenticate_user!, :only => [:show]

  def index
  end

  def show
    options = Hash.new

    @deployment = Deployment.find_by_uuid(params[:id]) || Deployment.find(params[:id])
    @evaluation_template = @deployment.evaluation_instances.first.evaluation_template
  end
end
 
My route to facilitate the new controller / view:
 resources :languages
 get '/languages', to: 'languages#show', as: :set_language
 
and my new form (view) show.html.haml - (languages):
  - if params[:lang].nil?
    #welcome-container
      #row-fluid
        #span12
          .span5{style: "text-align:center;padding-top:30px"}
            = image_tag 'layout/xxxx.png'
          .well.span5
            %h2 Setup / Disposición
            %br
            = form_tag deployment_path(@deployment), :method => :get do
              %p
                %br
                Please select your preferred language:
                %br
                Seleccione su idioma preferido:
                %br
                    = select_tag :lang, "<option>English</option><option>Español</option>".html_safe
              %p
              = submit_tag "Next", :class => 'begin btn btn-large btn-primary'
Current status:
 
I am able to redirect_to successfully, from deployments#show to languages#show.
 
From the new form show.html.haml - (languages), I am able to select a language preference. Once the language preference   is selected, and the form is submitted (the form submits to the deployment_path(@deployment), :method => :get) the       params[:lang] is set, and it also sets session[:language] => "Spanish" or "English" accordingly.
 
When the show.html.haml - (deployments) form is rendered, I can continue to select the remaining evaluation options (which I still need to modify for the corresponding language selection):
 
show.html.haml - (deployments)
 
  - if params[:job_type].nil?
  #welcome-container
    #row-fluid
      #span12
        .span5{style: "text-align:center;padding-top:30px"}
          = image_tag 'layout/xxxx.png'
        .well.span5
          %h2 Setup
           = form_tag deployment_path(@deployment, :anchor => "question/1"), :method => :get do
            %p
              Please select the position of the evaluator:
              %br
              = select_tag 'job_type', @evaluation_template.all_job_types.collect {|job_type_name| "<option value='#{job_type_name}'>#{job_type_name}"}.join('').html_safe
            %p
              Please select any team members you would like to exclude from this survey:
              %br
              - @deployment.evaluation_instances.each do |instance|
                = check_box_tag 'exclude[]', instance.team_member.id
                = instance.team_member.name
                %br
            = submit_tag "Begin evaluation", :class => 'begin btn btn-large btn-primary'
When I select the job_type, and submit this form, I end up at the wrong path (and consequently, the params[:lang] and session[:language] are not set). I end up at:
  /languages/88#question/1
As opposed to where I should end up (based on the form_tag above):
 
= form_tag deployment_path(@deployment, :anchor => "question/1"), :method => :get do
I know I am missing something basic here, but I cannot figure it out.
 
Any help would be greatly appreciated. If you need any other info, please let me know.
 
Thanks in advance.






Also tagged with one or more of these keywords: rails, forms, redirect_to, views, controllers

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users