Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如果表单失败,是什么使数据预填充字段?_Ruby On Rails_Ajax_Forms - Fatal编程技术网

Ruby on rails 如果表单失败,是什么使数据预填充字段?

Ruby on rails 如果表单失败,是什么使数据预填充字段?,ruby-on-rails,ajax,forms,Ruby On Rails,Ajax,Forms,我几乎完全是从零开始制作这个表单的,我想这就是为什么它不起作用的原因 但是我在我的控制器中实例化了我的表单 def skip_to_end @card_signup ||= CardSignup.new(params[:card_signup]) respond_to do |wants| wants.html { redirect_to new_card_signup_path } wants.json { render :json => { :html =&g

我几乎完全是从零开始制作这个表单的,我想这就是为什么它不起作用的原因

但是我在我的控制器中实例化了我的表单

def skip_to_end
  @card_signup ||= CardSignup.new(params[:card_signup])

  respond_to do |wants|
    wants.html { redirect_to new_card_signup_path }
    wants.json { render :json => { :html => (render_to_string :partial => '/card_signups/new_form') } }
  end
end
表单如下所示:

 - form_for @card_signup do |f|
      = f.text_field :first_name, :style => "width: 166px;", :value => "first name", :rel => "first name"
      %div{:class => 'error_message'}
      ....
当我尝试将其保存到一半时,它会转到我的
create
操作:

def create
  @card_signup = current_user.build_card_signup(params[:card_signup])
  if @card_signup.valid?
    respond_to do |wants|
      #wants.html { redirect_to disclaimer_card_signup_path, :locals => { :card_signup => @card_signup } }
      wants.json { render :json => { :html => (render_to_string :partial => 'disclaimer') } } 
    end
  else
    respond_to do |wants|
      #wants.html { redirect_to new_card_signup_path }
      wants.json { render :json => {:errors => @card_signup.errors, :html => (render_to_string :partial => '/card_signups/new_form') } }
    end
  end
end
然后它失败,并返回到原始表单,但是清除了所有表单


我怎样才能使字段至少预填充以前的内容?

您可能对此感兴趣:否则,我担心您必须传递json中的所有值并用它填充表单。啊,我非常喜欢将表单保存到用户会话中的想法,但这是从您的github复制的,“
此gem仍处于早期测试阶段。不建议在生产环境中使用它
“呃,这更像是一个免责声明。当然,它需要更多的测试等。只是想向您介绍一下想法。试试看,它已经起作用了。