RoR tutorial (Michael Hartl) URI::InvalidURIError - ruby-on-rails

I am following the RoR tutorial by Michael Hartl. Currently on chapter 3. My problem is that I don't get the view I see at https://www.railstutorial.org/book/static_pages#fig-raw_home_view.
Instead I'm seeing a
The URL I'm trying to enter is: "localhost:3000/static_pages/help".
The application.html.erb looks like this:
<!DOCTYPE html>
<html>
<head>
<title>SampleApp</title>
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
<%= csrf_meta_tags %>
</head>
<body>
<%= yield %>
</body>
</html>
I'm still a rookie so really don't know what to do.

You have a problem with your app's name. To the right of Rails.root in your image, you can see that your app's name is:
sample_app]
Rails.root is the path to your application's directory, and it's used as the base path for locating all the files in your project.
The Rails.root is used in the url marked bad URI. The url is illegal because urls can only contain certain characters, and a bracket isn't one of them.
Your web browser is trying to request a css file that is specified in the application layout, and rails converts the request url to a local file url using the Rails.root. Unfortunately, Rails doesn't even bother looking for the file--instead Rails stops when it sees an illegal character in the url and throws an error.

Related

what is causing Less::Error 'Unrecognised input'?

I'm following this answer about using yarn to set up semantic-ui with rails but get this vague error that says only Unrecognised input whenever I access localhost. The stack trace is short and the only clue it provides is to the stylesheet and javascript link tags in my application.html.erb head:
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
What does Unrecognised input mean, and what causes it?
who would win? many lines of code or one missing semicolon?
make sure you end your variable declarations with a semicolon.

JSON Parser Error Unexpected Token Error Rails

I recently cloned my Rails app, which was developed on Linux, to my Windows OS. There were a few errors, and I managed to fix most of them except this one. This error pops up when I try to access my page locally:
JSON::ParserError
419: unexpected token at ''
This is the code in question:
<head>
<title>DemoApp</title>
<%= stylesheet_link_tag "application", media: "all", "data-turbolinks-track" => true %>
<%= javascript_include_tag "application", "data-turbolinks-track" => true %>
<%= javascript_include_tag "ckeditor/ckeditor.js" %>
<%= csrf_meta_tags %>
I researched and couldn't find a straightforward solution to this. I had previously changed my runtimes.rb to 'UTF-8' so that it will run in Windows.
Any help is much appreciated.
It could be because of a BOM or a tabulation space, it happens to me when I try to paste inside Vim.

Rails 5: CSS will not link to html

I'm new to rails and trying to link main.css.scss to index.html.erb file in views/main.
My css file is located in app/assets/stylesheets. This is default, I did not change anything.
So in my index.html.erb file I added the following:
<%= stylesheet_link_tag "main" %>
This throwed an error which advised me to include ".css" at the end of "main". I added ".css" and ".css.scss" to the end of main. The main page loads, but without the css file.
What is the correct way to link css in rails 5?
Read the api docs here. It will help you to understand how you can link css in rails.
Also take a look at Samuel's answer. He explained it really well.
It's very simple. If you don't want to use default application.css and application.js, then create new files, like in your case main:
app/assets/stylesheets/main.scss
app/assets/javascripts/main.js
Then in your layout include those files:
<!DOCTYPE html>
<html>
<head>
<title>Foobar</title>
<%= stylesheet_link_tag 'main', media: 'all', 'data-turbolinks-track' => true %>
<%= javascript_include_tag 'main', 'data-turbolinks-track' => true %>
<%= csrf_meta_tags %>
</head>
And add those files to assets.rb:
Rails.application.config.assets.precompile += %w( main.scss main.js)
And restart the server.

Bootstrap Gem Installation Issue

I have added the bootstrap-sass gem and have installed the bundle. I have gone to github and think I have followed the directions correctly to finish the install for ruby on rails. It doesn't appear to be making a difference however on my local site - no font/formatting changes have been made, even after the install.
I am using a PC, https://github.com/findingtheway/pin
Change your app/views/layouts/application.html.erb to this:
<head>
<title>Pin</title>
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
<%= csrf_meta_tags %>
</head>
Rails uses application by default you have default which is why bootstrap is not loading.

Why is this COMMENTED piece of code, <%= stylesheet_link_tag "application", :media => "all" %>, not allowing my ROR app to run

I'm in the process of getting Twitter-rails-bootstrap GEM up and running on my Windows OS.
The load error is below:
cannot load such file -- less
(in C:/Sites/cardMS/app/assets/stylesheets/bootstrap_and_overrides.css.less)
Thanks
To comment some rails code you need append hash although it is in html comment block.
So change
<%= stylesheet_link_tag “application”, :media => “all” %>
to
<%#= stylesheet_link_tag “application”, :media => “all” %>
I just tested Nitin's answer in a sample app and looked it up. Indeed all you need to do to comment any erb is place a hash sign after the opening '<%' tag. I suppose the erb parser doesn't care about normal ruby comments as those too can have dynamic parts. This indeed happens when you generate a scaffold and the commented route "#get /posts/1" shows the correct resource. Rails generators do support this feature with templates.
<%= not commented blah %>
becomes
<%#= commented blah %>

Resources