【发布时间】:2015-07-06 17:01:36
【问题描述】:
我创建了一个简单的 ruby on rails 应用程序。它有一个控制器名称 ControllerB,它有 4 个视图,即 index、show、edit 和 new。
views文件夹中的show.html.erb包含html代码:
<p>Show post</p>
edit.html.erb 包含:
<p>Edit post</p>
这是我的路线文件:
Rails.application.routes.draw do
get 'controller_a/myindex'
resources :post_controller
resources :controller_b
root 'controller_a#myindex'
end
问题是,当我尝试使用 url http://localhost:3000/controller_b/edit 打开编辑视图时 它显示了显示视图的内容,即显示帖子。
但是当我从 routes.rb 中删除行资源 :controller_b 并使用 get 命令导入每个视图时,它工作正常。这里出了什么问题?我还重新启动了 rails webrick 服务器以确保已加载新配置。
这是我的 controller_b_controller.rb 文件:
class ControllerBController < ApplicationController
def index
end
def edit
end
def new
end
def show
end
end
根据 Prashanth 的要求,这是我的 rake 路由输出。
Prefix Verb URI Pattern
Controller#Action
controller_b_index GET /controller_b/index(.:format)
controller_b#index
controller_a_myindex GET /controller_a/myindex(.:format)
controller_a#myindex
post_controller_index GET /post_controller(.:format)
post_controller#index
POST /post_controller(.:format)
post_controller#create
new_post_controller GET /post_controller/new(.:format)
post_controller#new
edit_post_controller GET /post_controller/:id/edit(.:format)
post_controller#edit
post_controller GET /post_controller/:id(.:format) post_controller#show
PATCH /post_controller/:id(.:format) post_controller#update
PUT /post_controller/:id(.:format) post_controller#update
DELETE /post_controller/:id(.:format) post_controller#destroy
GET /controller_b(.:format) controller_b#index
POST /controller_b(.:format) controller_b#create
new_controller_b GET /controller_b/new(.:format) controller_b#new
edit_controller_b GET /controller_b/:id/edit(.:format) controller_b#edit
controller_b GET /controller_b/:id(.:format) controller_b#show
PATCH /controller_b/:id(.:format) controller_b#update
PUT /controller_b/:id(.:format) controller_b#update
DELETE /controller_b/:id(.:format) controller_b#destroy
root GET / controller_a#myindex
【问题讨论】:
-
这是您的实际代码,还是您还有一些您认为不相关的内容?这通常发生在 Rails 将
edit视为:id时,但您发布的代码不应导致这种情况。 -
@BroiSatse 你在说 routes.rb 吗?那么是的,就是这样。而且,是的,edit.html.erb 和 show.html.erb 都只有这么多代码,一个段落标签。
-
那么控制器中有什么有趣的东西吗?
-
显示
rake routes输出@AdityaNaidu -
@Prashant4020 我编辑了我的问题并添加了 rake 路由输出。
标签: ruby-on-rails ruby