Skip to content

Instantly share code, notes, and snippets.

@jacoyutorius
Last active August 29, 2015 14:07
Show Gist options
  • Select an option

  • Save jacoyutorius/4bf234cb275a40627d64 to your computer and use it in GitHub Desktop.

Select an option

Save jacoyutorius/4bf234cb275a40627d64 to your computer and use it in GitHub Desktop.
Controllerを作らずにViewを表示したい ref: http://qiita.com/jacoyutorius/items/97cf87d36807d00b883b
class AnonymousController < ApplicationController
def index
render "#{params[:directory]}/index"
end
end
mkdir app/views/foo
touch app/views/foo/index.html.erb
mkdir app/views/bar
touch app/views/bar/index.html.erb
$ rake routes
Prefix Verb URI Pattern Controller#Action
bar GET /bar(.:format) anonymous#index {:directory=>"bar"}
foo GET /foo(.:format) anonymous#index {:directory=>"foo"}
NoControllerApp::Application.routes.draw do
exclude_dir = ["layouts"]
anonymous_dir = []
Dir::glob(File.join(Rails.root.to_s, "/app/views/*/")).map{|f| anonymous_dir << f.split("/").last }
(anonymous_dir - exclude_dir).each do |dir|
get "#{dir}" => "anonymous#index", :defaults => {:directory => "#{dir}"}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment