0

我有四个模型:

class User < ActiveRecord::Base
  has_many :lists, dependent: :destroy
  has_many :wishes, through: :lists
  has_many :items, through: :wishes

class List < ActiveRecord::Base
  belongs_to :user
  has_many :wishes, dependent: :destroy
  has_many :items, through: :wishes

class Wish < ActiveRecord::Base
  belongs_to :list
  belongs_to :item

class Item < ActiveRecord::Base
  has_many :wishes
  has_many :lists, through: :wishes

当我想显示itemsList. 我得到错误:

No route matches {:action=>"show", :controller=>"items", :id=>#<Item id: nil, title: nil, link: nil, created_at: nil, updated_at: nil, image: nil>}

就好像它试图显示一个Item不存在的,因为我Item在数据库中只有一个。

class ListsController < ApplicationController
  def show
    @user = User.find(params[:user_id])
    @list = @user.lists.find(params[:id])
    @item = @list.items.build if current_user?(@user)
    @items = @list.items
  end

应用程序/视图/列表/show.html.erb

  <div class="span8">
    <% if @list.items.any? %>
      <ol class="lists white-box">
        <%= render @items %>
      </ol>
    <% end %>
  </div>

应用程序/视图/项目/_item.html.erb

<li>
    <div class="clearfix">
        <a class="thumb" href="<%= item_path(item) %>">
            <%= image_tag item.image_url(:thumb).to_s %>
        </a>
        <div class="clearfix">
          <span class="content">
            <%= item.title %>, <%= item.link %>
          </span>
          <span class="timestamp">
            Created <%= time_ago_in_words(item.created_at) %> ago.        
            <% if current_user?(item.lists.last.user) %>
            <%= link_to "delete", item, method: :delete,
                                        data: { confirm: "You sure?" },
                                        title: item.title %>
            <% end %>
          </span>

        </div>
    </div>
</li>

额外的

如果我改成app/view/items/_item.html.erb这样:

<li>
    <div class="clearfix">
        <a class="thumb" href="#">
            <%= image_tag item.image_url(:thumb).to_s %>
        </a>
        <div class="clearfix">
          <span class="content">
            <%= item.title %>, <%= item.link %>
          </span>    
        </div>
    </div>
</li>

该页面已呈现,但显示为好像它试图呈现一个Item不存在的:

第一行是数据库中的项目,第二行似乎是一个空的项目条目

编辑

在我的app/view/lists/show.html.erb我有<%= render 'shared/item_form' %>构建一个项目。

_item_form.html.erb 看起来像这样:

<% if signed_in? %>

  <div id="addWish" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="addWishLabel" aria-hidden="true">
    <div class="modal-header">
      <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
      <h3 id="addWishLabel">Add wish</h3>
    </div>
    <div class="modal-body">
      <%= form_for(@item, html: { multipart: true }) do |f| %>
      <div class="field">
        <%= render 'items/fields', f: f %>
        <%= hidden_field_tag :list_id, @list.id %>
      </div>
    </div>
    <div class="modal-footer">
      <button class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
      <%= f.submit "Add", class: "btn btn-primary" %>
      <% end %>
    </div>
  </div>
<% end %>
4

1 回答 1

0

您的控制器正在构建一个空项目 - 请参阅@item = @list.items.build if current_user?(@user)。您的视图无法正确生成指向此未保存项目的链接。

于 2013-02-23T12:25:44.343 回答