I tried to add namespace in my RoR project.
It works as expected:
controller:
class Dashboard::CategoriesController < ApplicationController
...some code
end
routes.rb
namespace "dashboard" do
resources :categories
end
but it doens'y work:
class Dashboard::UsersController < ApplicationController
...some code
end
class Dashboard::CardsController < ApplicationController
...some code
end
routes.rb:
namespace "dashboard" do
resources :users do
resources :cards do
member do
post 'review'
end
end
end
end
it throws an routing error: uninitialized constant CardsController
what's wrong?
Rails auto loads class if its name match file name. Error indicates that CardsController class is not loaded, so most probably you named your controller file wrongly. It should be app/controllers/dashboard/cards_controller.rb.
First of all, you'll be better making your routes more compacted:
#config/routes.rb
namespace :dashboard do
resources :users do
resources :cards do
post :review #-> domain.com/dashboard/users/:user_id/cards/1
end
end
end
The error it self will likely be caused by the way in which you're trying to call the controller. Typically, you'll receive errors with the namespace prepended to the class name (Dashboard::CardsController). In this instance, it just says CardsController.
You'll need to look at how you're calling the route.
Specifically, I presume you're using a form_for - which will build the route for you. If this is the case, you'll need to use the namespace within the form_for declaration itself:
<%= form_for [:dashboard, #user, #card], url: dashboard_user_cards_review(#user, #card) do |f| %>
Related
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.
I'm trying to add a namespace to my 'Category' controller and resource.
So the first thing I did was to move the categories_controller.rb to app/controllers/api/v1/categories_controller and the category_resource.rb to app/resources/api/v1/
And then I redeclared these artifacts as following:
Controller
module Api
module V1
class CategoriesController < ApplicationController
#before_action :doorkeeper_authorize!
end
end
end
Resource
module Api
module V1
class CategoryResource < JSONAPI::Resource
attribute :name
end
end
end
And in routes.rb I moved the categories route to
namespace :api do
namespace :v1 do
jsonapi_resources :categories
end
end
I already got different erros trying to solve this issue. To the current configuration, this is the error I get:
JSONAPI: Could not find resource 'categories'. (Class CategoryResource not found) (NameError)
What am I doing wrong?
Based on the documentation here (https://github.com/cerebris/jsonapi-resources) you should not move the resource.
And it should not be in the modules.
Your code looks fine - I have something similar with jsonapi-resources 0.7.0:
class Api::V1::UsersController
...
class Api::V1::UserResource < BaseResource
...
namespace :api do
namespace :v1 do
jsonapi_resources :users do
jsonapi_relationships
end
is it possible your rails load path is trying to load the api/v1 directory directly rather than treating it as a module subfolder?
I have a class called Device. It has a model device.rb
I have set the routing up so that the same controller is called from two different paths. i.e. the paths:
/driver_api/v1/devices
and
/sender_api/v1/devices
both call the following controller:
/user_api/v1/devices
In my routes.rb I have:
namespace :driver_api do
namespace :v1 do
resources :devices, :only => [:create], controller: '/user_api/v1/devices'
end
end
namespace :sender_api do
namespace :v1 do
resources :devices, :only => [:create], controller: '/user_api/v1/devices'
end
end
Now, in my devices controller, I'm trying to call a Device class method. i.e. in my controller:
class UserApi::V1::DevicesController < ApplicationController
Devise.method_name(input)
end
But i get an error:
uninitialized constant UserApi::V1::DevicesController::Device
Why am I getting this error?
Because you wrote Devise and not Device that could be a good reason.
If that is just a typpo from the question, here's another alternative.
Sometimes there's some kind of naming problem when the classes are defined in the way you did (I ignore the reason why that happens)
Try to decompose the class scoping by defining the modules:
module UserApi
module V1
class DevicesController < ApplicationController
# rest
end
end
end
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.
I have a questions controller and an associated model and a number of rest routes. Here is how it's set up in routes.rb:
resources :questions
I want to add a custom route that has the format /questions/widget/ID (where ID is the id of the question for which I want to generate a widget). I want this to be processed by the "widget" action in my questions controller. I've tried a number of things such as:
resources :questions do
member do
get 'widget/:id'
end
end
But nothing is working. I'm sure I'm missing something simple. Any ideas? Thanks in advance.
You do not have to specify the id since you are inside resources. It should look like:
resources :questions do
member do
get 'widget'
end
end
You can get more information from the Rails Guide. Look at section 2.9.1.
Edit: I just noticed that you are trying to match get /questions/widget/:id. This will set up a route for get /questions/:id/widget. This is more in line with Rails convention. If you really want it the other way, you need to set up a custom match statement:
match "/questions/widget/:id" => "questions#widget"
However, I would stick with convention.
I know it is old, but looking to fix another routing problem I ended here, it is possible, to do what you are asking for, here is an example
resources :articles do
get 'by_tag/:tag' => :by_tag, on: :collection
get 'by_author/:author' => :by_author, on: :collection
resources :comments, except: :show
end
now you have /artices/by_tag/:tag . The trick was to use on:collection.
Obviously don't forget to add the by_tag action and by_author.
class ArticlesController < ApplicationController
.....
def by_tag
...
end
end
Check this route works with
melardev#local~$ rails routes
Why don't you use this routes:
resources :questions do
resources :widgets
end
it will create path like questions/:question_id/widgets/new for you to create new widget for question with specific id of question.
This is what ended up working for me:
resources :post do
get "author/:author", to: "posts#author", on: :collection, as: "author"
end
Which outputs the following route:
author_posts GET /posts/author/:author(.:format) posts#author
Then in your controller, you need to create the author action:
class PostsController < ApplicationController
def author
#roles = Post.where(author: params[:author])
render :index # to reuse the index view
end
end
Then in your view:
<%= link_to post.author, author_posts_path(post.author), data: { turbo_frame: "_top" } %>