Snowman
Snowman

Reputation: 209

Can't seeem to keep form data after submit ROR

Question I have a rails app that I use to print labels. No matter what I have tried my select_tag will always default back to the first choice. I would like it to keep the choose I selected after the page reload/submit. Any help would be greatly appreciated.

Here is my form

= form_tag label_print_sessions_path, :method => 'get', :class => "form-horizontal" do
  .row-fluid
   .span6
     =render "form_entry", :field_name => "print_job"
     = text_field_tag :type, "manual", :style => 'display:none;'
   .span6
     =render "form_entry", :field_name => "description"
  .row-fluid
   .span6
      =render "form_entry", :field_name => "total_qty"
    .span6
      =render "form_entry", :field_name => "qty_per_label"

These are the select tags that I want to keep the value selected after submitting the page.

  .row-fluid
    .span6
      .control-group
        = label_tag :printer, "Select a Printer: ", :class => 'control-label'
       .controls
        = select_tag :printer, options_for_select([["Printer 1", "10040"], ["Prototype", "10080"]], :selected => params[:printer], :input_html => {:value => ''})
    .span6
      .control-group
         = label_tag :label_format, "Select a Format: ", :class => 'control-label'
        .controls
         = select_tag :label_format, options_for_select([["T-1. fmt", "11381"], ["G-2. fmt", "11380"], ["U-1. fmt", "11420"]], :selected =>  params[:label_format], :input_html => {:value => ''})

Controller

def label_print
  if params[:printer] && params[:label_format]
    current.add_print_params(params)
    @printer = current.printer
    @label = current.label_format
    winpath = @label.windows_path

    if current.qty_per_label == "0" 
      flash[:error] = "Qty Per Label cannot be blank"
      redirect_to label_setup_sessions_path
    else

      File.open("/tmp/print_job.xml", "w") do |f|
        f.puts '<?xml version="1.0" encoding="utf-8" standalone="no"?>'
        f.puts '<!DOCTYPE labels SYSTEM "label.dtd">'
        f.puts '<labels>'
        current.number_labels.times do |x|

          f.puts '  <label _FORMAT="' + winpath + '" _QUANTITY="1" _DUPLICATES="1" _JOBNAME="Labels for #{current.user}" _PRINTERNAME="' + @printer.name + '">'
          f.puts '    <variable name="qty">' + current.label_qty(x) + '</variable>'

          f.puts '    <variable name="description">' + current.description + '</variable>'
          f.puts '  </label>'
          f.puts ''
        end
        f.puts '</labels>'
      end
      FileUtils.mv('/tmp/print_job.xml', '/mnt/shares/easylabel/print_job.xml')
      gflash :success => { :value => "Labels sent to printer!", :time => 5000 }
      redirect_to label_setup_sessions_path
    end
  else
    flash[:error] = 'You should not be on this page. General Error.'
    redirect_to root_url
  end
end

Upvotes: 2

Views: 278

Answers (2)

JGutierrezC
JGutierrezC

Reputation: 4513

You are using :selected option, you could only put it as second argument for options_for_select. the real issue here is that you are using redirect_to label_setup_sessions_path and the params[:printer] is nil.

Instead use label_setup_sessions_path(params[:printer])

This way the url will be something like 127.0.0.1:3000/label_setup?printer=weathever and params[:printer] won't be nil

Let me know if you have doubts

Upvotes: 1

vich
vich

Reputation: 11896

Per the Rails docs, you don't need to pass the :selected option:

= select_tag :printer, options_for_select([["Printer 1", "10040"], ["Prototype", "10080"]], params[:printer])

And unless you have a very good reason, I would use POST or PATCH/PUT instead of GET for your form.

Upvotes: 1

Related Questions