从嵌套资源显示的空项目

时间:2016-02-01 01:34:31

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

我是Rails的新手,正在构建一个todo应用。

出于某种原因,默认情况下,所有列表中都会显示一个空的<%= render @list.items %>实例。 @list返回0,但List.find(params[:id])始终有_item.html.erb的一个“实例”。

我已在列表控制器的show方法中将<%= render @list.items %>分配给<%= link_to "Delete", [item.list, item], method: :delete, data: { confirm: "Are you sure?" } %> ,并在其展示视图中调用lists/*/items

我在_item.html.erb

中有这个链接
list/*/items/*

它将显示删除链接,但链接到空项中的class ListsController < ApplicationController before_action :set_list, only: [:show, :edit, :update, :destroy] def index @lists = policy_scope(List).where(author: current_user) end def show @item = @list.items.build end def new @list = List.new authorize @list end def create @list = List.new(list_params) @list.author = current_user authorize @list if @list.save flash[:notice] = "Your list was created." redirect_to @list else flash[:error] = "Your list was not created." render "new" end end def edit end def update if @list.update(list_params) flash[:notice] = "Your list was updated." redirect_to @list else flash[:error] = "Your list was not updated." render "edit" end end def destroy if @list.destroy flash[:notice] = "Your list was deleted." redirect_to root_path else flash[:error] = "Your list was not deleted." redirect_to @list end end private def list_params params.require(:list).permit(:title, :description, :author_id) end def set_list @list = List.find(params[:id]) authorize @list end end (而不是class ItemsController < ApplicationController before_action :set_list def create @item = @list.items.create(item_params) authorize @item if @item.save flash[:notice] = "Your item was added." redirect_to @list else flash.now[:error] = "Your item was not added." end end def destroy @item = @list.items.find(params[:id]) authorize @item if @item.destroy flash[:notice] = "Your item was deleted." redirect_to @list else flash.now[:error] = "Your item was not deleted." end end private def item_params params.require(:item).permit(:name, :list_id) end def set_list @list = List.find(params[:list_id]) end end ),这会产生路由错误。该链接在实际项目中工作正常。

控制器:

lists_controller.rb:

class List < ActiveRecord::Base
  validates :title, presence: true
  validates :description, presence: true

  belongs_to :author, class_name: "User"

  has_many :items, dependent: :destroy
end

items_controller.rb:

class Item < ActiveRecord::Base
  validates :name, presence: true

  belongs_to :list
end

型号:

list.rb:。

Rails.application.routes.draw do
  root "lists#index"

  resources :lists do
    resources :items
  end

  devise_for :users
end

item.rb的:

class AddListsToItems < ActiveRecord::Migration
  def change
    add_reference :items, :list, index: true, foreign_key: true
  end
end

路线:

select case when count(distinct book.key)=0
  then null
  else count(authorCollection.key is not null)/count(distinct book.key)
  end as avg_after_2000
from book
left join authorCollection on(book.key=authorCollection.key)
where book.year >= 2000

参考迁移:

{{1}}

3 个答案:

答案 0 :(得分:1)

问题是由我将实例变量传递给表单的方式引起的。

ListsController中的这一行正在每次刷新页面时构建一个新项目。

def show
   @item = @list.items.build
end

我删除了该作业,以及传递给表单。

<%= render "items/form", list: @list, item: @item %>变为<%= render "items/form", list: @list %>

simple_form_for([list, item]变为simple_form_for([@list, @list.items.build]

答案 1 :(得分:0)

<%= render 'item' %>

以及 _item.html.erb (已呈现),请使用以下代码:

<%= @list.items %>

答案 2 :(得分:0)

@item = @list.items.build是您的问题。

问题是构建嵌套项目总是会产生一个“空”项,特别是如果它是嵌套的。

虽然可以使用new操作中的代码而不受惩罚,但在show中使用基本上只会创建一个您必须应对的空白对象。

-

如果您想在查看new时创建item @list对象,您可以更好地构建/调用(就像您已经在做的那样):< / p>

<%= form_for [@list, @list.items.new] ...