I am facing an issue with inherited_resources when using a polymorphic nested resource, one of whose parents is a namespaced controller. Here is an abstract example:
# routes.rb
resources :tasks do
resources :comments
end
namespace :admin do
resources :projects do
resources :comments
end
end
# comments_controller.rb
class CommentsController < InheritedResources::Base
belongs_to :projects, :tasks, :polymorphic => true
end
When I access /admin/projects/1/comments, I get this error:
ActionController::RoutingError at /admin/projects/1/comments
uninitialized constant Admin::CommentsController
Now if I define the controller as Admin::CommentsController, I would need to move the file under controllers/admin which will in turn throw up an error for the url /tasks/1/comments
Is there a way I can fix this?
Why not keep CommentsController where it is and make a separate controller for admin in admin/comments_controller.rb? that inherits from it?
class Admin::CommentsController < CommentsController
before_filter :do_some_admin_verification_stuff
# since we're inheriting from CommentsController you'll be using
# CommentsController's actions by default - if you want
# you can override them here with admin-specific stuff
protected
def do_some_admin_verification_stuff
# here you can check that your logged in used is indeed an admin,
# otherwise you can redirect them somewhere safe.
end
end
The short answer to your question is mentioned here in the Rails Guide.
Basically you have to tell the route mapper which controller to use because the default is not there:
#routes.rb
namespace :admin do
resources :projects do
resources :comments, controller: 'comments'
end
end
That will take care of your routing problem, which is actually probably not related to Inherited Resources.
On the other hand, I have been unable to use Inherited Resources as well, in cases of a nested controller inside a namespace. I've moved away from that gem because of this.
I created something that might be interesting to you: a controller concern that will define all of the useful route helpers that inherited resources gives, in a way that accounts for namespaces. It's not smart enough to handle optional or multiple parentage, but it spared me a lot of typing long method names.
class Manage::UsersController < ApplicationController
include RouteHelpers
layout "manage"
before_action :authenticate_admin!
before_action :load_parent
before_action :load_resource, only: [:show, :edit, :update, :destroy]
respond_to :html, :js
create_resource_helpers :manage, ::Account, ::User
def index
#users = parent.users
respond_with [:manage, parent, #users]
end
def show
respond_with resource_params
end
def new
#user = parent.users.build
respond_with resource_params
end
# etc...
end
And then inside my views:
td = link_to 'Show', resource_path(user)
td = link_to 'Edit', edit_resource_path(user)
td = link_to 'Destroy', resource_path(user), data: {:confirm => 'Are you sure?'}, :method => :delete
Hope that helps!
Related
I have the Comment model, which is polymorphic associated to commentable models like Project, User, Update etc. And I have a page where a user can see every User's comment. I want a link near each comment with an address of an object this comment is associated with.
I could write something like that:
link_to 'show on page', Object.const_get(c.commentable_type).find(c.commentable_id)
But this will work only for not nested routes (like User). Here's how my routes look like:
resources :users do
resources :projects, only: [:show, :edit, :update, :destroy]
end
So when I need a link to a Project page, I will get an error, because I need a link like user_project_path.
How can I make Rails to generate a proper link? Somehow I have to find out if this object's route is nested or not and find a parent route for nested ones
You could use a bit of polymophic routing magic.
module CommentsHelper
def path_to_commentable(commentable)
resources = [commentable]
resources.unshift(commentable.parent) if commentable.respond_to?(:parent)
polymorphic_path(resources)
end
def link_to_commentable(commentable)
link_to(
"Show # {commentable.class.model_name.human}",
path_to_commentable(commentable)
)
end
end
class Project < ActiveRecord::Base
# ...
def parent
user
end
end
link_to_commentable(c.commentable)
But it feels dirty. Your model should not be aware of routing concerns.
But a better way to solve this may be to de-nest the routes.
Unless a resource is purely nested and does not make sense outside its parent context it is often better to employ a minimum of nesting and consider that resources may have different representations.
/users/:id/projects may show the projects belonging to a user. While /projects would display all the projects in the app.
Since each project has a unique identifier on its own we can route the individual routes without nesting:
GET /projects/:id - projects#show
PATCH /projects/:id - projects#update
DELETE /projects/:id - projects#destroy
This lets us use polymorphic routing without any knowledge of the "parent" resource and ofter leads to better API design.
Consider this example:
Rails.application.routes.draw do
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
resources :projects
resources :users do
# will route to User::ProjectsController#index
resources :projects, module: 'user', only: [:index]
end
end
class ProjectsController < ApplicationController
def index
#projects = Project.all
end
# show, edit, etc
end
class User::ProjectsController < ApplicationController
def index
#user = User.joins(:projects).find(params[:user_id])
#projects = #user.comments
end
end
This would let us link to any project from a comment by:
link_to 'show on page', c.commentable
And any users projects by:
link_to "#{#user.name}'s projects", polymorphic_path(#user, :projects)
I'm sure that there is a simple solution to this problem, but I can't for the life of me see what I am doing wrong - it's been a few months since I've worked on a Rails project, I must be forgetting something important.
I'm just trying to create a basic Rails form, but I am getting a no method path error when I navigate to the new form page.
This is for my Report model...
routes.rb
resources :report, only: [:new, :create], path_names: {new: ''}
report_controller.rb
def new
#report = Report.new
end
report/new.html.erb
<%= form_for #report do |f| %>
<% end %>
Navigating to http://localhost:3000/report yields
undefined method `reports_path'
Just to be comprehensive, here's the model...
class Report < ActiveRecord::Base
belongs_to :user
belongs_to :weather
belongs_to :feature
end
and the routes
report_index POST /report(.:format) report#create
new_report GET /report(.:format) report#new
I'm sure this is an amateur mistake... but I can't see what it is!
You need to change your routes to include a :show path if you want to be able to go to /report.
The path that I believe you are looking for is localhost:3000/reports/new
Oh for the love of god. It was a pluralization issue. The files should appear and be named as follows:
routes.rb
resources :reports, only: [:new, :create], path_names: {new: ''}
reports_controller.rb
class ReportsController < ApplicationController
def new
#report = Report.new
end
end
And the view files should all be in a folder called 'reports', not 'report'. The model is just the singular report.rb.
Say I have a module name Server that was created with a scaffold. I want the url 'www.example.com/server/' to be redirected to the first Server object that exists. So for example to be redirected to 'www.example.com/server/2'.
How could this be done with routes.rb (or any other way)?
route.rb:
Rails.application.routes.draw do
resources :servers
end
Server controller:
class ServersController < ApplicationController
before_action :set_server, only: [:show, :edit, :update, :destroy]
# GET /servers
# GET /servers.json
def index
#servers = Server.all
end
....
your can put
redirect_to server_path(Server.first) and return
inside your index method it'll redirect you when ever index action is called.
and just to extent #richfisher's answer (which might be a more appropriate way to do it.)
resources :servers, except: [:index] # this won't generate redundant routes
get '/servers/' => 'servers#first' #note this is now accessible via "server_path" instead of "servers_path" helper.
For what it's worth, I'd do this:
#config/routes.rb
resources :servers, except: :index do
get "", action: :show, id: Server.first.id, on: :collection
end
This will allow you to use the show action in place of index in a super efficient setup:
#app/controllers/servers_controller.rb
class ServersController < ApplicationController
def show
#server = Server.find params[:id]
end
end
Hi I'm getting this error
Circular dependency detected while autoloading constant
Subdomain::Settings::ThemesController
in my Rails 4.0 app whenever I try to access the stated controller. In fact I get similar errors for any of the other controllers name-spaced with the themes controller.
I have the following controllers name-spaced under Settings which itself is name-spaced under Subdomain.
Are these controllers defined correctly? Can anyone spot why this circular dependency error is cropping up?
# app/controllers/subdomain/settings/security_controller.rb
module Subdomain
class Settings::SecurityController < BaseController
def edit
...
end
end
end
# app/controllers/subdomain/settings/themes_controller.rb
module Subdomain
class Settings::ThemesController < BaseController
def edit
...
end
end
end
# app/controllers/subdomain/settings/profiles_controller.rb
module Subdomain
class Settings::ProfilesController < BaseController
def edit
...
end
end
end
# app/controllers/subdomain/base_controller.rb
class Subdomain::BaseController < ApplicationController
...
end
And the following routes configuration
MyApp::Application.routes.draw do
constraints(Constraints::SubdomainRequired) do
scope :module => :subdomain do
namespace 'settings' do
root to: 'security#edit'
resource :theme, only: [:create, :edit, :update], :controller => 'themes'
resource :profile, only: [:edit, :update], :controller => 'profiles'
resource :security, only: [:edit, :update], :controller => 'security'
end
end
end
end
The solution was I needed to rewrite each controller like so
module Subdomain
module Settings
class ProfileController
...
instead of
module Subdomain
class Settings::ProfileController
I know it is a typical problem. But I was just trying the routing in rails and I get this error:
uninitialized constant UsersController
And I don't know well where is the problem.
The resume of my app is a simple application of a library, where users can book books and see the books they've booked.
Here is my routes.rb
Brickstest2::Application.routes.draw do
resources :books
root "pages#home"
get "home", to: "pages#home", as: "home"
get "inside", to: "pages#inside", as: "inside"
devise_for :users
namespace :admin do
root "base#index"
resources :users
end
resources :books do
get :book_a_book, :as => "reserve"
end
resources :users do
get :booked_books, :as => "reserved", :on => :member
end
end
Actually When I do rake routes I get:
reserved_user_path
GET /users/:id/booked_books(.:format) users#booked_books
And in users_controllers.rb I have:
def booked_books
#user = User.first(params[:user_id])
#return unless #user == current_user
#books = Books.where(:user = #user)
end
The link to booked_books:
<%= link_to "My books", user_reserved_path(:user_id => current_user.id)%>
And finally my users/booked_books.html.erb:
<h1>My books</h1>
<%= #books.each do |book|%>
<p><%= book.name %>
<% end %>
Thanks in advance.
Have you declared a controller for your User resource that subclasses ApplicationController?
# app/controllers/users_controller.rb
class UsersController < ApplicationController
Note that the precise naming convention for a plural resource route for the User model is UsersController (note the s). I'm guessing that you've named your controller class UserController, which won't work for your use-case.
UPDATE:
Since you've declared a controller namespace, you'll want to declare your controller class like so:
# app/controllers/admin/users_controller.rb
class Admin::UsersController < ApplicationController
This should resolve any uninitialized constant UsersController errors involving your namespace.