新模型插入覆盖其他 [英] New model insert overwriting others

查看:49
本文介绍了新模型插入覆盖其他的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 rails 应用程序,其中有两个模型,分别称为 ColumnRow:

I have a rails application where I have two models called Column and Row:

列:

has_many :rows

accepts_nested_attributes_for :rows, :reject_if => lambda { |b| b[:data].blank? }

行:

belongs_to :column

还有表格:

<%= form_for [@table, @row] do |f| %>

   <% @columns.each do |column| %>
       <%= f.label :data %><br>
       <%= f.text_area :data %>
   <% end %>

   <%= f.submit %>
<% end %>

以及我在控制器中的创建操作:

and my create action in the controller:

@row = Row.new(row_params)

if @row.save
  redirect_to table_rows_path, notice: 'row was successfully created.' 
else
 render action: 'new'
end

和我的新动作:

@columns = Column.where(:table_id => @table.id)
@row = Row.new(id: @table.id)

所以我有两个问题.第一个是如果我说两列,那么新行页面上会有两个文本字段,我在第一个文本字段中输入测试",在第二个文本字段中输入另一个测试".唯一得救的是第二件事.第一个保存另一个测试"而不是测试".

So I have two problems. The first is if I have say two columns, so there will be two textfields on the new row page, and I enter "Test" in the first text field and "Another Test" in the second. The only thing that is getting saved is the second. The first one saves "Another Test" instead of "Test".

此外,如何获取行(属于列)以在每行内保存 column_id?

Also, how can I get the row (which belongs to a column) to save a column_id inside each row?

感谢大家的帮助!

推荐答案

您正在将新 Row 的 id 设置为 @table.id.把它拿出来;您永远不需要创建新的 .id.然后使用 column_id: params[:column_id] 将它们链接起来.

You are setting the new Row's id to the @table.id. Take that out; you never need to create a new .id. Then use column_id: params[:column_id] to link them up.

这篇关于新模型插入覆盖其他的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆