CSS only shows correctly on index page. Ruby On Rails - html

I am new to Ruby On Rails and am trying to get a css template to work for a project. It works fine on my index page but on every other page it doesn't seem to be getting the complete layout from the application.html.haml file.
If I use the link to include the css in the page it will only add font changes and color changes, but none of the actual layout changes ie. tables, formatting and such.
= stylesheet_link_tag 'style'
I'm not to sure where to troubleshoot now since it's only grabbing half my styles from the style sheet.
!!! 5
%html
%head
%title HELPDESK
= csrf_meta_tags
%link{:href => "style.css", :rel => "stylesheet", :type => "text/css"}/
%body
%h1.title HELPDESK
#main
- if flash[:notice]
#notice.message= flash[:notice]
- elsif flash[:warning]
#warning.message= flash[:warning]
= yield
If it's any help, I can rename my other pages to index.html.haml and the css loads perfectly fine, so the pages are written correctly I just have an error with the way Rails loads stylesheets or an error with a route.
tl:dr everything in my Rails app after site.com/questions/ does not load with the correct stylesheet, while site.com/questions loads perfectly fine.
Thanks!
Alright, for anyone that reads this later, I fixed the problem. I needed to make the link in the application.html.haml file (listed above) point to ../style.css instead of style.css

I fixed the problem. I needed to make the link in the application.html.haml file (listed above) point to ../style.css instead of style.css

Related

Different application layout for my rails application

I have a home page with its own style (put in application.css) but the rest of the website will be totally different.
Yet, it seems that Rails work with a "parent" file application.html.erb and we can basically use yield to include other .html.erb pages.
However, it's not viable for my application because as I said the home page will have a totally different structure from the rest of my web app and I can't use a common application.html.erb file.
What are the solutions for this situation?
You can specify the layout controller wide or for a specific action.
The layout file needs to be in the layouts folder.
To specify the layout file controller wide you can do this
class BlogController < ActionController::Base
layout "unique_blog_layout"
def index
end
...
end
To set it for a single view
class BlogController < ActionController::Base
def index
render layout: "unique_blog_layout"
end
...
end
For a full rundown checkout the docs here and maybe here too.
To use a different layout for just a single template, do this:
Add your new layout to views/layouts/name_of_my_new_layout.html.erb
Use render :index, layout: :name_of_my_new_layout in the controller to tell Rails to use your new layout to render the template

Creating dynamic links for a HTML email with embedded ruby

I have a simple question about links in HTML emails
I am trying to add a simple dynamic link in an HTML email to my site like this
MySite
This fails, but this works
MySite
I am assuming its to do with the embedded ruby but strangely, the links work if I view them on my iphone.
Any thoughts on this?
Stupid me....
I needed to construct the link using the ActionMailer generating URLs instructions, see here http://guides.rubyonrails.org/action_mailer_basics.html#generating-urls-in-action-mailer-views
My URL is
<%= link_to "View response", notification_micropost_response_url(#micropost, #response) %>
The default host must be first set in the dev and prod environment config files:
Example (development.rb)
config.action_mailer.default_url_options = { :host => 'localhost:3000' }

Specify a style sheet in Ruby on Rails

I have Bootstrap gem install on my app however I want to include a special style sheet for my login page. How would I include this style sheet without making the stylesheet global for all views (This is what is happening to me.)
1) In the controller method that renders your login page, add the following line at the end of the method:
render :layout => false
That code will prevent the application.html.erb layout from being applied to the login page, assuming you want a 100% custom layout and stylesheet. If you're happy with the global layout being applied (the structure of the page, including any header or footer partials, etc.), then ignore this step.
2) In your login.html.erb (or whatever file contains your view), you'll need the following line, to point at your specific css file:
<%= stylesheet_link_tag 'foo', media: 'print, screen' %>
where foo points to a foo.css file contained in your app/assets/stylesheets directory. You can skip the media bit if you don't want to differentiate between stylesheet media versions, but you may run into an issue with an unstyled view if the user ever tries to print the page, or if you're using a responsive layout.
3) Before that stylesheet link tag will work, you'll have to tell Rails to precompile it. In your_app/config/initializers/assets.rb, add the following line:
Rails.application.config.assets.precompile += %w( foo.css )
4) Restart your Rails application.
5) Create and write your foo.css file.
You should see your view specific css being applied. Also... I herd u liek mudkipz.

Link to home page renders blank

In my rails app, I have a link to the root that renders in all browsers as
<html>
<head>
<style type="text/css"></style>
</head>
<body>19</body>
</html>
Server-side I can see all assets getting passed back. For some reason the browser will not display them because if I view page source I can see ALL my markup correctly.
Now if I load or reload the page localhost:3000 normally the page and all assets are displayed correctly. If I simply refresh I get nothing.
The only time this occurs is when I click a link back to the homepage like a href="/".
The behavior does not occur if I hardcode a href="localhost:3000/" but I don't want to do this.
Rails app, Turbolinks, nowhere in code do I use the number 19, rake routes with root GET well-defined, no public/index.html. I suspect something to the degree of caching or turbolinks but I have no clue how to resolve.
Edit: Config/Routes.rb
SCRR::Application.routes.draw do
root 'home#index'
#pages
get 'about' => 'pages#about'
get 'history' => 'pages#history'
get 'links' => 'pages#links'
get 'safety' => 'pages#safety'
get 'membership' => 'pages#membership'
get 'events' => 'events#index'
get 'grand_prix' => 'grand_prix#index'
get 'newsletters' => 'newsletters#index'
end
Edit 2: Chrome Devtools Console Errors
This error is probably more relevant
Resource interpreted as Font but transferred with MIME type application/x-woff: "https://netdna.bootstrapcdn.com/font-awesome/2.0/font//fontawesome-webfont.woff".
jquery-2.1.0.js?body=1:1078
25115 : CS -> BG : FAILED closepopuptoplevel
onloadwff.js:77
From what you've posted, I don't know whether this would be a Rails issue or a browser issue.
I see you've tagged turbolinks, which could be a contributor to the issue; so let's have a look as to what the problem might be for you:
ERB
The ERB code you should use should be the following:
<%= link_to "Home", root_path %>
If you're trying to reference the "home" url directly, there may be an issue with how you're rendering the URL, hence why it won't show. You should ensure you use the path helper as described above.
This should be coupled with the correct routes:
#config/routes.rb
root "home#index"
resources :pages, path: "" do
collection do
get :about
get :history
get :links
get :safety
get :membership
get :events
get :grand_prix
get :newsletters
end
end
Assets
You mention the assets don't render when you refresh the page, this could be an issue with Turbolinks, although I'm not sure
Turbolinks basically just reloads the <body> of the page, leaving the <head> intact. Although this might sound like nothing to do with your issue, perhaps the problem is to do with the way in which Turbolinks is working with your assets
From your edit, it may appear that your assets are not loading correctly, or are at least not being shown correctly.
I hope this helps, I doubt it will give you a constructive answer

Rails default link when using hash sign

I'm working on a Rails project on the file located at myserver/myNewProject/here.html, and when I have an HTML link like
...
it jumps back to my old project at myserver/myOldProject#test. This is not good, since the old project has nothing to do with the new project.
This is probably because I copied over some code from my old project, but I don't know which part is causing this. How can I correct this behavior?
Use the :anchor attribute in the link_to:
<%= link_to('some link', posts_path(:anchor => 'test'))
Will generate a link like:
some link
See the docs for all the options link_to takes:
http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html