I'm using rails config gem to set feature flags.
Settings.yml
enable_grading: <%= 'true' == ENV['ENABLE_GRADING'] %>
When I try to hide/show some part of the template based on the feature flag, it responds inconsistently.
<% if Settings.enable_grading %>
<div class='grade_tabs'>
...
</div>
<% end %>
So here, even if Settings.enable_grading is true, the div block is not being rendered. But if I modify this to:
<% if Settings.enable_grading == true %>
<div class='grade_tabs'>
...
</div>
<% end %>
now this starts working!(the content is rendered). This is working if we use some sort of ruby interpolation just before the condition check.
<%= Settings.enable_grading %>
<% if Settings.enable_grading %>
<div class='grade_tabs'>
...
</div>
<% end %>
This is really weird. What is wrong in this code? I was using this gem to set feature flags before also which was working well.
Related
Rails each do method is acting strangely and I do not know why.
controller
def index
#fabric_guides = FabricGuide.with_attached_image.all.order(:name)
end
index.html.erb
<div class="guide-items">
<%= #fabric_guides.each do |fabric| %>
<div class="guide-container">
<%= link_to fabric_guide_path(slug: fabric.slug) do %>
<%= image_tag fabric.image if fabric.image.attached? %>
<% end %>
<div class="guide-info">
<p class="g-name">
<%= link_to fabric.name,
fabric_guide_path(slug: fabric.slug) %>
</p>
</div>
</div>
<% end %>
</div>
I have two FabricGuide records so I expect two "guide-container" but I get three. Or more precisely I get two guide containers and a third block of text containing all the content from the last FabricGuide record.
I have almost an identical setup for articles and have never encountered this problem. I'd happily share more information if needed. Thank you!
Please remove = equal sign from your each loop of view code
like below :-
<% #fabric_guides.each do |fabric| %>
...
...
<% end %>
you have used this <%= #fabric_guides.each do |fabric| %> in your view that's why it shows all record in DOM.
The expression for erb tags is <% %>
now if we want to print that tag too then we apply <%= %>
On my views I use 1 form that includes a block that renders comments. I do not want to run it when creating a new record. So, I tried conditions like so...
<% unless #annotation_id.nil? %>
<hr>
<div class="row">
<div class="col-md-8">
<h4>Comments</h4>
<%= render #annotation.comments %>
</div>
<div class="col-md-4">
<%= render 'comments/form' %>
</div>
</div>
<% end %>
This however results in never displaying the block - also when the annotation record exists. What am I doing wrong?
You don't show that you have actually set #annotation_id to something.
A simpler way might be to use the .new_record? method instead, like:
<% unless #annotation.new_record? %>
...
<% end %>
use if #annotation.persisted? or unless #annotation.new_record?
I am facing a problem with errors during form validation.
How semantic-ui shows errors
<div class="field error">
<label>Gender</label>
<div class="ui selection dropdown">
<div class="default text">Select</div>
<i class="dropdown icon"></i>
<input type="hidden" name="gender">
<div class="menu">
<div class="item" data-value="male">Male</div>
<div class="item" data-value="female">Female</div>
</div>
</div>
</div>
As you can see it places .errorcss class at the container.
How rails shows errors
Proc.new do |html_tag, instance|
%Q(<div class="field_with_errors">#{html_tag}</div>).html_safe
end
Rails wraps the form field (input,textarea,select etc) with a div with field_with_errors class.
Even customizing this at app.config I see no way changing the parent's css. Is there is any workaround for this ?
I also had this problem and found zero convenient solutions to it.
Solved it by doing the following things:
First of all, in any view I want to have the error class applied to the .fields div I've added the following line at the end:
new.html.erb (for example)
<%= render partial: 'shared/fields_error_class', locals: {model: #user} %>
Next, create the following partial: /shared/_fields_error_class.html.erb:
<% class_name = model.class.name.downcase %>
<% field_selectors = [] %>
<% model.errors.keys.each do |field| %>
<% field_selectors.push "##{class_name}_#{field}" if model.errors[field].any? %>
<% end %>
<%= javascript_tag do %>
$("<%= field_selectors.join(',') %>").parents('.field').addClass('error');
<% end %>
I hope my solution is self explanatory and will help others who struggle with semantic-ui and rails.
For Rails 5 and HTML5 without JQuery, replace the partial in the original solution with:
<% class_name = model.class.name.downcase %>
<% model.errors.keys.each do |field| %>
<% if model.errors[field].any? %>
<%= javascript_tag do %>
document.getElementById('<%= "#{class_name}_#{field}" %>').parentElement.parentElement.classList.add('error');
<% end %>
<% end %>
<% end %>
Also, add the following file to Rails 5:
# initializers/customize_errors.rb
ActionView::Base.field_error_proc = Proc.new do |html_tag, instance_tag|
# gets rid of field_with_errors div being applied to fields
html_tag
end
undefined method `each' for nil:NilClass
<% max_level = Spree::Config[:max_level_in_taxons_menu] || 1 %>
<nav id="taxonomies" class="sidebar-item" data-hook>
<% #taxonomies.each do |taxonomy| %>
<% unless taxonomy.name == 'Tags' %>
<h6 class="taxonomy-root"><%= Spree.t(:shop_by_taxonomy, :taxonomy => taxonomy.name) %></h6>
<%= taxons_tree(taxonomy.root, #taxon, Spree::Config[:max_level_in_taxons_menu] || 1) %>
This is the error i am getting and this is the code i updated to taxonomies.each.to_a not sure if that is right and how do i update the code so the server will run the new code instead of the older code?
Here is the updated code
<% max_level = Spree::Config[:max_level_in_taxons_menu] || 1 %>
<nav id="taxonomies" class="sidebar-item" data-hook>
<% #taxonomies.each.to_a do |taxonomy| %>
<% cache [I18n.locale, taxonomy, max_level] do %>
<h6 class='taxonomy-root'><%= Spree.t(:shop_by_taxonomy, :taxonomy => taxonomy.name) %></h6>
<%= taxons_tree(taxonomy.root, #taxon, max_level) %>
<% end %>
<% end %>
</nav>
Looks like what you're doing is forcing a nil object to an array, which will work, yt isn't an ideal solution. You can wrap the whole thing in this:
<% if #taxonomies && #taxonomies.any? %>
#your code goes here
<% end %>
...which will check for the presence of a taxonomies variable and make sure it isn't an empty array before running your code. As far as getting the code on the server goes, you'll have to post more info about your deployment setup and version control to answer that question.
How would one go about turning the following code into the latter?
<div id="faqs">
<% if #faqs.length > 0 %>
<% #faqs.each do |faq| %>
<div class="faq">
<strong>Q:</strong> <%= faq.question %>
<br />
<strong>A:</strong> <%= faq.answer %>
</div>
<% end %>
<% else %>
<p>No FAQs to display.</p>
<% end %>
</div>
<div id="faqs">
<% #faqs.empty? ? content_tag(:p, "No FAQs to display.") : #faqs.each do |faq| %>
<div class="faq">
<strong>Q:</strong> <%= faq.question %>
<br />
<strong>A:</strong> <%= faq.answer %>
</div>
<% end %>
</div>
I'm curious as to whether I can get the latter code to work. The only element of it that is failing at the moment is that the content_tag() is not displaying - this is due to the fact that I'm not using printable ruby tags (<%= # %>) but using them will dump out the FAQ objects underneath the content.
I considered the use of puts() to print the content_tag() while inside the ruby tags but that didn't work.
I've tried to search for this issue but haven't yielded anything useful.
Is this achievable and if so, does it have any benefits other than being prettier?
One way to make the later code to work if you can put the body of the loop in a helper function and return the out put of content_tag from that. The line in view file might be somewhat like this.
<%= #faqs.empty? ? content_tag(:p, "No FAQs to display.") : printList(#faqs) %>
and your printList function will return the output of nested content_tags. You can make a generic list printing function which can be used for any list.
Something so obvious but still shared.
This should work (for clarity, I moved FAQ tag generation in separate helper method):
<div id="faqs">
<%= raw (#faqs.empty? ? content_tag(:p, "No FAQs to display.") : #faqs.map { |faq| faq_div(faq) }.join) %>
</div>
or, perhaps more clean:
<div id="faqs">
<%= content_tag(:p, "No FAQs to display.") if #faqs.empty? %>
<%= raw #faqs.map { |faq| faq_div(faq) }.join %>
</div>
meanwhile, in helpers:
def faq_div(faq)
'<div class="faq"><strong>Q:</strong> %s<br /><strong>A:</strong> %s</div>' % [faq.question, faq.answer]
end
This should work:
<% if #faqs.each do |faq| %>
<div class="faq">
<strong>Q:</strong> <%= faq.question %>
<br />
<strong>A:</strong> <%= faq.answer %>
</div>
<% end.empty? %>
<p>No FAQs to display.</p>
<% end %>