开发者

ruby on rails sub directory inside in the 'Views' main directory

开发者 https://www.devze.com 2023-02-04 08:24 出处:网络
am a newbie in ruby on rails and am stuck with a simple problem of routing. I have my controller \'sub\' and the \'Views\' folder containing the add,edit,new erb files.

am a newbie in ruby on rails and am stuck with a simple problem of routing.

I have my controller 'sub' and the 'Views' folder containing the add,edit,new erb files.

In my routes file, i have 'map.resources :subs'.

Until now, everything is fine.开发者_如何学C

Problem: I moved the add,edit,new erb files into a subfolder called 'admin' inside the 'Views' main directory. I have no idea how to call those erb files from that 'admin' subdir.

By default, it is looking for /app/views/subs/index.html.erb, and i want it to look in /app/views/subs/admin/index.html.erb

Please can anyone tell me how to do this.

Many many thanks


I suggest a different approach because it seems what you want to do is admin routing. In your routes.rb write

  namespace :admin do     
    resources :subs
  end 

then put your views in the subdirectory views/admin/subs

also, put your controller in the subdirectory controllers/admin and namespace them with "Admin" too, e.g.

class Admin::StubsController < Admin::ApplicationController
  your_code_goes_here
end

of course, then you need an application_controller.rb in the controllers/admin dir as well. But you cold also derive from ApplicationController then that is not necessary.

your controller can be called through the url /admin/subs

does that help?


You could explicitly render your templates within your controller actions, like this:

render :template => "subs/admin/index"


I'm a beginner in RoR.

What I wanted was to group all views (such as a mobile friendly version) in 1 folder but not end up with an extra namespace OR create new method in controllers. localhost:3000/posts calls:

class Post < ActiveRecord

and not

class Admin::Post < ActiveRecord

BUT load the views in views/android/posts/index.html.erb

Because this was my Google first hit, the link below is to an alternative answer that took sometime for me to find.

Rails: Elegant way to structure models into subfolders without creating submodules

0

精彩评论

暂无评论...
验证码 换一张
取 消