I am trying to render different CSS depending on the page. I did some research
How does one load a CSS framework in Rails 3.1?
https://github.com/rails/rails/issues/1981
and of course the rails guide on the asset pipeline.
What I want is for the index(home page) to render one css file(welcome.css.scss) and the others to render users.css.scss.
Right now when you navigate to the page it renders the welcome.css.scss and then when I hit sign in it is still rendering welcome.css.scss. But after refreshing the page it will then render the user.css.scss
I updated my assets.rb file:
Rails.application.config.assets.precompile += %w( welcome.css )
Rails.application.config.assets.precompile += %w( users.css )
Then I created a welcome.html.erb
<!DOCTYPE html>
<html>
<head>
<title>Blocmarks</title>
<%= stylesheet_link_tag "welcome" %>
<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
<%= csrf_meta_tags %>
</head>
<body>
<%= yield %>
</body>
</html>
and a users.html.erb file
<!DOCTYPE html>
<html>
<head>
<title>Blocmarks</title>
<%= stylesheet_link_tag 'users' %>
<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
<%= csrf_meta_tags %>
</head>
<body>
<%= yield %>
</body>
</html>
I removed the require from the application.css.scss. The only thing in the file at this time is #import 'bootstrap';
and of course I have a welcome and a user.css.scss.
Results from the console:
-Navigate to the home page
Started GET "/" for 127.0.0.1 at 2014-10-25 13:04:11 -0500
ActiveRecord::SchemaMigration Load (0.3ms) SELECT "schema_migrations".* FROM "schema_migrations"
Processing by WelcomeController#index as HTML
Rendered welcome/index.html.erb within layouts/welcome (13.0ms)
Completed 200 OK in 471ms (Views: 446.7ms | ActiveRecord: 0.0ms)
Navigate to the users/sign-in
Started GET "/users/sign_up" for 127.0.0.1 at 2014-10-25 13:04:57 -0500
Processing by Devise::RegistrationsController#new as HTML
Rendered devise/shared/_links.erb (2.7ms)
Rendered devise/registrations/new.html.erb within layouts/devise (615.4ms)
Completed 200 OK in 896ms (Views: 890.9ms | ActiveRecord: 0.9ms)
Refresh the users/sign page:
Started GET "/users/sign_up" for 127.0.0.1 at 2014-10-25 13:06:02 -0500
Processing by Devise::RegistrationsController#new as HTML
Rendered devise/shared/_links.erb (0.8ms)
Rendered devise/registrations/new.html.erb within layouts/devise (7.4ms)
Completed 200 OK in 308ms (Views: 306.3ms | ActiveRecord: 0.0ms)
I also created a gist with the full console results to show what it is GETting first:
https://gist.github.com/tjperry07/b3093c0b7f16b5e000b0
This is happening because of rails turbolinks. It makes following links in your web application faster. Instead of letting the browser recompile the JavaScript and CSS between each page change, it keeps the current page instance alive and replaces only the body and the title in the head.
If you look at guides or turbolinks docs. It's clearly stated that
If using the turbolinks gem, which is included by default in Rails 4, then include the 'data-turbolinks-track' option which causes turbolinks to check if an asset has been updated and if so loads it into the page
FIX:
Replace your stylesheet tags with these:
<%= stylesheet_link_tag "welcome", media: "all", "data-turbolinks-track" => true %>
<%= stylesheet_link_tag "users", media: "all", "data-turbolinks-track" => true %>
Related
calculate.html.erb of "calc" controller:
<h2>
<%= #count%>
</h2>
Form, which do the get request:
<%= form_with url: "/calc", method: :get do |form|%>
<%= form.label :count, "Введите количество чисел" %>
<%= form.number_field :count %>
<%= form.submit "Начать"%>
<% end %>
routes.rb:
Rails.application.routes.draw do
# Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html
# Defines the root path route ("/")
# root "articles#index"
root 'pages#index'
get '/calc', to: 'calc#calculate'
end
In terminal, when I submit the form, I see this:
Started GET "/calc?count=5&commit=%D0%9D%D0%B0%D1%87%D0%B0%D1%82%D1%8C" for ::1 at 2023-01-23 16:51:04 +0300
Processing by CalcController#calculate as HTML
Parameters: {"count"=>"5", "commit"=>"Начать"}
Rendering text template
Rendered text template (Duration: 0.0ms | Allocations: 4)
Completed 200 OK in 2ms (Views: 0.5ms | Allocations: 206)
calc_controller.rb:
def calculate
render plain: params[:count].inspect
#count = params[:count]
end
P.s The URL doesn't change when I submit form
This line
render plain: params[:count].inspect
in your controller causes Rails to not render your view file but a plain text response.
Just remove that line and Rails should automatically render the view found in app/views/calc/calculate.html.erb
I want each of my view files to have their own layout because each one needs different stuff in their <head> tags with different scripts being run but they should still inherit the same stuff from layouts/application.html.erb. Is this possible?
layouts/application.html.erb -> layouts/a.html.erb -> views/a.index.html.erb
layouts/application.html.erb -> layouts/b.html.erb -> views/b.index.html.erb
content_for :header_tags is for adding extra lines to the <head> of your application.html.erb or base.html.erb. Here's a live example from whatever.html.erb:
<% content_for :header_tags do %>
<%= javascript_include_tag :redmine_helpdesk, :plugin => 'redmine_contacts_helpdesk' %>
<%= stylesheet_link_tag :helpdesk, :plugin => 'redmine_contacts_helpdesk' %>
<% end %>
Then the base.html.erb calls <%= yield :header_tags -%> to express whatever was stored with the :header_tags key.
I was implementing bootstrap into my ruby on rails app and now I've got the following error:
Sass::SyntaxError in Welcome#index
Showing /home/ubuntu/workspace/app/views/layouts/application.html.erb where line #6 raised:
File to import not found or unreadable: bootstrap-sockets.
Load paths:
/home/ubuntu/workspace/app/assets/images
/home/ubuntu/workspace/app/assets/javascripts
/home/ubuntu/workspace/app/assets/stylesheets
/home/ubuntu/workspace/vendor/assets/javascripts
/home/ubuntu/workspace/vendor/assets/stylesheets
/usr/local/rvm/gems/ruby-2.1.5#rails4/gems/turbolinks-2.5.3/lib/assets/javascripts
/usr/local/rvm/gems/ruby-2.1.5#rails4/gems/jquery-rails-3.1.2/vendor/assets/javascripts
/usr/local/rvm/gems/ruby-2.1.5#rails4/gems/coffee-rails-4.0.1/lib/assets/javascripts
/usr/local/rvm/gems/ruby-2.1.5#rails4/gems/bootstrap-sass-3.3.4.1/assets/stylesheets
/usr/local/rvm/gems/ruby-2.1.5#rails4/gems/bootstrap-sass-3.3.4.1/assets/javascripts
/usr/local/rvm/gems/ruby-2.1.5#rails4/gems/bootstrap-sass-3.3.4.1/assets/fonts
/usr/local/rvm/gems/ruby-2.1.5#rails4/gems/bootstrap-sass-3.3.4.1/assets/images
/usr/local/rvm/gems/ruby-2.1.5#rails4/gems/bootstrap-sass-3.3.4.1/assets/stylesheets
(in /home/ubuntu/workspace/app/assets/stylesheets/application.css.scss:17)
Extracted source (around line #6):
3
4
5
6
7
8
9
<head>
<title>Bloccit</title>
<meta name="viewport" content="width=device-width, initial-scale=1"
<%= stylesheet_link_tag "application", media: "all" %>
<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
<%= csrf_meta_tags %>
</head>
Rails.root: /home/ubuntu/workspace
Application Trace | Framework Trace | Full Trace
app/assets/stylesheets/application.css.scss:17
app/views/layouts/application.html.erb:6:in `_app_views_layouts_application_html_erb___973947755503535086_42092360'
Request
Parameters:
None
Toggle session dump
Toggle env dump
Response
Headers:
None
What could be causing it?
It shows that error is in your
app/assets/stylesheets/application.css.scss:17
Please check that properly and let me know.
I'm trying to add a title tag to my website using ROR.
After adding the desired changes to ... in aplication.html.erb, then commiting and deploying it, I still do not see the complete title tag present with the changes I just made when I view the website on search results.
Why is this the case and is there anything I am forgetting to include?
<!DOCTYPE html>
<html>
<head>
<title>Website Name - Desired title tag added here but doesn't show in search results</title>
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
<%= csrf_meta_tags %>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<%= render 'shared/user_info' %>
</head>
<body>
<div id="body-interior">
<%= render 'shared/header' %>
<div class="container">
<% flash.each do |name, msg| %>
<%= content_tag(:div, msg, class: "alert alert-info") %>
<% end %>
<%= yield %>
</div>
<%= render 'shared/footer' %>
</div>
<%= render 'shared/final_scripts' %>
</body>
</html>
Thanks!
When you say search results, I assume you mean search engines like Google? If so, you need to know that changes like these won't be recognized instantly by Google. The changes should be visible as soon as Google (or any other robot) crawls the page in question again.
As far as I know you can force this process with Google's Webmaster Tools.
I'm getting this error after creating a test app on Rails 4.1.5 , currently running on a test enviroment configured with apache and mysql, using passenger, on Fedora 21 so far I´ve tried everything regarding ownership and permissions, but it is still not working.
Permission denied # dir_s_mkdir - /var/www/html/blog/tmp
(in /var/www/html/blog/app/assets/stylesheets/welcome.css.scss)
Extracted source (around line #5):
<html>
<head>
<title>Blog</title>
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-
track' => true %>
<%= javascript_include_tag 'application', 'data-turbolinks-track' =>
true %>
<%= csrf_meta_tags %>
</head>
Rails.root: /var/www/html/blog