【问题标题】:Extra record showing in ActiveRecord query resultActiveRecord 查询结果中显示的额外记录
【发布时间】:2015-05-15 02:49:53
【问题描述】:

那么问题来了:

我有一个想要显示“项目”列表的页面。我还想在此页面上有一个用于添加其他“项目”的表单。

控制器中的“新”操作如下所示:

def new
  @collection = current_user.collections.find_by_id(params[:collection_id])
  @item_list = @collection.items
		
  @item = @collection.items.new
end

视图中的代码如下:

<div class="row-fluid">
  <div id="main_area" class="col-sm-offset-3 col-sm-6 col-md-offset-4 col-md-4">
    <div class="center">
      <h3><%= @collection.title %></h3>
      <% if @item_list.any? %>
        <ul>
          <% @item_list.each do |il| %>
            <li>
              <%=i l.name %>
                <% end %>
        </ul>
        <% end %>

          <div id="add_item">
            <%=f orm_for [@collection, @item] do |f| %>
              <div class="form-group <%= 'has-error has-feedback' if @item.errors[:name].present? %>">
                <label class="sr-only" for="item_name">Item Name</label>
                <%=f .text_field :name, :autofocus=>true, :placeholder => "Item Name", :class => "form-control", :'aria-describedBy' => "itemNameBlock" %>
                  <% if @collection.errors[:title].present? %>
                    <span id="itemNameBlock" class="error">Item <%= @item.errors[:name].first %></span>
                    <% end %>
              </div>
              <div id="signin_button_row">
                <%=f .submit "Save", :class=>"form-control green_button" %>
                  <span id="forgot_my_password" class="right-justify">
						<%= link_to "cancel", collection_items_path(@collection), :class => "new_colors terms" %>
					</span>
              </div>
              <% end %>
          </div>
    </div>
  </div>
</div>

我的问题是视图中的循环似乎总是有一个额外的“项目”,这似乎与我在控制器中使用“新”这一事实有关。

我该如何解决这个问题?

【问题讨论】:

    标签: ruby-on-rails activerecord


    【解决方案1】:

    这将创建相同的项目,但不会将其添加到集合中

    def new
      @collection = current_user.collections.find_by_id(params[:collection_id])
      @item_list = @collection.items
    
      @item = Item.new collection_id: @collection.id
    end
    

    【讨论】:

    • 谢谢。我喜欢这个解决方案,因为它将@item 与集合分开。
    【解决方案2】:

    这里的问题是,在构建模板 Item 对象时,您也将其放入集合中,这不是您想要的。

    在我看来,有两种可能的解决方法:

    1. 输出列表时,不通过检查item是否已经持久化来显示新item:

      <ul>
        <% @item_list.reject(&:new?).each do |il| %>
          <li>
            <%= il.name %>
          </li>
        <% end %>
      </ul>
      
    2. 实际上不要将新模板项添加到集合中,从概念的角度来看,这对我来说更有意义,因为在呈现查看,例如:

      class ItemsController < ApplicationController
      
        before_filter do
          @collection = collection.find(params[:collection_id])
          @items = @collection.items
        end
      
        def index
          @item = Item.new
        end
      
        def create
          @item = @items.new(item_params)
          if @item.save
            redirect_to  collection_items_path
          else
            render action: :index
          end
        end
      
        private
      
        def item_params
          params.require(:item).permit!
        end
      end
      

    希望这对你有意义并且对你有用!

    【讨论】:

    • 谢谢......我想我错过了执行“查找”检索所有对象,无论是否持久。从理论上讲,这两种解决方案都可以工作,如果我必须在两者中选择一个,我可能会选择第一个。
    猜你喜欢
    • 1970-01-01
    • 2017-09-14
    • 1970-01-01
    • 2021-08-20
    • 2012-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多