【问题标题】:Ruby on rails , two views in a controller show the same output but have different html codeRuby on rails,控制器中的两个视图显示相同的输出但具有不同的 html 代码
【发布时间】: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


【解决方案1】:

您的edit 网址应如下所示:

http://localhost:3000/controller_b/:id/edit

【讨论】:

  • :id 是对象 ID
【解决方案2】:

您看到相同内容的原因是因为 show 路径需要具有这种格式的 url:

controller_b GET    /controller_b/:id

所以当你去/controller_b/show/controller_b/edit时,它认为:id分别是showedit


如果你想要一个单一的资源路由,你必须将你的路由定义更改为:

resource :controller_b

这将使您可以访问此路线:

controller_b GET    /controller_b

不需要show,实际上我认为如果您指定editnew 以外的任何内容,它可能会失败。

【讨论】:

  • 谢谢!但是将 :id 添加到 url 的目的是什么?
  • resources 路由就是这样工作的。 show 是显示单个项目视图,因此您需要告诉控制器您正在查看哪个项目。您可能想查看单一资源:guides.rubyonrails.org/routing.html#singular-resources
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-25
  • 1970-01-01
相关资源
最近更新 更多