Rails表单在发生错误后继续发送旧值

时间:2017-08-20 09:19:27

标签: ruby-on-rails haml

我有一个表单,出于某种原因,在出错后总是保持相同的值。 例如,如果我输入已经存在的名称,它将会出现错误,同时即使您更改它也会继续发送旧值(因此您必须刷新页面并丢失所有更改以实际解决错误)。

   = provide(:title, t(:create_task))
.center
  = form_for :task, url: tasks_path do |f|
    = render 'shared/big_flash'
    = render 'task_error_messages'
    .row.text-center
      .col-lg-10.col-lg-offset-1.col-md-12.col-sm-12.col-xs-12
        .jumbotron
          %h2= t(:create_task)
          .row
            .col-lg-6.col-md-6.col-sm-6.col-xs-12
              = f.label t(:par_task_name)
              %i{class: 'material-icons text-muted', rel: 'tooltip', title: t(:tt_task_new_name)} info_outline
              = f.text_field 'name', maxlength: 255, class: 'form-control'

              = f.label t(:par_category)
              = f.collection_select 'category', Category.order(:name), :name, :name, {}, {class: 'form-control'}
              = link_to t(:btn_modify_categories), categories_modify_path, class: 'btn btn-primary btn-block', data: { confirm: t(:changes_lost_proceed) }

              = f.label t(:par_task_type)
              = f.select 'task_type', Task::TASK_TYPES_LIST, {include_blank: true}, {class: 'form-control', id: 'type_of_task'}

              = f.label t(:par_score)
              = f.number_field 'score', min: '0', step: '1', class: 'form-control'

            .col-lg-6.col-md-6.col-sm-6.col-xs-12
              = f.label t(:par_asset)
              %i{class: 'material-icons text-muted', rel: 'tooltip', title: t(:tt_task_asset)} info_outline
              = f.file_field 'category'

          .description
            = f.label t(:par_description)
            %i{class: 'material-icons text-muted', rel: 'tooltip', title: t(:tt_task_description)} info_outline
            = f.text_area 'name', class: 'form-control'

            = f.check_box 'mathjax'
            = f.label t(:par_mathjax)
            %i{class: 'material-icons text-muted', rel: 'tooltip', title: t(:tt_task_mathjax)} info_outline

          .row
            .correct_solutions.col-lg-6.col-md-6.col-sm-6.col-xs-12
              = f.label t(:par_correct_solutions)
              #generated_correct_solutions
              = button_tag t(:btn_add_correct_solution), type: 'button', class: 'btn btn-primary btn-toolbar', id: 'add_correct_solution'

            .wrong_solutions.col-lg-6.col-md-6.col-sm-6.col-xs-12
              = f.label t(:par_wrong_solutions)
              #generated_wrong_solutions
              = button_tag t(:btn_add_wrong_solution), type: 'button', class: 'btn btn-primary btn-toolbar', id: 'add_wrong_solution'

          #close-ended_task{class: 'hidden'}
            = f.check_box 'random', id: 'randomize_task'
            = f.label t(:par_random)
            %i{class: 'material-icons text-muted', rel: 'tooltip', title: t(:tt_task_random)} info_outline
            #randomize{class: 'hidden'}
              .row
                .col-lg-6.col-md-6.col-sm-6.col-xs-12
                  = f.label t(:par_number_of_solutions)
                  %i{class: 'material-icons text-muted', rel: 'tooltip', title: t(:tt_task_no_random_solutions)} info_outline
                  = f.number_field 'no_random_solutions', min: '1', step: '1', class: 'form-control'

                .col-lg-6.col-md-6.col-sm-6.col-xs-12
                  = f.label t(:par_min_number_of_correct_solutions)
                  %i{class: 'material-icons text-muted', rel: 'tooltip', title: t(:tt_task_min_no_random_correct_solutions)} info_outline
                  = f.number_field 'min_no_random_correct_solutions', min: '0', step: '1', class: 'form-control'

          .btn-group.btn-group-justified.hidden-xs
            .btn-group
              = f.submit t(:btn_create_new_task), class: 'btn btn-lg btn-primary btn-toolbar'
            .btn-group
              = link_to t(:btn_cancel), '#', class: 'btn btn-lg btn-primary btn-toolbar'
          .btn-group-vertical.visible-xs
            = f.submit t(:btn_create_new_task), class: 'btn btn-lg btn-primary'
            = link_to t(:btn_cancel), '#', class: 'btn btn-lg btn-primary'

我的tasks_controller.rb

  def new
    @task = Task.new
  end

  def create
    @task = Task.new(task_params)
    @task.user_id = helpers.current_user.id
    if @task.save
      flash[:success] = t(:task_created)
      redirect_to new_task_path
    else
      render 'new'
    end
  end

知道可能是什么问题吗? 我不想在出错后清理字段,我想保留它们,只是在下次POST时,发送新值。

0 个答案:

没有答案