rails route error undefined local variable or method - ruby-on-rails

I'm a beginner of RoR and this is my sample
class PagesController < ApplicationController
def welcome
end
end
in my route file
Rails.application.routes.draw do
root :to => “pages#welcome”
...
end
when I open powder

#config/routes.rb
root to: "pages#welcome"

Related

【Rails】How can I write child directory path in route.rb?

The code is below.
#app/controllers/admin/feeds_controller.rb
class Admin::FeedsController < ApplicationController
def api_index
#routes.rb
Rails.application.routes.draw do
scope '/hoge' do
resource :feeds, only: [] do
collection do
get :api_index
end
end
end
end
My wish is that path is hoge/feeds/api_index and can execute api_index action in admin/feeds_controller.
The routes.rb is currently Routing Error.
Because the path is controller/feeds.
How can I call api_index action in controller/admin/feeds?
Thank you.
Error(Add)
I wrote the below
#routes.rb
Rails.application.routes.draw do
scope '/hoge' do
resource :feeds, controller: 'admin/feeds', only: [] do
collection do
get :api_index
end
end
end
end
Then, I got a error
undefined local variable or method `api_index' for Admin::FeedsController:Class
But I definitely wrote def api_index in app/controllers/admin/feeds_controller.rb
How Can I do that?
You can use namespace.
More info in the Rails guide: https://guides.rubyonrails.org/routing.html#controller-namespaces-and-routing
#app/controllers/admin/feeds_controller.rb
class Admin::FeedsController < ApplicationController
def api_index
end
end
#routes.rb
Rails.application.routes.draw do
namespace :admin do
scope '/hoge' do
resource :feeds, only: [] do
collection do
get :api_index
end
end
end
end
end

I've received error "uninitialized constant ContactsController"?

I'm trying to create a form page, but keep getting the issue when I try and run the program. Using Ruby On Rails.
routes.rb is:
Rails.application.routes.draw do
root to: 'pages#home'
get 'about', to: 'pages#about'
get 'signup', to: 'pages#signup'
resources :contacts
end
contacts_controller is:
class ContactsController < ApplicationController
def new
end
end
What's the issue?
So normally if you get that error, it means you have a typo either in the file name or the class name at the top of the file.
So try to check that.
Make sure that the file is not misspelled. Should just be contacts_controller.rb

Rails 5 Routing Error - uninitialized constant DashboardController

I'm building out a multi-tenant site and am using Ryan Bigg's https://leanpub.com/multi-tenancy-rails as a guide. I am stuck at creating a simple scoped route; I keep getting the error Rails 5 Routing Error - uninitialized constant DashboardController. I am sure I am missing something simple as syntax has changed a bit since the final release of the publication. Does my code below look as it should--right now I am just looking to get the dashboard index page to show?
controllers/accounts/base_controller.rb
module Accounts
class BaseController < ApplicationController
before_action :authenticate_user!
end
end
controllers/accounts/dashboard_controller.rb
module Accounts
class DashboardController < Accounts::BaseController
def index
end
end
end
views/accounts/dashboard/index.html.rb
<section class="bg--secondary space--sm conversation">
<div class="container">
<div class="row">
<div class="col-md-6">
<h1>Dashboard</h1>
</div>
</div>
</div>
</section>
routes.rb
constraints(SubdomainRequired) do
scope module: 'accounts' do
root to: 'dashboard#index', as: :account_root
end
end
Rails.application.routes.draw do
devise_for :users
constraints(SubdomainRequired) do
root to: 'dashboard#index', as: :account_root
end
get '/accounts/new', to: 'accounts#new', as: :new_account
post '/accounts', to: 'accounts#create', as: :accounts
root 'welcome#index'
end
I made multi tenant project soon and I had used lvh.me for development environment. Try the following solution:
config/initializers/subdomain_constraint.rb
class SubdomainConstraint
def initialize
#domain_development = 'lvh.me'
end
def matches?(request)
if Rails.env.development?
#domain_development == request.domain
end
end
end
routes.rb
constraints(SubdomainConstraint.new) do
match '', to: 'dashboard#index', constraints: {subdomain: /.+/}, via: [:get]
end
I'm not sure you can define a class in the routes.rb file. Perhaps if you create a new file called lib/subdomain_required.rb and moved
class SubdomainRequired
def self.matches?(request)
request.subdomain.present? && request.subdomain != 'www'
end
end
to that file instead of putting it in your routes.rb.

How to fix Uninitialized Constant in Controller

I am getting a:
uninitialized constant ProfilesController::EUserPofile
error when trying to:
class ProfilesController < ApplicationController
def index
##profiles = EUserProfile.all
end
def preview
#profiles = EUserPofile.all
end
end
it works fine for index but for preview it crashes.
here is my route file:
Rails.application.routes.draw do
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
get 'welcome', to: 'pages#home'
get 'profiles', to: 'profiles#index'
get 'login', to: 'login#login'
resources :profiles do
get 'preview', on: :member
end
end
There's a typo: EUserPofile -> EUserProfile

Routing Error uninitialized constant when I create a new action

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.

Resources