Rails 3.2.11 First Test App -- Template is Missing - ruby-on-rails

I'm a newbie in ruby on rails Web-Programming. Today I tried to set up Ruby programming language and also the Rails framework. Ruby works properly, I made a first Test-Class Successfull. Only setting up my Rails framework prepares me some problem.
I made a test_app and i tried to run it.
rails new test_app
rails s
I realized that the the routes was commented in routes.rb and I uncommented it.
I changed #root :to => 'welcome#index' to root :to => 'welcome#index'.
I also realized that I do not had a controller for the page welcome/index and i created it
with rails g controller Welcome index.
But It do not work yet? Anybody an idea?
Template is missing
Missing template welcome/index, application/index with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :coffee]}. Searched in: * "D:/Davide Giunta/Development/workspace[ruby]/test_app/app/views"
Ruby Version: 1.9.3p368
Gem Version: 1.8.24
Rails Version: 3.2.11

rails g controller will only create the controller part, you also need to create a corresponding view file, in this case, you want app/views/welcome/index.html.erb
Using scaffold (only while learning), or resource for your generator might be faster. (I usually create them all by hand these days)

The answer from Jim Deville is correct. You need to create corresponding view file of your controller's action (action_name.html.erb) and place it to views dir under subdirectory named like your controller.
See basics of Rails here http://guides.rubyonrails.org/getting_started.html.
When you want to create action with controller, view and model you should use rails g scaffold SomeName also see http://guides.rubyonrails.org/getting_started.html#getting-up-and-running-quickly-with-scaffolding.

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

Rails Generate Controller Pages Home : Helper Error

I'm very new to Rails and am trying to get started but have run into a problem, I've searched around and it seems like a lot of people have had the same problem but I either don't understand their solutions or don't seem to have the files that they changed to fix it,
I'm currently using Windows and running all commands through Git Bash, I have Ruby v2.2.4p230, and Rails v4.2.5.1, through Git Bash, I have just run these commands
cd ~/Desktop
rails new pinteresting
cd pinteresting
rails generate controller pages home
rails server
So now if I go to the localhost3000, it gives me the basic sample layout of the app which is fine, but then I try to visit the pages/home and I get one of three errors,
Missing helper file helpers/c:/users/acer_pc/desktop/pinteresting/app/helpers/application_helper.rb_helper.rb
Missing helper file helpers/c:/users/acer_pc/desktop/pinteresting/app/helpers/pages_helper.rb_helper.rb
Missing template pages/home, application/home with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :coffee, :jbuilder]}. Searched in: * "c:/Users/Acer-PC/desktop/pinteresting/app/views"
If it helps, this is the folder https://github.com/Fuledbyramen/pinteresting
I set up your project on my localhost and I am able to visit http://localhost:3000/pages/home without any issues.
You have set get 'pages/home' three times in routes.rb, one is enough.
Thank you guys for all the replies, finally figured it out, like all the other SO answers, its something to do with the capitalization of file names which I just couldn't figure out, so I just wrote it directly to /c instead of ~/desktop, then in application.html.erb just changed application in lines 5 and 6 to default and it works... Wasn't missing any files or needed to do any commands or anything
You've only generated the controller for pages and created an action called home for it. You have some additional steps needed, as well, to fill out all of the functionality.
There's a "fast" way to create the whole lot: scaffold. Try using this command:
rails generate scaffold pages
Since you've already created your controller, when it asks if you want to overwrite your controller, you can answer 'N' to the question. You can answer 'N' to any other questions that it asks, as well, especially if you've already made changes to any of the files, so that you don't lose those changes.
If you want to go the long way around, you'll also need to create at least some of these:
Model
Migration
Views
To create the Model, you can use another similar command:
rails generate model pages
This will create a page.rb file in your app/models directory. This maps to your database table that stores the details for each page.
You can also generate the Migration to create the pages database table, like so:
rails generate migration CreatePages
You'll need to provide the basic details of the pages table structure, following the Rails migration guidelines, which you can find here.
Finally, you'll need to create the Views for each action. This is primarily what's missing from your current implementation, because generating the controller doesn't generate the corresponding view files.
To do this, you'll want to create a new file called home.html.erb (if it doesn't already exist) in the app/views/pages directory. Simply having this file will be enough to make it past the point that you need, but you'll probably want to put something in it. Let's display the time:
<p>At the tone, it will be: <%= DateTime.now.strftime("%Y-%m-%d %H:%M:%S") %></p>
This should give you a decent starting point to build out the rest of what you need.
When you're starting out, however, try to use the scaffold generator until you're comfortable with where the pieces are all located and how to create them individually. And enjoy!

Ruby on Rails, Routing

Just wanted to know, what does this line mean in the routes.rb file:
AppName::Application.routes.draw do
Please explain. I am new to Rails.
Have a read through this page.
Basically, within the block passed to Application.routes.draw (which is just a call to a method defined in ActionDispatch::Routing module within the Rails core framework), you define all the URLs/Paths that you want your Rails application to respond to.
You can see all these route definitions, by running:
rake routes
in your terminal.
It is the main routes file which defines the root and other paths for the link.
It is used as suppose you want to change your index page from default ruby on rails to your index page you make changes to file and add
root to: "controllername#index"
This file is also used to add the model to the application
resources: "model_name"
Apart from this you can also define links in your rails application
get 'courses/index'
So going from courses controller to view of the index.

Rails controller wrong template location

I have a model, controller and routes configured sucessfully with the name animegif.
In my show method, it looks under application/show instead of views/animegif/show.
Missing template animegif/show, application/show
"searched in /Users/myName/Desktop/testapp/app/views
My easy fix was adding this method to my animegif controller but I do not understand why is it not searching under views/animegif/show by default.
When I followed the rails tutorial by Michael Hartl, the paths were located correctly. Is there something I am doing wrongly?
Name of controller: animegifs_controller, model: animegif
For my routes I am using resources to generate the default routes for the model
def self.controller_path
"animegif"
end
If you read the error message once again you can see it first animegif/show directory of your views. If rails does not find the required template in that directory then it falls back to app/views. You are only getting this error message because either the show.html.erb is not there or you have any typo error.
If you are more curious about how rails finds your views, please refer to this post by Andy Wang. He has explained it in a better way.
Regarding the error message it is looking in app/views/animegif/ and app/views/application/ but cannot find the template. So you probably have a typo in the template's name show.html.erb (or other formats than html) in the respective directory.

Missing Template though routing seems to work

When I generate a new controller, under a subfolder, it now cannot find the templates, even though other controllers in the same 'structure' are working:
I have the following controller which sits in app/members/group_controller.rb (created by a rails g controller Members::Group command)
class Members::GroupController < ApplicationController
def index
render :layout => 'dashboard'
end
end
I have a template in views/members/group/index.html.erb
I have the following relevant line in routes.rb (ie leaving out some others for clarity):
namespace :members do
match '/group' => 'group#index'
end
rake routes shows me the following relevant line:
members_group /members/group(.:format) members/group#index
When I type the url http://127.0.0.1:3000/members/group, I get the Template Missing error as follows:
Template is missing
Missing template members/group/index, application/index with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :arb, :coffee]}. Searched in: * "/Users/mitch/Documents/Development/TME/app/views" * "/Users/mitch/.rvm/gems/ruby-1.9.2-p290/bundler/gems/active_admin-7c3e25f30224/app/views" * "/Users/mitch/.rvm/gems/ruby-1.9.2-p290/gems/kaminari-0.13.0/app/views" * "/Users/mitch/.rvm/gems/ruby-1.9.2-p290/gems/devise-2.0.0/app/views"
The routing is working to the index method, because I can eg put in a redirect and it gets acted upon, but I cannot get the template to display.
Why so?
Thanks
(Rails 3.1)
This seems to be linked to how I generate the controller in the first place.
I used upper case as follows:
rails g controller Members::Group (and tried a few other test controllers similarly, destroying them and recreating them)
When I destroyed the controller and ran the lower case equivelant:
rails g controller members::group all works fine and the templates can be found
I can't find any info elsewhere to support this though...
I observe that you render dashboard layout in groups index page please check path of dashboard .Is it in right place????
I had the exact same problem. When I used terminal to navigate to the directory and listed the files in /layouts, I had one layout file appear as a red, archived file. I have no idea why.
To fix it: simply copy&paste the code from the layout file, delete the layout file (rm "file"), and then create the same layout using the terminal via:
touch file_name.html.erb
Paste your code into the new file and it should work.

Resources