【问题标题】:Simple Form simple_fields_for only show last record简单表单 simple_fields_for 只显示最后一条记录
【发布时间】:2015-07-31 14:05:23
【问题描述】:

我有一个嵌套的简单表单给edit 一个用户。用户有一个他/她可以更新的配置文件,并且一条新记录被写入profile 表。

simple_fields_for 显示userall 配置文件记录,原因是关系user 1 to many profile records但是,我只想在表单中显示最新的个人资料记录!我怎样才能做到这一点?

<%= 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 %>

【问题讨论】:

  • 更新时如何将新记录写入配置文件表?
  • 好的,我想我找到了解决方案
  • 我的意思是您有意在更新时创建新的配置文件,或者代码行为不端。如果你是有意创作的,那怎么做?
  • 我正在编辑具有个人资料记录的用户。编辑总是将新的配置文件记录写入数据库,并且应该显示最新的配置文件记录。
  • @Pavan 似乎我的回答低于有效的方法,如果问题措辞不当,抱歉。

标签: ruby-on-rails ruby-on-rails-4 simple-form


【解决方案1】:

有点晚了,但接受的答案并非 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 形式的键的参数,这意味着需要更多代码来解释唯一参数。)

【讨论】:

    【解决方案2】:

    编辑动作:

       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 %>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多