0

私は4つのモデルを持っています:

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に1つしかないので、存在しないものを表示しようとしているようです。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

app / view / lists / show.html.erb

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

app / view / items / _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ページはレンダリングされますが、存在しないをレンダリングしようとしているように表示されます。

最初の行はデータベースのアイテムであり、2番目の行は空のアイテムエントリのようです

編集

私の中で私はアイテムを作るため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 に答える