[英]Rails REST doesn't show/load the right page - Ruby on Rails
基本上我為主題創建了一個控制器、模型和視圖。 基本上我的控制器中有 6 個動作,並在我的路由文件中設置了一個 REST 來路由正確的文件。
當我輸入時, http://localhost:3000/subjects/index它向我顯示了 show.html.erb 而不是 index.html.erb 的視圖
這是我的主題控制器的樣子:
class SubjectsController < ApplicationController
def index
@subjects = Subject.sorted
end
這是我的 index.html.erb 文件的內容。
<% @page_title = "All Subjects" %>
<div class="subjects index">
<h2>Subjects</h2>
<%= link_to("Add New Subject", new_subject_path, :class => "action_new") %>
<table class="listing" summary="Subject list" border="1">
<tr class="header">
<th>#</th>
<th>Subject</th>
<th>Visible</th>
<th>Pages</th>
<th>Actions</th>
</tr>
<% @subjects.each do |subject| %>
<tr>
<td><%= subject.position %> </td>
<td><%= subject.name %> </td>
<td class="center"><%= status_tag(subject.visible) %></td>
<td class="center"><%= subject.pages.size %> </td>
<td class="actions">
<%= link_to("View Pages", pages_path(:subject_id => subject.id), :class => 'action show') %>
<%= link_to("Show", subject_path(subject), :class => 'action show') %>
<%= link_to("Edit", edit_subject_path(subject), :class => 'action edit') %>
<%= link_to("Delete", delete_subject_path(subject), :class => 'action delete') %>
<td>
</tr>
<% end %>
<table>
</div>
這也是我在路線上設置的內容:
resources :subjects do
member do
get :delete
end
end
知道我錯過了什么嗎?
答案很簡單:為了訪問索引頁面,您需要點擊以下 URL:
http://localhost:3000/subjects
顯然,它將與GET
為什么你得到了錯誤的原因是:因為格式的任何subjects/:id
會帶你到show
中的動作SubjectsController
,所以Rails的解釋subjects/index
與您試圖訪問該頁面subjects/:id
與index
為主題的id
。 Rails 沒有錯,因為在 RESTful Web 服務中,您只能使用資源的復數名稱來訪問索引頁面,例如http://localhost:3000/subjects
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.