I'm trying to display a very simple app from codecademy and this is the error I'm getting in my browser:
PagesController#welcome is missing a template for request formats: text/html
NOTE!
Unless told otherwise, Rails expects an action to render a template with the same name,
contained in a folder named after its controller. If this controller is an API responding with 204 (No Content),
which does not require a template, then this error will occur when trying to access it via browser,
since we expect an HTML template to be rendered for such requests. If that’s the case, carry on.
My directory
pages_controller.rb
class PagesController < ApplicationController
def welcome
end
end
routes.rb
Rails.application.routes.draw do
root ‘pages#welcome’
end
application.html.erb
<!DOCTYPE html>
<html>
<head>
<title>Portfolio</title>
<link href="https://fonts.googleapis.com/css?family=Roboto:100,300,500,700&display=swap" rel="stylesheet">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
<%= csrf_meta_tags %>
<%= csp_meta_tag %>
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
<%= javascript_pack_tag 'application', 'data-turbolinks-track': 'reload' %>
</head>
<body>
<%= yield %>
</body>
</html>
welcome.html.erb
<h1>Hello</h1>
Running 'rails server' and getting the above error in my browser. As far as I can tell the files are in the right place and of the right type ... Rails v 6.0.1
Apparently the issue was that my project was in a folder the title of which had a space in it.
After moving the project to a different folder it now works.
This is on rails 6.0.1
I deleted my controller and view that had the issue and the auto-generated the controller and view with
rails g controller controllername new create
Related
I used following specifications.
1) rails 5.1.3
2) ruby 2.4.1
3) rail with webpacker
4) Angular 4.3.4 with webpacker (Typescript)
I create new rail app using command rails new my-rails-app --webpack-angular.
Folder structure
After that, as per instruction under app/javascript/packs/application.js and app/javascript/packs/hello_angular.js, I added <%= javascript_pack_tag 'application' %> in application.html.erb to head tag next after javascript_include_tag.
Also add below markup to body tag in layout application.html.erb
// <hello-angular>Loading...</hello-angular>
//
// <%= javascript_pack_tag 'hello_angular' %>
application.html.erb
<!DOCTYPE html>
<html>
<head>
<title>AngularOnRailsWithWebpack</title>
<%= csrf_meta_tags %>
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
<%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>
<%= javascript_pack_tag 'application' %>
</head>
<body>
<hello-angular>Loading...</hello-angular>
<%= javascript_pack_tag 'hello_angular' %>
<%= yield %>
</body>
</html>
Now create new scaffolding by running rails g scafolld post title:string desc:text and set post index page as root path
Now run rails server by running rails s and webpack server by running ./bin/webpack-dev-server
Works fine.
But When I navigate routes by click on new post or edit post it fire errors in web console like below.
Solution I tried
1) Remove javascript_include_tag from application.html.erb. But it will remove turbolink. I don't want to remove that.
2) Restore javascript_include_tag and update index.ts under app/javascript/hello-angular/index.ts. Add turbolink load event as per this video instruction https://www.youtube.com/watch?v=lGToT9RyDxc
Index.ts
import './polyfills.ts';
import { platformBrowserDynamic } from '#angular/platform-browser-dynamic';
import { AppModule } from './app/app.module';
document.addEventListener('turbolinks:load', () => {
platformBrowserDynamic().bootstrapModule(AppModule);
})
Work fine. It will render code fine but still same zone error in web console.
May be this is rails issue. If so then I hope it will resolve in next patch release.
Can any one help that how to resolve this issue ? What I am doing wrong.
I felt confused about the Rails layout. I have the home and video page, and I want to include their css and js relatively.
Therefore, after I used scaffold to create video, I created video.css and video.js.
Furthermore, I created a file in view/layouts/video_layout.html.erb and put the following code into it.
<!DOCTYPE html>
<html>
<head>
<title>Video</title>
<%= csrf_meta_tags %>
<%= stylesheet_link_tag 'video', media: 'all', 'data-turbolinks-track': 'reload' %>
<%= javascript_include_tag 'video', 'data-turbolinks-track': 'reload' %>
</head>
<body>
VIDEO BEGIN
<%= yield %>
VIDEO END
</body>
</html>
I thought it will include only video.css and video.js. However, when I accessed localhost:3000/videos, the video page was still in original condition (no VIDEO BEGIN and VIDEO END )
add the following line to app/controllers/videos_controller.rb
layout "video_layout"
eg
class VideosController < ApplicationController
layout "video_layout"
def index
end
....
end
also check this http://api.rubyonrails.org/classes/ActionView/Layouts.html
I follow the Ruby on Rails tutorial by Michael Hartl and I'm stuck with an example that is not working for me.
I try to remove duplicate code from .erb files so that the code exists only in the application.html.erb file. With the old home.html.erb file everything works out well (when I do a GET for home the content is shown), but with the one I am supposed to use to eliminate duplicate code, no content is shown. After testing, I found out that even removing the title tag from the old file is enough to make the content dissapear.
Any ideas why that is happening? Is the tutorial wrong or did I miss something?
application.html.erb:
<!DOCTYPE html>
<html>
<head>
<title>Title | <%= yield(:title) %></title>
<title>
<%= stylesheet_link_tag "application", :media => "all" %>
<%= javascript_include_tag "application" %>
<%= csrf_meta_tags %>
</head>
<body>
<%= yield %>
</body>
</html>
home.html.erb
<% provide(:title, 'Home') %>
<h1>Sample App</h1>
<p>
This is the home page for the
Ruby on Rails Tutorial
sample application.
</p>
old home.html.erb:
<% provide(:title, 'Home') %>
<!DOCTYPE html>
<html>
<head>
<title>Title | <%= yield(:title) %></title>
</head>
<body>
<h1>Sample App</h1>
<p>
This is the home page for the
Ruby on Rails Tutorial
sample application.
</p>
</body>
</html>
The problem with your layout file is that you have two <title> tags, one containing the title of your application, and one left open. If you remove the one left open, the problem will be resolved.
You should fill in the application layout in the controller. In other words, add layout 'application' to your controller. For example:
class StaticPagesController < ApplicationController
layout 'application'
def home
end
def help
end
def about
end
end
My only line in the routes file is this:
root :to => 'spikes#index'
In the Javascript assets I have a simple function like this:
spike.js file:
$(function(){
alert("WAT");
document.write("abc");
});
In the Views->spikes->index.html.haml file I have NOTHING! it is empty.
I used to have the following code, but I removed it:
= javascript_include_tag 'spike'
But still when I run my Rails app, I see an alert and "abc" written on the browser.
Where is it calling it from? What on earth!?!
UPDATE: This is also my application.html.erb file:
<!DOCTYPE html>
<html>
<head>
<title>D3Spike</title>
<%= stylesheet_link_tag "application", :media => "all" %>
<%= javascript_include_tag "application" %>
<%= csrf_meta_tags %>
</head>
<body>
<%= yield %>
</body>
</html>
You probably have a <script> tag in app/views/layouts/application.html.erb (or maybe .haml, not .erb), which wraps all your pages. If you take that out for a second, you'll probably see the alert go away.
A couple things to understand: 1) the JavaScript code you showed us runs just by virtue of being included (paste it into a console in your browser to see what I mean) and 2) in Rails, the normal behavior is for all your JavaScript to be included all the time, which is perhaps not what you expected.
I'm following the Ruby of Rails getting started guide, and I see this code in the layout file:
<!DOCTYPE html>
<html>
<head>
<title>Blog</title>
<%= stylesheet_link_tag "application" %>
<%= javascript_include_tag "application" %>
<%= csrf_meta_tags %>
</head>
<body style="background: #EEEEEE;">
<%= yield %>
</body>
</html>
Coming from an MVC3 background, is this the equivalent to the RenderContent() method one would invoke from the _layout.cshtml file?
The functionality is about the same in that context, yes. However, yield in general is a keyword in the ruby language, concerning blocks. You can find more information here: ruby blocks.
Building on that, you are able to provide content for different parts, using content_for(:something) and yield :something (the yield passes :something to the layout engine, the layout engine fills in the content for it).