2017-01-18 127 views
2

基本上,我爲主題創建了一個控制器,模型和視圖。基本上,我的控制器中有6個動作,並在我的路由文件中設置一個REST來路由正確的文件。Rails REST不會顯示/加載正確的頁面 - Ruby on Rails

當我進入,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 

任何想法,我缺少的是什麼?

回答

2

答案很簡單:爲了您訪問索引頁,您需要點擊以下網址:

http://localhost:3000/subjects 

顯然,它會隨着GET

你之所以得到了錯誤是:因爲格式爲subjects/:id的任何內容將帶您進入SubjectsController內的show操作,因此Rails在您試圖訪問subjects/:id頁面時使用index作爲id來解釋subjects/index。 Rails沒什麼問題,因爲在REST風格的Web服務中,你只能使用資源的複數名稱來訪問索引頁,就像你的情況http://localhost:3000/subjects

+0

嗨,感謝您的回覆。我在訪問它時得到了這個。無意之中http://prnt.sc/dx796n –

+0

。它現在有效。 –

相關問題