'uninitialized constant SubcategoriesController' - Rails - ruby-on-rails

I have a SubcategoriesController
class Categories::SubcategoriesController < ApplicationController
def new
#category = Category.find(params[:category_id])
#subcategory = Subcategory.new
end
def edit
end
def create
end
def destroy
end
end
These are my routes
resources :categories do
resources :subcategories, except: [:index, :show]
end
When I'm trying to visit categories_path, it throws uninitialized constant SubcategoriesController
Where did I go wrong?

Current route configuration expects to find SubcategoriesController in app/controllers/subcategories_controller.rb, but you have declared Categories::SubcategoriesController (perhaps in app/controllers/categories/subcategories_controller.rb).
Try moving and renaming the controller so router can find it.
Hope that helps!

Related

Why am I getting a recordnotfound error when trying to access an instance in rails?

I have a user profile controller called "userinfo" and it's corresponding view. The userinfo index is the root path. In the homepage(which is the userinfo index), I have a link that takes you to the user profile page. It is giving me this error when I go to the home page:
My routes are:
My userinfos_controller:
class UserinfosController < ApplicationController
before_action :find_userinfo, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user!
def index
#userinfors = Userinfo.find(params[:id])
end
def show
#myvideo = Video.last
end
def new
#userinformation = current_user.userinfos.build
end
def create
#userinformation = current_user.userinfos.build(userinfo_params)
if #userinformation.save
redirect_to root_path
else
render 'new'
end
end
def edit
end
def update
end
def destroy
#userinformation.destroy
redirect_to userinfo_path
end
private
def userinfo_params
params.require(:userinfo).permit(:name, :email, :college, :gpa, :major)
end
def find_userinfo
#userinformation = Userinfo.find(params[:id])
end
end
and my view is:
<%= link_to 'profile', userinfors_path(#userinfors) %>
My routes.rb file:
Rails.application.routes.draw do
devise_for :users
resources :userinfos do
resources :videos
end
resources :pages
get '/application/decide' => 'application#decide'
root 'userinfos#index'
get '/userinfos/:id', to: 'userinfos#show', as: 'userinfors'
end
Thanks for any help!
ok, there are multiple errors and you are not following conventions of rails, index is not for what you have used.
Index is used to list all the users and show for a particular one with id passed in params.
Your index path is, as you can see, /userinfos which is correct and it doesn't have any id with it but you are trying to find user with params[:id] which is nil and hence the error.
Lets try out this:
def index
#userinfors = Userinfo.all #pagination is recommended
end
In your index view,
<% #userinfors.each do |userinfor| %>
<%= link_to "#{userinfor.name}'s profile", userinfo_path(userinfor) %>
<% end %>
It should work now.
Please read routing and action controller to get the idea and understand the magic behind rails routing and mvc architecture..

Rails 4: One-to-one controller issue

I'm building an app which consists on sharing résumés. I am using Devise gem. Each user is able to create only one résumé. I made the models and and their relations. Resume belongs_to User and User has_one 'Resume'.
After making the views, I wanted to test my app but I got the error: undefined methodbuild' for nil:NilClass`
Here is my ResumeController and my routes.rb
class ResumeController < ApplicationController
before_action :find_resume, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user!, except: [:show]
def show
# #resume = Resume.find_by(params[:id])
end
def new
#resume = current_user.resume.build
end
def create
#resume = current_user.resume.build(resume_params)
if #resume.save
redirect_to #resume, notice: "resume was successfully created"
else
render 'new'
end
end
def edit
end
def update
if #resume.update(pin_params)
redirect_to #resume, notice: "resume was successfully updated"
else
render 'edit'
end
end
def destroy
#resume.destroy
redirect_to root_path
end
private
def resume_params
params.require(:resume).permit(:title, :description)
end
def find_resume
#resume = resume.find(params[:id])
end
end
Routes.rb
Rails.application.routes.draw do
devise_for :users
resources :resume, except: [:index]
get 'static_pages/index'
root to: "static_pages#index"
end
I just want the user to be able to create only one Resume and then he will be able to share it.
Update: After following messanjah's answer there was another error coming from the _form.html.erb: undefined method resumes_path' for #<#<Class:0x00...>. Here is the gist with forms and model: goo.gl/XvW2LH So you can see all the files closely.
Without more knowledge of where the error is happening, I can only suggest some areas that might be suspect.
To build a has_one relationship, you must use the build_*association* constructor.
def new
#resume = current_user.build_resume
end
def create
#resume = current_user.build_resume(resume_params)
end

Rails: Redirecting to post after comment submission

I have a form to submit comments on posts.
After submitting comments, the user should be redirected to the post.
I am getting the following error when I hit the "submit" button:
NameError in CommentsController#create undefined local variable or method `post' for CommentsController...
The error points to the following line in my comments controller:
redirect_to post_path(#post)
Here's my comments_controller.rb:
class CommentsController < ApplicationController
before_action :authenticate_user!
def create
#topic = Topic.find(params[:topic_id])
#post = #topic.posts.find(params[:post_id])
#comment = Comment.new(comment_params)
#comment.post = #post
redirect_to post_path(#post)
end
private
def comment_params
params.require(:comment).permit(:title, :body)
end
end
And here's my route file:
Rclone::Application.routes.draw do
get "comments/create"
devise_for :users
resources :users, only: [:update]
resources :topics do
resources :posts, except: [:index] do
resources :comments, only: [:create]
end
end
get '/posts/:id/comments', to: 'posts#show'
get 'about' => 'welcome#about'
root to: 'welcome#index'
end
What am I doing wrong on the routes file?
Comment.new will not presist or create the post. You need to do #comment.save. Also use pry and pry-nav gem to debug such errors. pry will halt the execution of you program where it finds binding.pry. From that point onwards you can execute your program line by line. Just insert binding.pry one line before where you want to halt execution.
example
def create
binding.pry
#topic = Topic.find(params[:topic_id])
Check you params hash and you will find your bug. My guess is you are going wrong with topic_id or post_id
Links:
http://pryrepl.org/
https://github.com/pry/pry

superclass mismatch for class when namespacing admin controller

Hey I am trying to namespace a bunch of controllers that I only want admins to be able to access. For example, I want routes like admin/products or admin/categories, but when I call any of those controllers located in my controllers/admin folder, I get the following error message
superclass mismatch for class CategoriesController
If I restart the server right after, I get this
Unable to autoload constant Admin::CategoriesController
and
Circular dependency detected while autoloading constant Admin::CategoriesController
These are my routes
Rails.application.routes.draw do
root 'pages#home'
devise_for :admins
namespace :admin do
resources :categories, :except => [:new, :show]
resources :products
end
resources :products
resources :carts, :only => [:show]
resources :line_items, :only => [:create, :destroy]
# Shop controller
get 'shop/index
# Admin controller
get 'admin/index'
This is my categories controller
class CategoriesController < ApplicationController
before_filter :authenticate_admin!
def index
#categories = Category.all
#category = Category.new
end
def create
category = Category.new(categories_params)
if category.save
flash[:notice] = "You have added a new category"
redirect_to categories_path
else
flash[:error] = "An error occured"
render "index"
end
end
def edit
#category = Category.find(params[:id])
end
def update
#category = Category.find(params[:id])
if #category.update(categories_params)
flash[:notice] = "Succesfully updated #{#category[:name].titleize}"
redirect_to categories_path
else
flash[:error] = "An error occured trying to update #{#category[:name].titleize}"
render "edit"
end
end
def destroy
#category = Category.find(params[:id])
if #category.destroy
flash[:notice] = "You succesfully removed #{#category.name}"
else
flash[:error] = "An error occured trying to remove #{#category.name}"
end
redirect_to categories_path
end
private
def categories_params
params.require(:category).permit(:name)
end
end
This has been troubling me for some time now so any help will be greatly appreciated
You forgot to namespace the Controller class :
class Admin::CategoriesController < ApplicationController
which is why you receive Unable to autoload constant Admin::CategoriesController error as Rails is looking for namespaced class Admin::CategoriesController and what you have is CategoriesController

Update route in rails doesn't respond well to after_action?

class FrogsController < ApplicationController
before_action :find_frog, only: [:edit, :update, :show, :destroy]
after_action :redirect_home, only: [:update, :create, :destroy]
def index
#frogs = Frog.all
end
def new
#ponds = Pond.all
#frog = Frog.new
end
def create
#frog = Frog.create(frog_params)
end
def edit
#ponds = Pond.all
end
def update
#frog.update_attributes(frog_params)
end
def show
end
def destroy
#frog.destroy
end
private
def find_frog
#frog = Frog.find(params[:id])
end
def frog_params
params.require(:frog).permit(:name, :color, :pond_id)
end
def redirect_home
redirect_to frogs_path
end
end
Hi all. I was wondering if someone could explain to me why the update route in rails can't take my after_action of redirecting (custom made method on the bottom) it home. The error that I get when i include update in the after_action is "Missing template frogs/update".
This is going to cause me to manually add a redirect_to frogs_path inside the update method.
thanks!
The after_action callback is triggered after the action has run its course. You cannot use it to render or redirect. Do that within the action itself by calling the method:
def update
...
redirect_home
end

Resources