Rails 3.1 button Icon with css - ruby-on-rails

How can I add an icon to a rails button with css?
My request is both for <% button_to %> and <% f.submit %>
Thanks for helping
Hello #jdc I've finally found the trick, I did not use your method, but it was helpul to find my way.Thanks a lot man.So thi is How i proceed :
1°) Change the html.erb code from :
<%= f.submit %>
to
<%= f.submit , :id => 'button' do -%>
<% end %>
2°) In the app/assets/stylesheets and put the following css code:
#button {
background-image: url(plus_8x8.png);
background-repeat:no-repeat;
display:block;
float:left;
margin:0 7px 0 0;
background-color:#f5f5f5;
border:1px solid #dedede;
border-top:1px solid #eee;
border-left:1px solid #eee;
font-family:"Lucida Grande", Tahoma, Arial, Verdana, sans-serif;
font-size:12px;
line-height:130%;
text-decoration:none;
font-weight:bold;
color:#565656;
cursor:pointer;
padding:5px 10px 6px 7px;; /* I put this here to move the text away on older IE junk */
}
Of course #button at the top of the css code is the :id name in <%= f.submit , :id => 'button' do -%> code in the html.erb file
I hope this will help you guys to style css button on rails 3 applications.

If I understand what you're asking, you could try something like this:
<% f.submit :html => { :class => 'imgbutton' } %>
then in your css:
background: url(someicon.gif);
border: none; /* If your icon IS the button */
color: transparent; /* If you don't want the button text to show up */
*padding-left: 9999px; /* I put this here to move the text away on older IE junk */
Might not work for some really ancient browsers to do it with CSS though.

Related

Rails/Bootstrap. Using .each falling to have a new Thumbnail next to the one before horizontally

Every cycle I get the new Thumbnail BUT under the previous one. Any suggestions how to solve that? How in each cycle add the new Thumbnail horizontally next to the previous one, until the space is fill and then move to the next line? Thanks.
From TO Picture
CODE
<% #employees.each do |em| %>
<div class="listTumbnail">
<% if em.user.imagepath %>
<div class = "user-image">
<img src="<%= em.user.imagepath%>" class="listimg">
</div>
<% else %>
<div>
<%= image_tag("icons/desconocido.jpg", :alt => "not found", :class => "listimg") %>
</div>
<% end %>
</div>
<% end %>
.user-image {
float: none;
padding-top: 127px;
margin-left: 193px;
padding-left: 6px;
border-left-style: solid;
border-left-color: #000;
border-left-width: 1px;
}
.listimg {
display: block;
max-width:80px;
max-height:100px;
width: auto;
height: auto;
margin: 0 auto;
border-radius: 40px;
}
.listTumbnail {
border: 2px solid #95989A;
border-radius: 3px;
color: #000;
height: 140px;
width: 110px;
margin: 5px;
}
try adding this to your stylesheet:
.listThumbnail {
display: block;
}
.user-image {
display: inline-block;
}
If for some reason you don't have a css file in your project, here's one basic way to add one:
Create a file named my_styles.css (or whatever you want to name it) & add the above css examples there.
Add the following link in the <head> of your application.html.erb file:
<link rel="stylesheet" type="text/css" href="my_styles.css">
This allows your html file to work with your css file/stylesheet.

Targeting dynamic class names in css/scss or JavaScript

In my rails application I have two models, Users and Goals. A User can have Many Goals, and a Goal belongs to a User. I have a page that lists all of a users Goals in the users show page. This is done using a .each loop, and in this loop I have created a dynamic class name that puts the goal_id into the class name.
I want to be able to target these individual classes in my css/scss folder so that I can, for example, have only the first goal be highlighted when I hover over it.
app/views/users/show.html.erb
<div class="user-goals-mouse-events">
<% #user.goals.each do |goal| %>
<%= link_to goal_path(goal), class: "user-goals-hover-#{goal.id}" do %>
<div class="row">
<div class="col-md-4">
<h2 class="color-light-blue"><%= goal.name %></h2>
</div>
<div class="col-md-8 user-goals-description">
<h4><%= goal.description %></h4>
</div>
</div>
<h6 class="hide-username-<%= goal.id %>"><%= #user.username %></h6>
<% end %>
<% end %>
The Scss is not working currently but it may help show what i'm attempting to do.
app/assets/stylesheets/custom.css.scss
.user-goals-mouse-events > a {
border-bottom-style: solid;
border-bottom-color: black;
border-bottom-width: 1px;
//background-color: black;
&:hover {
background-color: #26AEFF;
color: gold;
}
}
.user-goals-mouse-events > div > h6 {
opacity: 0;
}
.color-light-blue {
color: #26AEFF;
&:hover {
color: white;
}
}
If a better solution to this problem is done a different way (e.g. with javascript) then please let me know as I want to do it the most efficient way.
Try to use the attribute-value-begins-with selector:
a[class^=user-goals-hover] {
border-bottom-style: solid;
border-bottom-color: black;
border-bottom-width: 1px;
//background-color: black;
&:hover {
background-color: #26AEFF;
color: gold;
}
}
#hashrocket answer is good, but I'd advise to be more explicit on your classes. It is a code smell when you need a class both for styling and for targeting.
Yes, there is such a thing as code smell in CSS too! :)
I would have a constant class for styling and a dynamic one or even a completely different attribute for targeting:
<%= link_to goal_path(goal), class: "user-goals", data: {goal_id: goal.id} do %>
which would render as
<a href="/goals/42" class="user-goals" data-goal-id="42">
and the css would just be
.user-goals {
//...
&:hover {
//...
}
}

size option in gravatar_for helper, ruby on rails, not working

No matter what I put for the size option, the size remains the default 80 x 80. Any ideas why this might be?
<%= link_to gravatar_for(micropost.user, options = { size: 100 }), micropost.user %>
I thought there might be something in CSS that's overriding the parameter, but couldn't find anything:
.gravatar {
float: left;
margin-right: 10px;
}
.gravatar {
float: left;
margin-right: 10px;
}
img {
display: block;
padding: 5px 0;
}
I'd go about another aproach here if it is the https://github.com/mdeering/gravatar_image_tag gem you're using.
<%= link_to micropost.user do %>
<%= gravatar_image_tag('micropost.user', :gravatar => { :size => 15 }) %>
<% end %>
Try this:
<%= link_to gravatar_for(micropost.user, micropost.user), size: 100 %>
For more information on the usage of link_to helper method see RoR API here:
http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to
A snipet of your HTML code from browser's console would also be helpful here to help you better.

Rails link_to tag tag with styled glyphicon

I am trying to use glyph icons that I have styled in my rails app.
I want to make the icon a link to another page, but can't use the standard bootstrap styling because I have customised the links for my page's style.
I keep getting a keyword class error - does anyone know why?
Thank you.
I have the following link in my view:
<li><%= link_to <span class="glyphicon glyphicon-comment"></span>, page_path('messages') %> </li>
I also have css as follows:
span.glyphicon-comment {
vertical-align:middle;
margin:auto;
padding:10px;
font-size: 2em;
text-align: right;
a:link {text-decoration:none; background-color:transparent; color:grey;};
a:visited {text-decoration:none;background-color:transparent; color:grey;};
a:hover {text-decoration:none;background-color:transparent; color:dark grey;};
a:active {text-decoration:none;background-color:transparent; color:grey;};
}
You have to use " around the string, also need to call html_safe method on it.
<%= link_to "<span class=\"glyphicon glyphicon-comment\"></span>".html_safe, page_path('messages') %>
but a better and cleaner way would be
<%= link_to page_path('messages') do %>
<span class="glyphicon glyphicon-comment"></span>
<% end %>
Your problem is that you are using a:visited {text-decoration:none; background-color:transparent; color:grey;}; on your glyphicon css but they should be on your link
Try this:
<%= link_to page_path('messages'), class: "comment_link" do %>
<span class="glyphicon glyphicon-comment"></span>
<% end %>
and then style your link, if you are using scss then:
.comment_link{
.glyphicon-comment{
vertical-align:middle;
margin:auto;
padding:10px;
font-size: 2em;
text-align: right;
}
&:hover, &:visited, &:active{
text-decoration:none;
background-color:transparent;
color:dark grey;
}
}

Css class is not applying on button

I have a button below:
<table align="center" width="50%">
<tr>
<td style="color: #212121;">
<div class="button">
<%= button_to "Search", {:class => "buttonhome" } %>
</div>
</td>
</tr>
</table>
I am applying buttonhome class i.e below
.buttonhome
{
height: 100px;
width: 200px;
font-family: Arial;
background-color: transparent;
border-style: none;
font: white;
}
But its not applying on the button. Kindly suggest me. Waiting for reply. Thanks
<%= button_to "Search", {action: "search"}, {class: 'buttonhome'} %>
possible duplicate 1 2
Try
div.button .buttonhome
{
height: 100px;
width: 200px;
font-family: Arial;
background-color: transparent;
border-style: none;
font: white;
}
also check if your css is loading fine and your button has the class buttonhome.
The html_option is 3rd parameter of button_to according to documentation
http://apidock.com/rails/ActionView/Helpers/UrlHelper/button_to
so use this
<%= button_to 'Search', {}, { :class => "buttonhome" } %>

Resources