【问题标题】:Routes for rails model controllerrails模型控制器的路由
【发布时间】:2018-07-23 10:34:15
【问题描述】:

我已经创建了一个站点,并希望在上面有一个博客部分。但是,我不想将博客放在网站的索引页面上。

我创建了一个模型并生成了一个控制器,并按照https://scotch.io/tutorials/build-a-blog-with-ruby-on-rails-part-1上的说明进行操作

我唯一没有遵循的是这段代码: #config/routes.rb

Rails.application.routes.draw do
  root to: "posts#index"
 resources :posts
end

我想通过 localhost:3000/posts/ 进入帖子页面

我的根源是:

Rails.application.routes.draw do
mount Ckeditor::Engine => '/ckeditor'
get 'pages/index' => 'high_voltage/pages#show', id: 'index'
root :to => 'high_voltage/pages#show', id: 'index'
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end


resources :posts do
  resources :index
  resources :show
  resources :edit
  resources :new
  resources :form
end

谢谢!

【问题讨论】:

    标签: ruby-on-rails


    【解决方案1】:

    你可以用这个

    match 'posts' => 'posts#index'
    

    【讨论】:

    • 我使用了 get 函数,它可以显示该页面。我也在尝试获取显示、编辑和新建的链接。我使用相同的样式吗?
    【解决方案2】:

    您的以下路线是错误的:

    resources :posts do
      resources :index
      resources :show
      resources :edit
      resources :new
      resources :form
    end
    

    请参阅以下步骤。完成以下步骤后,您应该运行rails routes 命令以查看所有路由。

    第一个选项:

    resources :posts, only: [:index]
    

    对于其他操作,如显示,编辑。

    resources :posts, only: [:index, :show, :edit]
    

    对于所有操作:

    resources :posts
    

    第二个选项:

    对于单一资源,您可以像下面这样使用:

    get 'posts', to: 'posts#index'
    

    有关 Rails 路线的更多信息:

    http://guides.rubyonrails.org/routing.html

    【讨论】:

      【解决方案3】:

      您的路线定义有误。应该是的。

      Rails.application.routes.draw do
        mount Ckeditor::Engine => '/ckeditor'
        get 'pages/index' => 'high_voltage/pages#show', id: 'index'
        root :to => 'high_voltage/pages#show', id: 'index'
        # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
        resources :posts
      end
      

      【讨论】:

        【解决方案4】:

        您提供的代码存在一些问题。首先,最后的“结束”语句应该在你的资源块之后:posts。其次,resources 块应该被简化为只是

        resources :posts
        

        如提供的示例所示。您当前正在使用“嵌套资源”,这不是您想要的。下面是嵌套资源的描述,让您了解它们的使用方式:

        http://guides.rubyonrails.org/routing.html#nested-resources

        这两个变化应该让您朝着正确的方向前进。希望这可以帮助。

        【讨论】:

          猜你喜欢
          • 2012-10-01
          • 1970-01-01
          • 1970-01-01
          • 2011-12-11
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-01-16
          相关资源
          最近更新 更多