0

我只是试图通过事件的索引页面访问显示页面,但我不断收到这个 mongoid 错误-

Mongoid::Errors::DocumentNotFound at /events/%23%3CMongoid::Criteria:0x007f8ac1da9578%3E

Summary: When calling Event.find with an id or array of ids, each parameter must match a document in the database or this error will be raised. The search was for the id(s): # ... (1 total) and the following ids were not found: #. Resolution: Search for an id that is in the database or set the Mongoid.raise_not_found_error configuration option to false, which will cause a nil to be returned instead of raising this error when searching for a single id, or only the matched documents when searching for multiples.

控制器

  def index
    @event = Event.all
  end

  def show
    @event = Event.find(params[:id])
  end

路线

  resources :events do
    resources :leads, only: [:new, :create]
    resources :registrations, only: [:new, :create]
  end

索引视图

<ul>
  <% @event.each do |e|  %>
  <li><%= link_to e.title, event_path(@event) %></li>
  <% end %>
</ul>
4

1 回答 1

1

尝试这个

<%= link_to e.title, event_path(e) %>

您必须link_to为每个events可通过e您调用的变量访问的方法调用该方法@event.each do |e|

另外,我建议重命名@eventby@events因为它拥有多个Event. 所以在你的控制器中:

def index
  @events = Event.all
end

在您看来:

<ul>
  <% @events.each do |e|  %>
    <li><%= link_to e.title, event_path(e) %></li>
  <% end %>
</ul>
于 2013-07-10T11:42:49.417 回答