Here is a app contorller directory from Rails project
doing a self study for rails, but from what I understand if I create a directory in the app folder then I have to do the complete the routes files with a match that route like:
match "/editor/usynkdataeditor/saveusynkeditor",
Question to the community is there a better way that I can define different directory structure for a specific workflow or is it safe to define all the controllers in parent controllers directory.
If you create additional directory in controllers directory, you are effectively namespacing your controllers.
So this controller would be:
class Editor::UsynkdataeditorController < ApplicationController
def saveusynkeditor
end
end
As far as routes are defined, you can do something like:
MyApplication::Application.routes.draw do
namespace :editor do
get "usynkdataeditor/saveusynkeditor"
end
end
Whish will give you route:
$ rake routes
editor_usynkdataeditor_saveusynkeditor GET /editor/usynkdataeditor/saveusynkeditor(.:format) editor/usynkdataeditor#saveusynkeditor
Or, preferably just use restful routes instead of saveusynkeditor like this:
MyApplication::Application.routes.draw do
namespace :editor do
resources :usynkdataeditor do
collection do
get :saveusynkeditor
end
end
end
end
when you will get:
$ rake routes
saveusynkeditor_editor_usynkdataeditor_index GET /editor/usynkdataeditor/saveusynkeditor(.:format) editor/usynkdataeditor#saveusynkeditor
editor_usynkdataeditor_index GET /editor/usynkdataeditor(.:format) editor/usynkdataeditor#index
POST /editor/usynkdataeditor(.:format) editor/usynkdataeditor#create
new_editor_usynkdataeditor GET /editor/usynkdataeditor/new(.:format) editor/usynkdataeditor#new
edit_editor_usynkdataeditor GET /editor/usynkdataeditor/:id/edit(.:format) editor/usynkdataeditor#edit
editor_usynkdataeditor GET /editor/usynkdataeditor/:id(.:format) editor/usynkdataeditor#show
PUT /editor/usynkdataeditor/:id(.:format) editor/usynkdataeditor#update
DELETE /editor/usynkdataeditor/:id(.:format) editor/usynkdataeditor#destroy
There is a really good explanation http://guides.rubyonrails.org/routing.html#controller-namespaces-and-routing of what you are trying to achieve in rails guides.
Finally, to answer your question:
Better way? Well it's up to your preferences. How do you like your code organized? You can use namespacing but you don't have to. However,
at the same there is nothing wrong with having all controllers in parent controller directory.
This falls under Namespacing and it's generally considered the best approach to do what you're trying to do. Check it out.
Related
I'm creating an engine that needs to insert some routes into the application's router. For this particular gem, I'd rather not application's routes.rb if possible. Is there a way to insert routes at a particular location in the router via code? I'm looking for an API that does something like:
Rails.application.routes.insert("resources :foos", :before => "some string in routes.rb")
If I create a config/routes.rb inside the engine and define some routes, this kind of works. Rails is smart enough to mix the engine's routes into the application's routes, but it tacks them on at the end of the route list. I need them to appear at the beginning so the engine's routes take priority.
I'm aware that I can namespace the routes by mounting the engine in the application's routes.rb, but this creates a routing structure that I don't really want. I want the engine's routes to look they are a part of the application by defining some routes in the actual application.
I have a workaround which is to add the following to the application's routes.rb.
Rails.application.routes.draw do
MyEngine.setup_routes(self)
#...other routes below
end
MyEngine.setup_routes looks like
def self.setup_routes(map)
map.get 'a_path', :to => 'a_controller#a_path'
end
This at least allows me to control the point where the routes get defined in the application's route list, but the user still has to manually update his routes.rb (or I have to build an installer that does it). It seems like there should be a way to tell rails to tack some routes onto the start of the route list...
First, why do we need to namespace controllers?
The example on rails guides shows
namespace :admin do
resources :post, :comments
end
In this case, we have paths such as GET /admin/posts. Is it identical to GET /posts? Or is GET /posts kept as original while another GET /admin/posts is added as extra?
Does rails create any other stuff for namespace?
You dont have to use namespaces if you dont want to, but it's there to make your life easier, specially in big applications with a lot of controllers. And no, routes arent duplicated if you namespace them, unless you specify the route again in your route file without the namespace, but that doesnt make much sense.
I have an engine (developed by me / the company I work for) that we use on several different projects. I just converted it to work with rails 3.1 w/ assets pipeline and everything seems to be working... for the most part.
My problem is that I need to extend the functionality of the UsersController with a little bit of app-specific spice, but I'm not sure about the best way to do it. The engine doesn't define a Users#show action, but this app does need it, so I added to the routes file:
JobEngine::Application.routes.draw do
root :to => 'home#index'
resource :users, :only => [:show]
resources :jobs, :only => [:show]
end
Then in my application I created the UsersController:
class UsersController < MyEngine::UsersController
def show
end
end
Then I made a users/show.html.haml view, I've stripped it down to only show one of the problem lines:
= link_to "Somewhere", job_path(3)
This gives me an error that reads undefined method 'job_path' for #<#<Class:0x00000102d69900>:0x00000102d4ed30>
Which is bizarre because before I made my app's UsersController inherit from MyEngine::UsersController it worked just fine.
When I do rake routes in the console, there are these lines:
users GET /users(.:format) {:action=>"show", :controller=>"users"}
job GET /jobs/:id(.:format) {:action=>"show", :controller=>"jobs"}
I can alter the class definition to be:
class UsersController < ApplicationController
and then the link works just fine. However, the engine's controller MyEngine::UsersController already inherits from ApplicationController. I can put code into my app's ApplicationController (like a before_filter) and it will run as expected, so I know my class definition does ultimately hit my app's ApplicationController, why is the job_path helper not working?
When I change the show action to read:
def show
job_path(3)
end
I get the error:
ActionController::RoutingError (No route matches {:action=>"show", :controller=>"jobs", :id=>3}):
app/controllers/users_controller.rb:9:in `show'
Which further confuses me because now it actually does recognize job_path as a method, but somehow the router isn't picking up where to go with all the correct parameters.
What am I doing wrong? What is the correct way to extend engine controller functionality? I saw the extending engine functionality question here.
And followed that code example, changing my class definition to instead re-open MyEngine::UsersController but I still get the exact same results concerning job_path(NUMBER)
UPDATE:
Ok I sort of figured out what's going on. Let's say your engine has a job_path route, and your application has a job_path route. If you're on a page that was accessed via an engine's controller, you can call the engine's helper with just job_path, but you can also call the main application's helper with main_app.job_path.
Likewise, if you're on a page accessed via one of your application's controllers, you access the engine's helper with my_engine.job_path and your own application's helper with job_path. This is assuming that you have something like mount MyEngine::Engine => "/my_engine", :as => 'my_engine'.
When you inherit an engine controller from your application, it then completely changes your route helpers to think you're in the context of the engine through the controller/view lifecycle. So to fix my problem all I really have to do is change it to be main_app.job_path(3) and it works.
I'm not completely satisfied with this solution because it feels a little...weird. Maybe I have a partial on this page that will be used on a separate non-inheriting page. Now the link helper will only work for one of the two pages, but never both =\ Am I missing something here...?
Try changing your Mount path in main app's routes with below,
mount MyEngine::Engine => "/"
This would solve your problem.
Though you can make this approach work, the semantics do not create a clean architecture. You can surmise this from the duplication of the Users controller - which implies that some User functionality is handled in the AppEngine, and some is handled in the parent app itself.
Instead, think about what functionality exists uniquely within the app, and which is packaged into the AppEngine gem. Perhaps with JobEngine, as you call it, your Users controller there is actually a UsersStatisticsController and, in the app, the controller there is the 'true generic' UsersController that handles CRUD, profiles, message queue, etc.
If you feel you must unify them into a single controller (rather than giving them distinct nomenclature), then you are best to create a Namespaced controller, where the various functionality can be conjoined thereby.
Even though this adds complexity, it's generally arguable that this is the most sound solution. Here's another post on SO about it
I just installed Ruby on Rails and created a scaffold called posts. RoR generated controllers and other required files for me.
I created a new method in posts_controller, but I can't access it. I looked at other methods that are in the controller and looks like I need to access them by /posts/[MY POST ID]/[MY METHOD NAME].
Assuming I created my custom method hello in the controller, how do i access it?
I looked at routes.rb, but there's no configuration for it.
Updated:
I understand that I can manually configure it in routes.rb, but how do all the other methods work? For example, I have "edit", and "update" methods in the "posts_controller.rb" controller. How do those two methods work without configuring routes?
# GET /posts/1/edit
def edit
#post = Post.find(params[:id])
end
I can't find a configuration that matches /posts/[0-9]/edit pattern.
The documentation you're looking for is Rails Routing From the Outside In. Once you've read this you'll understand everything Rails does to take your request and point it at method in your controller.
You need to add a route for it to routes.rb. For example:
# Previous routes
# resources :posts
# Updated routes
resources :posts do
get "hello", :on => :member
end
Have a look at this Rails guide about routing, it should help you understand Rails routing.
This will give you a good head start on the routes: http://guides.rubyonrails.org/routing.html
Not every method you make will have its own path, rails is built on the rest principle, and your scaffold created methods in the post controller that follow those paths, like index, show etc....
You can force your method to have a route added to it, but in reality you rarely actually need to do so as following the convention is far easier.
In Rails 3.x
match 'posts/hello' => 'posts#hello'
Available at example.com/posts/hello
When you used scaffold to generate post, it added a line resources :posts in your routes.rb file. That line configures routes for all the controller actions that were generated. As Caleb mentions above, not every action has a dedicated path. A single path can correspond to multiple actions because rails also takes into account the HTTP method. So, for instance, the path /posts with the HTTP method GET corresponds to the controller action index, while the same path with the HTTP method PUT corresponds to the controller action update. You can see these associations when you run rake routes from the console. I agree with Jordan and Caleb that the Rails Guides is a good read and will help you understand routes.
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.
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