渲染@items,没有路由匹配错误

时间:2013-02-23 12:24:08

标签: ruby-on-rails

我有四种模式:

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

当我想显示与items相关联的List时,会出现问题。我收到错误:

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

First line is the Item from the database, the second line appears to be an empty Item entry

修改

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

1 个答案:

答案 0 :(得分:0)

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