안녕하세요, 저는 현재 루비를 시작하는 13 장을하고 있습니다. http : // localhost : 3000 / entries / view_all에 액세스 할 때이 오류가 발생 합니다 . 값이있는 데이터베이스 항목을 보려고했지만 여전히 전혀 작동하지 않습니다.
이것은 예상 결과입니다.
이것은 내가 지금 직면 한 오류입니다.
Couldn't find all Entries with 'id': (all, {:include=>:Content, :order=>"created_at DESC"}) (found 0 results, but was looking for 2)
암호:
def view_all
#@entries = Entry.all(:order => 'created_at DESC')
@entries = Entry.find(:all, :include => :Content, :order => "created_at DESC")
end
이것은 내 항목 컨트롤러입니다.
def view_all
@entries = Entry.find(:all, :include => :Content, :order => "created_at DESC")
end
이것은 내 route.rb입니다.
Rails.application.routes.draw do
resources :users
resources :entries do
get :view_all, on: :collection
end
end
이것은 내 view_all.html.erb입니다.
<% @entries.each do |entry| %>
<h1><%= entry.Title %></h1>
<p><%= entry.Content %></p>
<p><em>Posted at <%= entry.created_at %></em></p>
<% end %>
<%= link_to 'Add New Entry', new_entry_path %>
4보다 큰 rails 버전을 사용하는 경우 find
메소드는 모든 행을 가져 오기 위해 인수 로만 id
또는 여러 ids
개를 사용합니다.
def view_all
@entries = Entry.all.includes(:Content).order(created_at: :desc)
end
또는 이전 버전의 경우 질문의 의견에 제안 된대로 수행 할 수 있습니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다