Rails嵌套了简单博客的路由

时间:2016-04-05 07:09:30

标签: ruby-on-rails ruby-on-rails-4 routes

我需要为我的博客以及博客下的每个帖子制作页面。它看起来应该是example.com/blog/my_first_post。帖子是静态HTML文件,我不使用任何数据库。

这是我的路线:

  get 'blog' => 'static_pages#blog' do
    get '/my_first_post' => 'blog#my_first_post' 
  end

这是我的StaticPages控制器:

  ...

  def blog
    def my_first_post
    end
  end

  ...

博客页面工作正常,但帖子无效。

4 个答案:

答案 0 :(得分:0)

要获得此example.com/blog/my_first_post,您的路线应如下所示 -

get 'blog' => 'static_pages#blog'
get 'blog/my_first_post' => 'static_pages#my_first_post'

您的控制器应该看起来像这样 -

class StaticPagesController < ApplicationController
  def my_first_post
  end

  def blog
  end
end

答案 1 :(得分:0)

检查一下:但有一件事

如果它只是静态页面,那么最好使用如下所示。

get 'blog/my_first_post' => 'static_pages#my_first_post'

你不能把这个方法称为视图,如果它在其中。就这样。

  def blog
  end

  def my_first_post
  end

答案 2 :(得分:0)

使用以下资源:博客(如果有)

get 'blog/my_first_post'

答案 3 :(得分:0)

试一试:

get 'blog/my_first_post', to: Proc.new { |env|
    [
      200,
      {"Content-Type" => "text/html"},
      [File.read("public/my_first_post.html")] // where you static files are
    ]
  }

在您的情况下,控制器是多余的

相关问题