Ruby On Rails Not Creating The View For A Controller - ruby-on-rails

I'm definitely very new to Ruby on Rails. I created a new rails folder like this:
Edit: Sorry for the inconvienience, but I did use the --api tag.
rails new <project-name> -d mysql --api
since I use mysql instead of sqlite. Then, in the tutorial they used a command like this:
rails generate controller Welcome index
I did the exact same thing, however in the log it shows this:
create app/controllers/welcome_controller.rb
route get 'welcome/index'
It didn't seem to create the view. I don't know why it's doing this. After I start the server with rails server I can go to the localhost:3000/welcome/index but it just takes me back to the home page, and in the log it says Started GET .... No Content ... . So it couldn't find the view. How can I fix this?

Unless you created an API only project with the --api flag, the rails generate controller Welcome index command should have created an index.html.erb file in the views folder.
You can either delete the welcome_controller.rb file and the generated line from the route.rb file and run the command again or you can manually create the view with the touch command (assuming your OS supports the touch command):
touch app/views/welcome/index.html.erb
or through you favourite text editor or IDE. On a side note, controller names should be plural (WelcomePagesController for example).

Related

Getting "Uninitialized constant" routing error when loading a view

I'm a beginner to both ruby and rails, and using Rails 5.17 to develop a web app for a class.
Creating the empty Rails project was successful, but something is going wrong when creating a new controller. I generated a new controller named cars from the root of the project, which was successful. There was a file in app/controllers named cars_controller.rb which looks like this:
class CarsController < ApplicationController
end
I added a method to this file named hello that does nothing.
I then created a file named cars.html.erb in the app/views/layouts directory. This file is a basic page of html code.
In config/routes.rb, I added the following:
get '/cars', to:: 'cars_controller#hello'
resources: cars
After all of this, I ran rails server, and opened localhost:3000 in a browser. This brings up the normal Ruby on Rails welcome page.
But when I go to localhost:3000/cars, I get the following:
Routing Error
uninitialized constant CarsControllerController
I've tried changing the name of the cars_controller.rb file. I've tried changing the name of the class in the controller file from CarsController to Cars. I've tried many different routes in routes.rb. I finally tried uninstalling Rails 5.17 and installing Rails 5.13.
I'm very confused, and I'd be grateful for any advice I can get. Thanks in advance!
One of the great things about Rails is its preference for convention over configuration. However, for this to really benefit you, you need to stick to doing things “The Rails Way” rather than your own way, wherever possible.
In this case, start by getting rid of your custom get route, and just use resources :cars.
From the command line, run rake routes (you might be able to run rails routes on your rails version too) and see the routes that it has created for you.
Now, rename the method you added to your CarsController from hello to index.
Move your hello.html.erb file from app/views/layout to app/views/cars/index.html.erb.
Finally, start the rails server (rails start) and load the url http://localhost:3000/cars in your browser.
—-
Note that templates in app/views/layout have a special purpose. These are used to apply a general template to your views. Look up the use of layout within a controller for more details
I think you have an error in how you had defined your route - you don't need _controller.
Instead, try this:
get '/cars', to: 'cars#hello'
Also, keep in mind that in your cars directory you need the view: hello.html.erb

Terminal does not generate controller in the editor

I am new to programming in Ruby and Rails. I am following the instructions to generate a new controller (rails generate controller welcome index). In the terminal, it creates correctly the controller, it appears:
create app/controllers/welcome_controller.rb
/ route get 'welcome/index'
/ invoke erb
/ create app/views/welcome
/ create app/views/welcome/index.html.erb
and the rest.. but I can't see the controller in the text editor.
I tried to delete and generate again the controller, and I have also check with ls in the terminal, and it lists the controller, but in my text editor, I don't see the view and the action in the controller. I am using a ruby 2.3.3 in a mac.
Looks like your editor has cached file tree, you need to refresh it.
What is the editor?

Is there a way to add route to Rails by command line?

I am a beginner in ruby on rails. I found it quite inconvenience to add route to route.rb manually every time I add a new action or page to controller or the project. So I want to know if there is a way using command line rather than editing the route.rb file?
Adding routes to the routes.rb file from the terminal can be easily achieved with sed.
Install sed using the following command (Ubuntu):
sudo apt get install sed
Assuming you are in the root directory of your app, here is the command to add the routes:-
sed -i '23iresources :people' config/routes.rb
This is what it does:
File to add text to is config/routes.rb
Line number to insert text to is 23
-i is the insert flag: the text will be inserted
resources :people is what gets added
Now, the route resources :people will be inserted on line 23 in config/routes.rb file in your Rails app.
I don't know about a commandline way to update routes, but you might consider using wildcards instead, that way a single line in your routes file can allow you to access many pages on your site:
http://guides.rubyonrails.org/routing.html#route-globbing-and-wildcard-segments
If you do rails generate -h
rails generate -h
Please choose a generator below.
Rails:
assets
controller
generator
helper
integration_test
jbuilder
job
mailer
migration
model
resource
scaffold
scaffold_controller
task
As you can see there is no generator for routes by default. This is one of the points where rails runs out of its Magic. Of course you can write your generator Creating and Customizing Rails Generators & Templates.
But I would recommend, be ready to write code when there isn't a quick default way. Once you become a pro you will quite often see rails magic falling short of the solution your are trying to implement.

Ruby On Rails "Hello World" page doesn't work

I'm new in ruby on rails and I have a problem. When I starе server " rails server and going to the page
www.localhost:3000
in my browser everything works fine with ROR default page, but.... after making controller rails generate controller demo index
and going to the page
www.localhost:3000/demo/index , localhost:3000/demo - the same
- there is nothing, seems like page doesn't exist, only a blank page. I tried to change index.html.erb code
<h1>Hello World</h1>
- no change. I want to practice with ROR, but can't because of that bug.
Work environment: Windows 7 64-bit
Database: MySQL
You need to have a view corresponding to your action.
If your controller is called Demo and your action is index it should be
# app/controllers/demo_controller.rb
class DemoController < ApplicationController
def index
end
end
and the view file should be at app/views/demo/index.html.erb.
The index action is the base action of the controller. Try just:
http://localhost:3000/demo
to access the index.
By default routes /demo/index is the show action with the id of index.
make sure
views/layout/application.html.erb has yield and index.html.erb have some text
check your routes.rb
root :to => 'demo#index'
Go to http://localhost:3000/
Uncomment this line from your config/routes.rb file
match ':controller(/:action(/:id))(.:format)'
Remove index.html file from public/index.html
First of all delete or rename the index.html file from public folder.
Then set root :to => 'demo#index' at route file.
Then restart the server by using this command. rails s by default your server will run at port 3000.
Now simply type http://localhost:3000/ or http://localhost:3000/demo/index
Have you deleted
app_root
--public
----index.html
yet?
Rails will look in the public folder first for a matching file before it will goto build one from your views.
As indicated by João there, you will also need a view for that. Additionally, delete public/index.html and finally add a route for your controller.
If you had created a scaffold Rails start guide you would already have the route there for you.

Wrapping with application.html - rails

I am really new to rails and working to customize a project. I generated a new scaffold, called it New_Scaffold. When the New_Scaffold's index.html.erb is displayed, the wrapper of the application.html.erb (where the yield method is called) is not displayed.
I thought it was automatic in rails, is it not ? How can I display the application.html.erb to wrap the New_Scaffold's index.html ?
The scaffold command generates a bunch of things for a model and controller for you automatically.
If you want to try Rails out try to create a new project like this:
rails new blog
cd blog
rails generate scaffold Post title body:text
rake db:migrate
rails server
This will create a basic project with one model, Post, that will have a generic setup with views and a controller that responds to all the RESTful actions.
the application.html.erb file can be found under views/layouts

Resources