Insert ruby in html class= property
<div class="circleElement <%= u.tag %>" >...</div>
should work.
Rails: how do I add a class to this image src code? Putting div tags around it didn't work
You have to put the CSS class out of the <%= %>
Try this :
<% if item.image.attached? %>
<image src="<%=(url_for(item.image)%>" class="itemholsIm">
<% end %>
Add a CSS class to %= f.submit %
<%= f.submit 'name of button here', :class => 'submit_class_name_here' %>
This should do. If you're getting an error, chances are that you're not supplying the name.
Alternatively, you can style the button without a class:
form#form_id_here input[type=submit]
Try that, as well.
What's an elegant way to conditionally add a class to an HTML element in a view?
I use the first way, but with a slightly more succinct syntax:
<div class="<%= 'ok' if @status == 'success' %>">
Though usually you should represent success with boolean true
or a numeric record ID, and failure with boolean false
or nil
. This way you can just test your variable:
<div class="<%= 'ok' if @success %>">
A second form using the ternary ?:
operator is useful if you want to choose between two classes:
<div class="<%= @success ? 'good' : 'bad' %>">
Finally, you can use Rail's record tag helpers such as div_for
, which will automagically set your ID and class based on the record you give it. Given a Person
with id 47:
# <div id="person_47" class="person good">
<% div_for @person, class: (@success ? 'good' : 'bad') do %>
<% end %>
How to add data attribute in Rails form select tag?
Try:
{class: "form-control selectpicker", "data-live-search" => "true" }
Best way to add css classes or html text in Rails views
You are on the right track: You can move the HTML part that is identical into a partial like this
# in app/views/shared/_modal.html.erb
<div class="modal fade" id="<%= name %>Modal" tabindex="-1" role="dialog" aria-labelledby="<%= name %>ModalTitle" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered modal-lg" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<%= yield %>
</div>
</div>
</div>
</div>
and than render that model like this:
<%= render "shared/modal", name: 'login' do %>
<%= render '/devise/sessions/new' %>
<% end %>
<%= render "shared/modal", name: 'register' do %>
<%= render '/devise/registrations/new' %>
<% end %>
Note how the local variable name
is used in the partial to generate the correct id
and label
. And how the yield
in the partial will be replaced with the HTML provided in the block of the render
call.
See Using Partials to Simplify Views from Layouts and Rendering in Rails from the Rails Guides.
Ruby - dynamically add property to class (at runtime)
The method_missing
approach would work, but if you're going to use the accessors a lot after adding them, you might as well add them as real methods, like this:
class Client
def add_attrs(attrs)
attrs.each do |var, value|
class_eval { attr_accessor var }
instance_variable_set "@#{var}", value
end
end
end
This will make them work like normal instance variables, but restricted to just one client
.
Stimulus JS error when toggling class from another element
Currently, your toggle_controller.js
toggles the classlist of this.element
export default class extends Controller {
static classes = [ "change" ]
toggle() {
this.element.classList.toggle(this.changeClass)
}
}
this.element
refers to the element on which the data-controller attribute is placed. By moving the data-controller from div id="numpad"
to <div id="amount_container">
, this.element
changes scope.
The solution is to define a stimulus target attribute to specify which element to toggle.
export default class extends Controller {
static targets = [ "numpad" ]
static classes = [ "change" ]
toggle() {
this.numpadTarget.classList.toggle(this.changeClass)
}
}
<div id="amount_container" data-controller="toggle" data-toggle-change-class='hidden'>
<div id="amount_box">
<div>$999,999.99</div>
</div>
<div id="numpad" data-toggle-target="numpad">
<div>1</div>
<div>2</div>
<div>3</div>
...
<div data-action="click->toggle#toggle">Next</div>
</div>
</div>
Styling elements with a dot (.) in the class name
.a\.b { }
However there could be browsers around that don't support this.
insert html code from a form helper in rails 3.1
define a method in your application_helper.rb somthing like:
def formatted_text_field(name, value = nil, options ={})
"<div class='clearfix'>" +
label_tag(name) +
"<div class='input'>" +
text_field_tag(name, value, :class => 'xlarge') +
"</div>" +
"</div>"
end
you can then call <%= formatted_text_field('customer_email', nil) %>
from any view.
And you can also store the string in a variable in the helper method and instead return output.html_safe
.
Related Topics
How to Set The Current Frame (Or Similar) of a CSS Animation
Button Border Radius and Cursor
iOS 10.3 Safari Text-Align:Justify Bug for Persian/Arabic Content
How to Make Vertically Rotated Links in HTML
How to Make a "Double Arrow" with CSS3 Content Technique
CSS Font Twitter Like Close Button, What Did I Missing
CSS-Transform Animation Causing to Flickering
Fixed Element in Transform Translate Container Not Working
Freeze Keyframe Animation for Debugging
Svg Pattern and Gradient Together
CSS3 Onclick Activate Another Div's Animation
Correct Way for Writing an <Address> Tag for My Web Site, to Provide Contact Details for Our Company
@Font-Face Doesn't Work in Firefox (But Exact Same Code Works on Another Site)
Fade/Transition Tailwind Class to Something Else Over Certain Amount of Time