提交ROR后无法保留表单数据

时间:2015-08-10 15:48:23

标签: ruby-on-rails ruby ruby-on-rails-3

问题我有一个用于打印标签的rails应用程序。无论我尝试过什么,我的select_tag总是会默认回到第一个选择。我想在页面重新加载/提交后保留我选择的选择。任何帮助将不胜感激。

这是我的表格

= 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"

这些是我想在提交页面后保持选择值的选择标记。

  .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 => ''})

控制器

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

2 个答案:

答案 0 :(得分:1)

根据Rails docs,您无需传递:selected选项:

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

除非你有充分的理由,否则我会使用POSTPATCH/PUT代替GET作为您的表单。

答案 1 :(得分:1)

您正在使用:已选择的选项,您只能将其作为options_for_select的第二个参数。这里真正的问题是你使用redirect_to label_setup_sessions_pathparams[:printer]是零。

而是使用label_setup_sessions_path(params[:printer])

这样,网址就像127.0.0.1:3000/label_setup?printer=weathever和params [:printer]一样不会是

如果您有疑问,请告诉我