I noticed that when I create a record in one of my tables, and I have some kind of data that is associated with that table, it adds 1 record already, but it is nil?
我注意到,当我在一个表中创建一条记录时,我有一些与该表相关的数据,它已经添加了一条记录,但它是nil?
For instance:
例如:
lists controller
列出了控制器
def show
@list = List.find(params[:id])
@idea = @list.ideas.build
respond_to do |format|
format.html # show.html.erb
format.json { render json: @list }
end
end
list model
列表模式
has_many :ideas
idea model
概念模型
belongs_to :list
list > show.html.erb
列表> show.html.erb
<%= @list.ideas.each do |idea| %>
<div>
<div class="list_idea_desc"><%= idea.description %></div>
<div><%= link_to 'Show', idea %></div>
<div><%= link_to 'Destroy', idea, method: :delete, data: { confirm: 'Are you sure?' } %> </div>
</div>
<% end %>
I get this record:
我得到这个记录:
[#<Idea id: nil, name: nil, description: nil,
picture: nil, created_at: nil, updated_at: nil, list_id: 1>]
This idea
record automatically gets inserted into my database when I create a new list. Why does this happen?
当我创建一个新列表时,这个想法记录会自动插入到我的数据库中。这为什么会发生?
3 个解决方案
#1
4
Difference between new
and build
is that build
adds the newly created object to the collection:
new和build之间的区别在于build将新创建的对象添加到集合中:
Replace:
替换:
@idea = @list.ideas.build
With:
:
@idea = @list.ideas.new
Or if you want to use build
only, you should
或者如果您想只使用build,您应该这样做
Replace:
替换:
<%= @list.ideas.each do |idea| %>
With:
:
<%= @list.ideas(true).each do |idea| %>
true
discards the cached copy of ideas and goes back to the database.
true会丢弃想法的缓存副本并返回数据库。
#2
1
This is due to @idea = @list.ideas.build
code line, remove it and check it. It may works for your view.
这是由于@idea = @list.ideas。构建代码行,删除它并检查它。它可能适合你的观点。
<% @idea = @list.ideas.build %>
<%= form_for [@list,@idea] do |f| %>
#3
1
Try this:
试试这个:
<%= @list.ideas.each do |idea| %>
<% if idea.present? %>
<div>
<div class="list_idea_desc"><%= idea.description %></div>
<div><%= link_to 'Show', idea %></div>
<div><%= link_to 'Destroy', idea, method: :delete, data: { confirm: 'Are you sure?' } %> </div>
</div>
<% end %>
<% end %>
Use present? condition in record saving time also.
用礼物吗?记录保存时间的条件。
#1
4
Difference between new
and build
is that build
adds the newly created object to the collection:
new和build之间的区别在于build将新创建的对象添加到集合中:
Replace:
替换:
@idea = @list.ideas.build
With:
:
@idea = @list.ideas.new
Or if you want to use build
only, you should
或者如果您想只使用build,您应该这样做
Replace:
替换:
<%= @list.ideas.each do |idea| %>
With:
:
<%= @list.ideas(true).each do |idea| %>
true
discards the cached copy of ideas and goes back to the database.
true会丢弃想法的缓存副本并返回数据库。
#2
1
This is due to @idea = @list.ideas.build
code line, remove it and check it. It may works for your view.
这是由于@idea = @list.ideas。构建代码行,删除它并检查它。它可能适合你的观点。
<% @idea = @list.ideas.build %>
<%= form_for [@list,@idea] do |f| %>
#3
1
Try this:
试试这个:
<%= @list.ideas.each do |idea| %>
<% if idea.present? %>
<div>
<div class="list_idea_desc"><%= idea.description %></div>
<div><%= link_to 'Show', idea %></div>
<div><%= link_to 'Destroy', idea, method: :delete, data: { confirm: 'Are you sure?' } %> </div>
</div>
<% end %>
<% end %>
Use present? condition in record saving time also.
用礼物吗?记录保存时间的条件。