Rails routing gives file not found error - ruby-on-rails

I have a Ruby on Rails application with the following entries in routes.rb:
get '/teachers/welcome', to: 'teachers#welcome'
Which means that if I type: http://localhost:3000/teachers/welcome then I should be able to see the welcome view from the teachers controller. But I keep getting File Not Found error. I'm new to Ruby so bear with me.
When I look at the application, the files are there:
app/controllers/teachers_controller.rb
app/views/teachers/welcome.html.erb

Please watch the naming of you model (without s) but your contoller with s .. and the view name have to match the action name . And run rake routes to check your working routes . + be carefull of the routes order in the routes.rb file .. and will not have any problem in your life with the routes

Assuming that you have a method(action) 'welcome' in your controller try this
get 'teachers/welcome' => 'teachers#welcome'

Make sure you have everything named properly, like the controller and the view.
app/controllers/teachers_controller.rb
class TeachersController < ApplicationController
def welcome
end
end
app/views/teachers/welcome.html.erb

You have to remove the first slash
get 'teachers/welcome' => 'teachers#welcome'
Your Controller:
# app/controllers/teachers_controller.rb
class TeachersController < ApplicationController
def welcome
end
end
Note: you can use a generator to create it automatically:
rails generate controller teachers welcome

Related

Accessing model in controller equals nil

I've got the following problem
I have a model called Article, which I access at two points.
First at the "home-page ( root )" I just do Article.all and list all of them.
That works fine for me, but whenever I access Article.all on my admin-panel ( /admin/articles ) it just says the following:
My controller looks like this:
def index
#news = Article.all
end
My model can be accessed easily in the rails console:
All of this is somehow solved by opening the controller and just re-saving it.
After a rails restart or logout and login on the site it is broken again
You may wish to organize groups of controllers under a namespace. Most commonly, you might group a number of administrative controllers under an
Admin:: namespace.
At the console:
namespace :admin do
resources :articles
end
This will create a number of routes for each of the articles
use one of the routes to access your controller from Admin
Try converting to array,
#news.to_a.count

NoMethodError in Rails while saving data

Checks_controller
class Checkscontroller < ApplicationController
def show
#check= Tester.find(params[:id])
end
def new
end
def create
#check = Tester.new(check_params)
#check.save
redirect_to #check
end
def check_params
params.require(:check).permit(:title, :description)
end
end
I am trying to save the data in 'checks' controller to 'Tester' model, getting "NoMethodError in ChecksController#create", undefined method tester_url' for#` while trying to save the data to my DB. There seems to be some issue on this line: "redirect_to #check".
Routes.rb
Rails.application.routes.draw do
get 'home/screen'
resources :checks
root 'home#screen'
end
EDIT: I see this answer got accepted. To anyone else looking at this: PLEASE DO NOT DO THIS WITHOUT A REALLY GOOD REASON.
Ok, so since you want to use the ChecksController for your Tester model, you'll have to add this to your routes: note that I'm assuming that you do not have a Check model, since I don't see it anywhere and youre using Tester as a check?
resources :testers, as: 'checks' controller: 'checks'
This line will make it so that /checks/1 goes to a Tester object with ID: 1, and use the ChecksController show method to show it
Old answer, for posterity
You're getting this error because you're missing routes for your Tester model in your routes.rb file.
You could add resources :testers to it and it will work. Of course you also already need AT LEAST your TestersController to exist with a show action
This error is occurring because when you redirect_to #check, Rails knows it's a Tester object and expects a route called tester to route to TestersController#show. It's attempting to use a helper method that rails creates for routes, called tester_url

Rails generate controller (under a namespace)

I want to add a controller and its routes entry under a namespace (api) for which I am proceeding with rails generate api/Users my_method, which creates the files and entries as follows:
create app/controllers/api/users_controller.rb
route get "users/my_method"
invoke erb
create app/views/api/users
create app/views/api/users/my_method.html.erb
Everything worked fine apart from the routes entry. What I am assuming is it should create the routes entry as well under the correct namespace or it shouldn't create it at all, or I am doing something wrong.On the other hand when going with scaffold way it does correctly.
Is it something which we need to do it manually?
Using ruby 2.0 and rails 4 for the application.
Type in terminal
rails generate scaffold Api::User username email
rake db:migrate
this is part of result
class Admin::ServicesController < ApplicationController
# GET /api/users
# GET /api/users.json
def index
#api_users = Api::User.all
end
To do generate and I think you will understand everything, don't forget see new app structure :-), Good luck, and solve your problem quickly as possible.
You can namespace your routes in your config/routes.rb like this
namespace :api do
resources :user
end

What will be my route?

I'm working on Rails 3.
My URL is: http://localhost:3000/terms_and_conditions?val=pp
My method is below:
class GeneralController < ApplicationController
def terms_and_conditions
if !params[:val].nil?
#val=params[:val]
else
#val='tc'
end
end
end
What will be my route? Please help me to create the route.
I suggest you first read the guides titled Rails Routing from the Outside In.
To setup a simple GET accessible route add the following to your routes.rb file
get "/terms_and_conditions" => "general#terms_and_conditions"
If you need more than just GET, you can use match instead. In your app root you can perform rake routes to see all the routes of your app as well. With regards to your choice of exposing /terms_and_conditions — it would be better if you used a shorter path such as /terms and/or consider doing /terms-and-conditions instead.
Try:
[YourAppNameHere]::Application.routes.draw do
match '/terms_and_conditions', to: 'general#terms_and_conditions'
end

Ruby on Rails how does this route not work

In my config > routes I have:
#Service Routes
match "services" => "services#index"
match "startsingleservice" => "services#start_single_service"
match "stopsingleservice" => "services#stop_single_service"
match "zookeeperreindex" => "services#show_zookeeper"
The first 3 work, no issues no problems. And all four are in the same file def/functions whatever you wanna call them. Are in the same file. Where again first 3, work awesome. Adding that new guy there, zookeeper just doesn't wanna work I get
Unknown action
The action 'show_zookeeper' could not be found for ServicesController
the function zookeeperreindex is almost a mirror of the actual index def in the same file, changed for the needs of redisplay as I only want a JSON output for that one. But bottom line is I changed the routes to match, I know the function is working for the most part, and I am not seeing where I could be messing this simplicity up, I've also restarted the server itself to ensure it wasn't that
Edit
In replying with code from the controller which by the way did "show_zookeeper" defined right.. I realized I had a misplaced "end" tag.. So, in moving that the route worked.
It looks like in your ServicesController (app/controllers/services_controller.rb)
You never define a method show_zookeeper. My guess is that you define a method zookeeperreindex instead of show_zookeeper.
Why don't you link the contents of that file? You should see something along the lines of,
class ServicesController < ActionController::Base
def index
...
end
def start_single_service
...
end
def stop_single_service
...
end
def show_zookeeper # <---- This one is missing
end
end
The way the routes work the part after the => determines the controller and action. For example "services#start_single_service" will be mapped to :controller => ServicesController, and :action => start_single_service.
Thus the final call will be ServicesController.start_single_service
Look at http://guides.rubyonrails.org/routing.html for more info

Resources