How can add an underline using ruby on rails and css? - css

I'm working with ruby on rails and I"m not very familiar with the language. What I want to do is fairly straight forward. I have a language toggle bar which allows the user to choose the language with which they can view the site.
I want to underline the selected language when it is chosen. But I'm not sure how to add a class or a span so that I can just underline (bottom-border) one of the languages.
Here is my code:
<div class="language">
<% if #language == :fr %>
<%= link_to 'English', language_path(language: :en) %>
<span>|</span>
French
<% else %>
English |
<%= link_to 'French', language_path(language: :fr) %>
<% end %>
</div>
Can someone please help me? When I select the class 'Language' it underlines both the "English|French" entirely, when all I really want is to underline the language that is currently selected. So either or, not both.

Put the active language into its own span and style it there
<div class="language">
<% if #language == :fr %>
<%= link_to 'English', language_path(language: :en) %>
<span>|</span>
<span class="selected">French</span>
<% else %>
<span class="selected">English</span>
<span>|</span>
<%= link_to 'French', language_path(language: :fr) %>
<% end %>
</div>
CSS:
.language > .selected {
text-decoration: underline;
}
You might also need to style your generated anchor tags to have text-decoration: none;, since they are probably underlined by default (as is good practice anyhow). In any case, don't use border-bottom for this.
Having said all that, I think you need to make it as clear as possible that the currently unselected language is an anchor tag. When people see underline, they tend to assume the underlined thing is a link. Conversely, things with no underline that ARE links constitute surprising behavior.

Related

Why is my text is extending the width of the div? (Not due to word-wrap or overflow-wrap attributes)

I am trying to load notifications in my app into a dropdown from my navbar (which is a bootstrap navbar). The partials that I am working with for each notification are similar to this one:
<%= link_to student_path(notification.notifiable), class: "dropdown-item notification-row #{"unread" if !notification.read_at?}" do %>
<%= image_tag notification.faculty.photo, class: "avatar", alt: "faculty-pic" %>
<div class="notification-message">
<%= notification.faculty.teacher_name %>
<%= notification.action %>
<%= notification.notifiable.first_name %>
<%= notification.notifiable.last_name %>
to you
</div>
<div class="notification-time"><%= time_ago_in_words(notification.created_at) %></div>
<% end %>
I am specifically attempting to keep the "notification-message" class at a width of 200 px as displayed in my css:
.notification-message {
width: 200px;
}
but my partials do not render accordingly...
I want the "notification-message" content to wrap within that width so that my notifications are consistent. I have tried adding both the word-wrap, and overflow-wrap attributes to my css, but they do not help (my partial is rendered just the same). Is this happening because of some sort of bootstrap style? Any other ideas??

How do I add rails form code to bootstrap css

I'm trying to style a simple Ruby on rails form with the Twitter Bootstrap css but I'm struggling to workout which bits of rails code goes into which part of the Bootstrap css for a form.
For example where would the following line fit into the Bootstrap css for a form? :
<%= f.text_field :company %>
Based on the examples in the Twitter Bootstrap documentation, I am assuming you are attempting to apply the form-control class to your input. If this is the case, you would do it like this.
<%= f.text_field :company, :class => "form-control" %>
See similar question: Using CSS to style Ruby objects in erb
<%= f.text_field :company %>
Actually, this code means "Create a markup input of type text, with the good name (ex: user[company]) so Rails can handle the parameter easily". No more no less.
Example Output:
<input type="text" name="user[company]">
Bootstrap will envelope then this object with a style provided by CSS. So
1- Ensure your css is loaded into your "application.css"
Usually, you'll need to add style for the form
<%= form_for #resource, html: { class: "form form-inlined" } do |f| %>
And also around your input text field:
<div class="form-group">
<%= f.label :company %>
<%= f.text_field :company, class: "form-input" %>
</div>
This is just an example, but you can see where to put the bootstrap class on each elements.

Ruby field_with_errors doesn't #extend .control-group .error

Ok. I can't figure this out. The problem is that #extend is not working in css. I have css:
#import "bootstrap";
.field_with_errors {
#extend .control-group;
#extend .error;
}
It doesn't highlight the fields that have div class .field_with_errors. I can't figure out why, it worked on other apps I made. If I write in CSS something like color: #f00; - this works. It just doesn't #extend for some reason. Any ideas?
Form:
<h1>Report</h1>
<div class="row">
<div class="span6 offset3">
<%= form_for(#problem) do |f| %>
<%= render 'shared/error_messages' %>
<%= f.label :name, raw("Your name:") %>
<%= f.text_field :name %>
<%= f.label :email, raw("E-mail address (for confirmation):") %>
<%= f.text_field :email %>
<%= f.label :description, raw("Enter a description of the problem:") %>
<%= f.text_area :description %>
<%= f.submit "Submit", class: "btn btn-large btn-primary" %>
<% end %>
</div>
</div>
Probably a dumb question, I must've missed something. I just don't know what it is and would really like this to work like it did before. Any help appreciated!
Edit:
After looking at bootstrap-sass files, I realized that I am able to #extend classes that are in the files there (#extend .form-control works for instance). So it must be that .error and .control-group is not there!! Where it went I still can't figure out, unless they just changed it like a week ago. :/
As the comment of 'soup' points out, the problem is caused by installing Bootstrap 3 and then calling Bootstrap 2 class names. The class names have changed (have a look here). The correct code is:
.field_with_errors {
#extend .has-error;
}
And accordingly the error message has to be wrapped in (also look here):
div class="alert alert-danger"
So in your app\views\shared\_error_messages.html.erb-partial (you're propably reusing code from Michael Hartl's Rails Tutorial) change div class="alert alert-error" to div class="alert alert-danger". Because your code is so far off, I give you a version adapted for Bootstrap 3:
<h1>Report</h1>
<div class="row">
<div class="col-md-6 col-md-offset-3">
<%= form_for(#problem) do |f| %>
<%= render 'shared/error_messages' %>
<div class="form-group">
<%= f.label :name, raw("Your name:") %>
<%= f.text_field :name, class: 'form-control' %>
</div>
<div class="form-group">
<%= f.label :email, raw("E-mail address (for confirmation):") %>
<%= f.text_field :email, class: 'form-control' %>
</div>
<div class="form-group">
<%= f.label :description, raw("Enter a description of the problem:") %>
<%= f.text_area :description, class: 'form-control' %>
</div>
<%= f.submit "Submit", class: "btn btn-lg btn-primary" %>
<% end %>
</div>
</div>
In case anyone lands on this (like I did) because it isn't working with LESS, here's what I had to do:
.field_with_errors .form-control {
&:extend(.has-error .form-control);
}
.field_with_errors .form-control:focus {
&:extend(.has-error .form-control:focus);
}
When I tried it with:
.field_with_errors {
&:extend(.has-error);
}
which is the equivalent of the recommended way I found to use the bootstrap 3 style for errors, LESS wasn't doing anything. It may have something to do with the fact that the selectors in bootstrap 3 that mention has-error always have it combined with something else, e.g.:
.has-error .form-control {
Anyway - the above solution worked for me so hopefully it helps someone else.
I figured it out. This has to do with Twitter-Bootstrap, which has a class .alert in it. That is the class responsible for showing errors with correct styling.
Bootstrap installs who knows where and is easiest to access through Developer Tools in browser (that sucks). I couldn't find where the source of the gem is located (it pointed me to folder that is not a folder). So I copied twitter css files from github directly into my app -> vendor/assets/stylesheets . Now I can deal with the problem.
I changed the style of .alert class inside one of bootstrap files (_alerts.scss) and added extra classes in my custom.css.scss file to make border around inputs red on errors.
Somehow this whole thing worked better in previous apps I made, I think this has to do with changes they made to bootstrap and possibly the fact that I messed with bootstrap's original design (like a full-width header).
The biggest thing that led me to solution was that the class is .alert and not .error like I thought. Also big thanks to browser developer tools, I couldn't find my files without you.
Edit:
Here's the download instructions on Bootstrap to get the files:
http://getbootstrap.com/getting-started/#download
Upgrading your code as per 3.x should work
.field_with_errors {
#extend .has-error;
}
This error causes because in application.scss there is the folowing line
*= require_tree .
and it is placed before the #import "bootstrap".
So, _custom.scss has been included before Bootstrap.
The easiest way to fix it -- what worked for me -- is to add !optional to both classes:
.field_with_errors {
#extend .control-group !optional;
#extend .error !optional;
}
OK, after chasing down this issue for a couple days, I figured out what the problem is.
You need to include bootstrap at the top your custom.css.scss file.
#import 'bootstrap';
My guess would be that this includes all the classes in the custom file so that you can reference them later. I assume if you use some bootstrap gem it will make them globally available, but if you do it vanilla like I did then you need to use the code above.

Rails - form_for how to keep in same line?

For my form below, how would I prevent the form from skipping to the next line? I have taken a look at bootstrap class "form-inline" and did not seem to work as it disabled my form.
Hello World!
<%= form_for(current_user.likes.build(blog_id: blog.id)) do |f| %>
<div><%= f.hidden_field :blog_id %></div>
<%= f.submit "Submit" %>
<% end %>
This results in:
Hello World!
<Submit Button>
Question: How do I make it come in one row so it is as follows:
Hello World!<Submit Button>
Either make both your text and form inline or, as commented by Sparda, use float: left orfloat: right.
This is a trivial css thing :)

Modifying <%= f.submit %>'s default CSS input class

I have been trying without much luck to get <%= f.submit %> to appear as the same as my other "buttons", all in a row. I have found this helpful post on modifying the class of f.submit, but realized upon examining its element in browser that it took on the class of input, regardless of which additional classes I added as option parameters, thus restricting its appearance.
Essentially, each of my other buttons has the following form:
<div class="sort-nav">
<ul>
<li><h4><%= link_to "Some stuff", some_link_path %></h4></li>
</ul>
</div>
And I was wondering if there is a way to fit all of these styles compacted into one class, and override that of the input class contained in f.submit. Thanks.
For edification, this button is going to be my "Follow"/Unfollow" button used to create or destroy Relationships, which I first intend to render a _follow_form partial with the following code:
<% if current_user.following?(#course) %>
<%= render 'unfollow' %>
<% else %>
<%= render 'follow' %>
<% end %>
With each of the individual _followpartial looking like the following:
<%= form_for(current_user.relationships.build(followed_id: #course.id)) do |f| %>
<%= f.hidden_field :followed_id %>
<%= f.submit "Follow course" %>
<% end %>
a bit hackish but you can always use js to submit the form so instead of using f.submit, change it to
<div class="sort-nav">
<ul>
<li>
<h4><%= link_to_function "Some stuff", '$(this).closest("form").submit()' %></h4
</li>
</ul>
</div>

Resources