Can I Render A Layout Directly From routes.rb, Without A Controller?

荒凉一梦 提交于 2019-12-01 01:20:04

问题


I would like to set up a pair of style guides for the admin and public sections of a website.

Each will need its own layout which will contain a mixture of static html and calls to erb partials (so a static page won't cut it). I have no need of a controller(s) to serve these pages and I don't want what is effectively development-only content cluttering up the rest of the code. This got me wondering whether there is a way to render a layout directly.

Disclaimer: I appreciate this is not something I should do often/ever and I know there are a wealth of arguments for why this is a bad idea. I am interested in whether this is possible.

Is there a way for me to render a layout directly from routes.rb without going through a controller?


回答1:


For some weird reason I wanted to render a blank JS file for a while, and writing a controller felt like too much for this kind of hack. Thanks to @genkilabs 's answer, I used this 3 liner:

get 'analytics/some_file.js', to: -> (env) do
  [200, { 'Content-Type' => 'application/javascript' }, ['']]
end



回答2:


I wanted to do something really stupid once, so if you do too, try this working example.

match :movedpage, :to => proc { |env|
    if Rails.env.production?
        @remote_path = 'http://productionhost.com'
    elsif Rails.env.staging?
        @remote_path = 'http://staginghost.com'
    else
        @remote_path = 'http://localhost:3000'
    end
    [
        200,
        {"Content-Type" => "text/html"},
        [File.read("public/moved_page.html").gsub('@remote_path', @remote_path)]
    ]
}, :via => :all

Where moved_page.html was a static page asking people up update their bookmarks and @remote_path just typed in a link like <a href="@remote_path">@remote_path</a>. Note that <%= %> won't work because you don't have view helpers in there.

So, theres enough rope to get yourself in trouble ^_^




回答3:


Actualy the answer is NO, you can't do it without a controller. But see some trivial workaround...

It's not very fair but should work:

Assuming you have FooController with any logic you already have implemented. Now you want to render anypage.html.erb without creating any special controller. Here is how:


  1. Configure a route to your static page:

    get '/your/static/page', to: 'foo#anypage'

  2. Implement the view app/views/foo/anypage.html.erb.


The problem is that it is impossible to change a path to your view. The path depends on the controller that you specify in a route (foo in the example). Also note that it will be rendered with a specified for FooController layout.

It should work by convention and you can read about it here.


UPDATE

Also I found very simmilar solution here. Using ApplicationController seems more reasonable for such pages. (Note that you needn't to create an action for it)



来源:https://stackoverflow.com/questions/24982111/can-i-render-a-layout-directly-from-routes-rb-without-a-controller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!