rails view without a controller
Asked Answered
T

6

25

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?

Toxemia answered 29/8, 2009 at 21:25 Comment(0)
M
16

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.

Meaghanmeagher answered 29/8, 2009 at 21:28 Comment(2)
Thoughtbot have a useful plugin for this: github.com/thoughtbot/high_voltage/tree/masterFraught
This seems outdated now...? It first said map is undefined. Then I added map as a parameter on top of routes.rb |map| Then Rails gave a more elaborate error pointing me to this URL the-lowdown-on-routes-in-rails-3Exculpate
M
29

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...

Mcmasters answered 10/1, 2013 at 1:21 Comment(0)
M
16

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.

Meaghanmeagher answered 29/8, 2009 at 21:28 Comment(2)
Thoughtbot have a useful plugin for this: github.com/thoughtbot/high_voltage/tree/masterFraught
This seems outdated now...? It first said map is undefined. Then I added map as a parameter on top of routes.rb |map| Then Rails gave a more elaborate error pointing me to this URL the-lowdown-on-routes-in-rails-3Exculpate
M
4

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.

Muirhead answered 29/8, 2009 at 22:34 Comment(0)
C
1

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/

Cardio answered 30/8, 2009 at 16:31 Comment(0)
M
1

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.

Modal answered 28/9, 2012 at 10:48 Comment(0)
M
1

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

Mccarty answered 12/4 at 10:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.