Can rails handle creating a view without a controller? For example, let say I have a page that just links to other pages, would I need to create a dummy controller for that, or could I just do something in my routes file?
No. All requests has to go through a controller.
I like to have a PagesController
, with map.page ":action", :controller => "pages"
. That way, I can create app/views/pages/foo.erb
and have it available on /foo
without any extra code.
I like August's answer but I have a slightly different method.
Let's say you want to add
/any/path/somefile.html.erb
but not add a controller...
You can just add folder to views called "application", create your file in that directory..
Then in your routes file just add
match '/any/path/somefile' => 'application#somefile'
Your erb still evaluates, you get your layout, and you can create any path you want... (all this does is remove the need for the pages controller)
Hope it helps...
No. All requests has to go through a controller.
I like to have a PagesController
, with map.page ":action", :controller => "pages"
. That way, I can create app/views/pages/foo.erb
and have it available on /foo
without any extra code.
Another option would be adding a static html file in your /public directory if you truly don't need it as part of your application.
If you are a brave soul. You can try edge rails 3. Katz demonstrated this possibility on his blog. Here is the link:
http://yehudakatz.com/2009/07/19/rails-3-the-great-decoupling/
No. All requests have to go through a controller.
If you have a page like index.html.erb
and contact.html.erb
in your view folder. You need to create a dummy controller called contact
. Then you can link to the contact.html.erb
from the index.html.erb
. And give the link as <%= link_to 'contact', :controller => "ads", :action => "contact" %>
here "ads"->controller name.
Rails handles view without controller action
I havent tested if it would work without a controller at all, but without controller action it works.
12 years later, Rails, 7.
Routes
resources :invoices
invoices_controller.rb
before_action :authorize_action
# def index
#
# end
private
def authorize_action
....
end
invoices/index.haml
=> works!!
BUT the authorize_action
method fires if index is commented out or not.
I opened a issue
© 2022 - 2024 — McMap. All rights reserved.