嵌套表单丢失数据

发布于 2024-11-30 04:50:55 字数 1278 浏览 1 评论 0原文

遗憾的是,在验证用户输入的数据失败后,我的表单丢失了。我该如何更改此设置,以便表单不会丢失数据?

查看_form.html.erb:

<%= form_for(@user, :html => {:name => "newUser""}) do |f| %>
  <div class="field_left">
    <%= f.label :last_name, "Last name<span>*</span>".html_safe %><br />
    <%= f.text_field :last_name %>
  </div>
<% end %>

<div class="field">
  <%= f.fields_for :table1 do |ff| %>
    <%= ff.text_field :name %>
  <% end %>
</div>

控制器users_controller:

def new
  @user = User.new 
  1.times {@user.table1.build}

  respond_to do |format|
    format.html # new.html.erb
    format.xml  { render :xml => @user }
  end
end

def create
  @user = User.new(params[:user])
  @table1= params[:user][:table1][:name]

  respond_to do |format|
    if @user.save
      format.html { redirect_to(@user, :notice => 'User was successfully created.') }
      format.xml  { render :xml => @user, :status => :created, :location => @user }
    else
      format.html { render :action => "new" }
      format.xml  { render :xml => @user.errors, :status => :unprocessable_entity }
    end
  end
end

感谢您的帮助!

Sadly loses my form after unsuccessful validation the data which have the user typed in. How can I change this, so that the form doesn't losing the data?

View _form.html.erb:

<%= form_for(@user, :html => {:name => "newUser""}) do |f| %>
  <div class="field_left">
    <%= f.label :last_name, "Last name<span>*</span>".html_safe %><br />
    <%= f.text_field :last_name %>
  </div>
<% end %>

<div class="field">
  <%= f.fields_for :table1 do |ff| %>
    <%= ff.text_field :name %>
  <% end %>
</div>

Controller users_controller:

def new
  @user = User.new 
  1.times {@user.table1.build}

  respond_to do |format|
    format.html # new.html.erb
    format.xml  { render :xml => @user }
  end
end

def create
  @user = User.new(params[:user])
  @table1= params[:user][:table1][:name]

  respond_to do |format|
    if @user.save
      format.html { redirect_to(@user, :notice => 'User was successfully created.') }
      format.xml  { render :xml => @user, :status => :created, :location => @user }
    else
      format.html { render :action => "new" }
      format.xml  { render :xml => @user.errors, :status => :unprocessable_entity }
    end
  end
end

Thanks for your help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

春夜浅 2024-12-07 04:50:55

解决方案是将实例变量设置为值,如以下代码所示。

<%= f.fields_for :table1 do |ff| %>
  <%= ff.hidden_field :name, :value => @value %>
<% end %>

The solution is to set an instance variable as value like the following code.

<%= f.fields_for :table1 do |ff| %>
  <%= ff.hidden_field :name, :value => @value %>
<% end %>
剩一世无双 2024-12-07 04:50:55

使用: <%= f.fields_for @table1 do |ff| %>

确保使用从表单提交接收到的数据在控制器中设置@table1

Use: <%= f.fields_for @table1 do |ff| %>

Make sure that @table1 is set in the controller with the data that is received from the form submission.

情栀口红 2024-12-07 04:50:55

最好使用

<%= f.fields_for :table1, f.object.table1 do |ff| %>
  <% ff.text_field :name %>
<% end %>

您不必设置值,即 <% ff.text_field :name, value: 'something' %>

Is better to use

<%= f.fields_for :table1, f.object.table1 do |ff| %>
  <% ff.text_field :name %>
<% end %>

You won't have to set values, i.e. <% ff.text_field :name, value: 'something' %>.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文