简单表格 simple_fields_for 只显示最后一条记录
Simple Form simple_fields_for only show last record
我有一个给 edit
用户的嵌套简单表单。用户有个人资料 he/she 可以更新,并且新记录写入 profile
table。
由于 user 1 to many profile records
关系,simple_fields_for
显示 user
的 all
个人资料记录。 但是,我只想在表格中显示最新的个人资料记录!我怎样才能做到这一点?
<%= simple_form_for(@user) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= f.input :lang %>
<%= f.input :firstname %>
<%= f.input :lastname %>
<%= f.input :email %>
<%= f.input :born %>
<%= f.input :gender %>
<%= f.simple_fields_for :profile do |p| %> # the magic needed here
<%= p.input :postal_code %>
<%= p.input :core %>
<%= p.input :daytime %>
<%= p.input :style %>
<% end %>
<% if @is_new %>
<%= f.simple_fields_for :status do |s| %>
<%= s.input :entered, as: :hidden, input_html: { value: Time.current } %>
<% end %>
<% end %>
</div>
<div class="form-actions">
<%= f.button :submit %>
</div>
<% end %>
编辑动作:
class user > ApplicationController
...
def edit
@profile = @user.profile.order("saved DESC").first
end
...
end
工作形式:
<%= simple_form_for(@user) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= f.input :lang %>
<%= f.input :firstname %>
<%= f.input :lastname %>
<%= f.input :email %>
<%= f.input :born %>
<%= f.input :gender %>
<%= f.simple_fields_for :profile, @profile do |p| %> # the magic needed here
<%= p.input :postal_code %>
<%= p.input :core %>
<%= p.input :daytime %>
<%= p.input :style %>
<% end %>
<% if @is_new %>
<%= f.simple_fields_for :status do |s| %>
<%= s.input :entered, as: :hidden, input_html: { value: Time.current } %>
<% end %>
<% end %>
</div>
<div class="form-actions">
<%= f.button :submit %>
</div>
<% end %>
来晚了,但接受的答案并非 100% 准确。 (只是发表评论,但没有代表。)我会这样做:
假设你有
class User < ActiveRecord::Base
has_many :profiles
...
def latest_profile
profiles.order(...) # query to get whatever record it is you want
end
end
你的simple_fields_for可以
<%= f.simple_fields_for :profiles, f.object.latest_profile do |p| %>
注意 :profiles 的复数形式,我们不需要用额外的实例变量来混淆控制器,因为您可以访问表单中的对象。 (我相信使用单数会奏效,但您不会获得带有 :profiles_attributes 形式的键的参数,这意味着需要更多代码来说明唯一参数。)
我有一个给 edit
用户的嵌套简单表单。用户有个人资料 he/she 可以更新,并且新记录写入 profile
table。
user 1 to many profile records
关系,simple_fields_for
显示 user
的 all
个人资料记录。 但是,我只想在表格中显示最新的个人资料记录!我怎样才能做到这一点?
<%= simple_form_for(@user) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= f.input :lang %>
<%= f.input :firstname %>
<%= f.input :lastname %>
<%= f.input :email %>
<%= f.input :born %>
<%= f.input :gender %>
<%= f.simple_fields_for :profile do |p| %> # the magic needed here
<%= p.input :postal_code %>
<%= p.input :core %>
<%= p.input :daytime %>
<%= p.input :style %>
<% end %>
<% if @is_new %>
<%= f.simple_fields_for :status do |s| %>
<%= s.input :entered, as: :hidden, input_html: { value: Time.current } %>
<% end %>
<% end %>
</div>
<div class="form-actions">
<%= f.button :submit %>
</div>
<% end %>
编辑动作:
class user > ApplicationController
...
def edit
@profile = @user.profile.order("saved DESC").first
end
...
end
工作形式:
<%= simple_form_for(@user) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= f.input :lang %>
<%= f.input :firstname %>
<%= f.input :lastname %>
<%= f.input :email %>
<%= f.input :born %>
<%= f.input :gender %>
<%= f.simple_fields_for :profile, @profile do |p| %> # the magic needed here
<%= p.input :postal_code %>
<%= p.input :core %>
<%= p.input :daytime %>
<%= p.input :style %>
<% end %>
<% if @is_new %>
<%= f.simple_fields_for :status do |s| %>
<%= s.input :entered, as: :hidden, input_html: { value: Time.current } %>
<% end %>
<% end %>
</div>
<div class="form-actions">
<%= f.button :submit %>
</div>
<% end %>
来晚了,但接受的答案并非 100% 准确。 (只是发表评论,但没有代表。)我会这样做:
假设你有
class User < ActiveRecord::Base
has_many :profiles
...
def latest_profile
profiles.order(...) # query to get whatever record it is you want
end
end
你的simple_fields_for可以
<%= f.simple_fields_for :profiles, f.object.latest_profile do |p| %>
注意 :profiles 的复数形式,我们不需要用额外的实例变量来混淆控制器,因为您可以访问表单中的对象。 (我相信使用单数会奏效,但您不会获得带有 :profiles_attributes 形式的键的参数,这意味着需要更多代码来说明唯一参数。)