English 中文(简体)
Ruby on Rails: Can I route a request directly to a view?
原标题:

I have an admin section that has a sub-directory of the controllers directory. I.e., the directory app/controllers/admin/ holds a set of files, each containing a controller for handling a separate portion of the admin section.

Now, I want to create a very simple admin homepage that just says something a la "welcome to the admin section", but I want to avoid creating an entire controller for this purpose, or placing the "action" method for this view in some other, arbitrary controller.

So, the question is, is there a way to route directly to a view -- to route directly to an HTML file? (And, I don t want to route to an HTML file from a requested path like /some-file.html; I need to route from a path like /admin/.)

Asking this question here is hardly worth the time it takes to create a stub controller, but I m sure I will have such a need again in the future.

最佳回答

No, you can t. Why?

Design: It s just a violation of the MVC pattern Rails enforces you to use, for your own good. There is always a controller involved. And yes, even for such stub pages, a controller is required. Anyway, a few lines of code won t hurt you, and you ll love it again when you need to perform some access control.

Hope that answered your question :-)

问题回答

Like moritz says, you can t completely bypass the controller, but you do not necessarily have to create an entire controller just for this action. When I need a "dashboard" or "landing" type of page, I just add the action to my ApplicationController:

class ApplicationController < ActionController::Base
  def index
  end
end

Then put your page at app/views/application/index.html.erb

For your case, you can put an application_controller.rb in your app/controllers/admin/ directory, and do the same thing there.

I find that this is the perfect place for pages that fall outside of a resource.





相关问题
rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

SSL slowness in EC2

We ve deployed our rails app to EC2. In our setup, we have two proxies on small instances behind round-robin DNS. These run nginx load balancers for a dynamically growing and shrinking farm of web ...

Auth-code with A-Za-z0-9 to use in an URL parameter

As part of a web application I need an auth-code to pass as a URL parameter. I am currently using (in Rails) : Digest::SHA1.hexdigest((object_id + rand(255)).to_s) Which provides long strings like : ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

activerecord has_many :through find with one sql call

I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent => :destroy has_many :roles, :through => :permissions end class Permission < ActiveRecord::...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签