conditional haml - if else nesting
You can use a ternary operator to conditionally apply the style attribute:
#content-inner{ :style => @transparency ? "background: url(../../../images/illustrations/" + @transparency + ") no-repeat 88% 50%" : '' }
#main-block
For a more complicated arrangement, for example manipulating multiple hash attributes, it's best to either use a helper method, or to move the common content to a partial.
Haml if else nesting
Try
-["email", "password"].each do |param|
.text{:class => f.object.errors.any? ? "error" : nil}
= f.label param.to_sym
= f.text_field param.to_sym
%p#foo= "Escriba el #{param}"
= render 'shared/error_messages', :object => f.object, :param => param.to_sym
haml conditional if/else indentation
You can set the class to a variable holding the class names based on the index to DRY it up:
- @color.shades.each_with_index do |shade, index|
- shade_classes = index == 0 ? '' : 'horizontalLine second'
#shades{ :class => shade_classes }
%h3 something
%dl
%dt some
%dd some1
How to do an if/else in HAML without repeating indented code
!!!
- @user = "jed" #=> stubbing out an instance
%html
%head
- string = defined?(@user) ? "#{@user}" : nil #=> for demo only, wrap this in a helper method
%title{'data-account' => string}
%body
=yield
HAML: Indenting if/else statements with common content
you can try Ternary operator:
%div{ :class => condition ? 'truecondition' : 'falsecondition' }
(Ten lines of content)
HAML conditional tags
If you need to do this logic in different views I think there are two approaches you can follow:
1. Make a partial and render it where you need this. If you need to pass variables use local_assigns
_my_list.html.haml
- if ordered
%ol
- else
%ul
use it
render 'partials/my_list', ordered: ordered
2. Make your own helper
def my_list(ordered)
if ordered
content_tag(:ol, class: 'my-class') do
# more logic here
# use concat if you need to use more html blocks
end else
content_tag(:ul, class: 'my-class') do
# more logic here
# use concat if you need to use more html blocks
end
end
end
use it
= my_list(ordered)
You can keep your ordered variable outside of the view and deal with the whole logic inside the helper.
If you ask yourself what to use, well the first answer from here is pretty good.
If Else in HAML to check the value of a select box
Yes and no. You can write a conditional based on the values of the record that you bind to the form:
= form_for @question do |f|
= f.label :type
= f.select, :type, %w(Text Picture Audio Video), id: 'question_type_combo'
- unless f.object.question_type_combo === 'Text'
= f.label :url
= text_field :url, id: 'question_url_text'
But this would only change the visibility after the user submits the form and not be very useful.
Instead you can just use jQuery to create an event handler for the ´change´ event.
$(document).on('change','#question_type_combo', function(){ var type = $(this).first(':selected').val(); var $other_input = $('#other_input'); if (type == 'Text') { $other_input.hide(); } else { $other_input.show(); }});
// sets the initial state// if you are using turbolinks$(document).on('page:load', function(){ $('#question_type_combo').trigger('change');});
// if you are not using turbolinks $(function(){ $('#question_type_combo').trigger('change');});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><form> <div class="field"> <label>Type</label> <select name="question[question_type_combo]" id="question_type_combo"> <option>Text</option> <option>Something else</option> </select> </div> <div class="field" id="other_input"> <label>URL</label> <input type="text" name="question[url]"> </div></form>
If else condition in HAML
You need to call cycle within a loop. Are you doing that?
And this would make tidier code:
.review.alert{class: cycle('alert-info', 'alert-success')}
%h4
= review.username
%hr
...
Unless you meant to only include username, save_hours & suggestion every other time?
Expressing conditional HAML possibly with ternary operator
The code you have makes the text a child of the <br>
element; that is not desirable. What you really meant, I think, was:
%br
- if @page.nil?
(nothing yet)
- else
#{@page.name}
For this you can simply do:
%br
#{@page.nil? ? "(nothing yet)" : @page.name}
Or
%br
= @page.nil? ? "(nothing yet)" : @page.name
Or simply:
<br>#{@page ? @page.name : "(nothing yet)"}
However, personally I would 'fix' this in the controller so that you always have a @page
, with something like:
unless @page
@page = Page.new( name:"(nothing yet)", … )
end
With this you can stub out what a new/empty/nothing page looks like and let your view treat it like any other. Your @page
still won't have a @page.id
, so you can use that for tests to decide if you are creating a new item or editing an existing one.
This is how I handle all my forms that may be used to create or edit an item: provide defaults by creating (but not adding to the database) an item with the default values.
Aside: You're creating a <br>
which is almost never a good idea, and you're creating it with Haml which should not be used for content markup. You might step back and think about what you're doing.
Related Topics
Attempting to Install Libv8, "Failed to Build Gem Native Extension"
How to Create Temp Dir in Ruby
What Graphing Packages/Apis Exist for Ruby
Ruby Find and Return Objects in an Array Based on an Attribute
Convert 12 Hr Time to 24 Hr Format in Ruby
Warning: String Literal in Condition
How to Use Rails 5.1.0 and Jquery
Rails - Paperclip Validating Attachment Size When It Shouldn't Be
Rails Redirect_To :Back Not Working
Regex to Match Hashtags in a Sentence Using Ruby
Trouble on Rendering a Template Passing a Local Variable
How to Make the Url's in Ruby on Rails Seo Friendly Knowing a @Vendor.Name
Add a Callback Function to a Ruby Array to Do Something When an Element Is Added
Ruby - Array.Join Versus String Concatenation (Efficiency)
Raise Exception When Accessing Attributes That Doesn't Exist in Openstruct