Linking Text Logo and span - ruby-on-rails

how do i link my Logo which is made out of Text and CSS to my root_path and the inside of it?
This is how it would look without Rails:
<h1><a href=#>Main Title<span>subtitle</span></a></h1>
I am using no images and just plain CSS text for the Logo.
Thank you!

If you mean link to root page:
<h1><%= link_to 'Main Title<span>subtitle</span>'.html_safe, '/' %></h1>
If you want other locations and have some resources set up, you could use paths:
<h1><%= link_to 'Main Title<span>subtitle</span>'.html_safe, example_path %></h1>
If you happen to need even more HTML markup in the link, then check this question out:
How do I wrap link_to around some html ruby code?

Related

Linking to a post within Rails site from a Rails blog post produces posts/posts/article-name path

I'm a pretty new developer ~1 year, and I built a blogging site in Rails.
I have a set up where if I click on a post from the home page it goes to www.example.com/posts/article-name.
I decided for my Contact and About Me pages I would just make them blog posts, and then link to them from the nav bar using this syntax in my layouts/_navbar.html.erb file:
<li class="nav-item">
<%= link_to 'About', "posts/about-me", class: "nav-link #{yield(:about_active)}" %>
</li>
When I click on the About link from the Home page it works fine, taking me to www.example.com/posts/about-me
However, if I am inside of a blog article and I click on the About page it takes me to www.example.com/posts/posts/about-me
I'm a little lost on how to fix this. I tried even hard-coding the link but that doesn't seem to work. Any help would be appreciated.
There's something you need to understand about absolute and relative paths.
relative path is what you have tried.
try the absolute path
<%= link_to 'About', "/posts/about-me", class: "nav-link #{yield(:about_active)}" %>

Need help for linking a PDF on page created with rails

I ask myself the question does an pdf tag exist in rails 4 which can show
an pdf file very simple?
This solution answers me that i need to give the correct route informations:
<%= link_to 'Help', public_path%>
I already have an pdf file in my project directory app/assets/public/help.pdf
and now i wanna have a link with the simple name "Help" to have on my page.
Third edit:
After a nice first answer nothing changes and i get the same error message from rails. What i have to write in the routes.rb if i wanna use a link for my pdf file. Does anyone know how can i link an PDF from my own directory!?
In my experience with Rails, I've never seen a PDF link tag helper.
What you want, is to use asset_path in combination with link_to
<%= link_to 'Help', asset_path('data/help.pdf') %>
#=> Help
If it needs to be absolute, you could always use asset_url
<%= link_to 'Help', asset_url('data/help.pdf') %>
#=> Help
What I usually do for linking a document that can be public is I put the document in the public folder in rails and then use this for the path:
link_to 'Help', root_path << 'TheTitleOfYourPDF.pdf'

Rails 4 Different background image for home and items index view

I should like to set two different background images for the home page of my app and the index view for the items.
I tried this directly in the home view:
<div class="container" style="background: url('/assets/images/hom.jpg')">
but it doesn't display any image. Someone can help me please?
I already have tried to follow this post but it still doesn't work.
How to set a different background image for each page in Rails 3 site?
Try it without /assets/ in the URL. The rails assets pipeline does not require you to state that it is in an /assets/ directory
You could achive this by setting the appropiate CSS class and setting the image background in you stylesheets. Something like:
<div class="container home">
And in a SCSS file:
.container.home{
background-image: image-url("hom.jpg");
}
Keep in mind that image-url is a helper that should be used in .scss files.
I tried this in my pages:
<div style="background-image: url('/assets/hom.jpg')">
And it seems to work locally, but after a push on Heroku the images aren't displayed online.
In addition, the image appears cut, because is referred to a DIV, and I want it referred to the body of my pages.
One way to do this is by adding the controller and action names to the body tag in your layout. Then you can rely on pure CSS to serve different images. In your layout file you can do this:
<%= content_tag :body, class: "#{controller_name}-#{action_name}" do %>
<!-- Your layout content, or everything that should be in the body tag goes here -->
<% end %>
This would give you the following HTML:
<body class="items-index> <!-- or whatever the current controller/action are -->
Then, you can add the relevant CSS.
.items-index {
background-image: image-url("items_index.jpg");
}
.pages-home {
background-image: image-url("pages_home.jpg");
}
You can wrap this in a helper to make it cleaner.
def class_for_body
"#{controller_name}-#{action_name}"
end
Or even:
def body_tag(&block)
content_tag :body, yield, class: "#{controller_name}-#{action_name}"
end
See controller_name and action_name methods provided by Rails.

How do I get the url to a path in ruby

I am using <%=link_to "Add fund",addFund_item_path(:id=>item.id),:class=>"addfund btn btn-primary"%>" to generate a link to a path in rails.
I want to get only the url (the href) of the path so I can use it in a form. How do i do that?
Just addFund_item_path(:id=>item.id).
So you just want the actual href? I would do this:
<%= content_tag "a", addFund_item_url %>
It won't be a 'link' per se - it will be clickable, but won't go anywhere - but it will show the full href path

link_to acting strangely in Rails

All my link_to's in my views seem to return the link text, but also the ink address in brackets. Why is this?
E.g
<%= link_to "Home", root_url %>
and renders in the view
Home (http://localhost:3000/)
Check your stylesheets. For example, I know plenty of print media css does that for the benefit of those who can't "click" their paper.

Resources