gpt4 book ai didi

ruby-on-rails - Rails REST 不显示/加载正确的页面 - Ruby on Rails

转载 作者:数据小太阳 更新时间:2023-10-29 08:58:48 24 4
gpt4 key购买 nike

基本上我为主题创建了一个 Controller 、模型和 View 。基本上我的 Controller 中有 6 个 Action ,并在我的路由文件中设置了一个 REST 来路由正确的文件。

当我进入时,http://localhost:3000/subjects/index它向我展示了 show.html.erb 而不是 index.html.erb 的 View

这是我的主题 Controller 的样子:

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/:idindex作为id的主题。 Rails 没什么问题,因为在 RESTful Web 服务中,您只能使用资源的复数名称来访问索引页面,就像您的情况一样 http://localhost:3000/subjects

关于ruby-on-rails - Rails REST 不显示/加载正确的页面 - Ruby on Rails,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41720923/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com