How can I condense my code into a single statement:
<% #policyholderdetail.errors.each do |attr,msg| %>
<% if attr == :title %>
<li><%=attr %> <%= msg %></li>
<% end %>
<% end %>
I would like to show only the errors for :title next to the field but feel there should be a better statement to do this as opposed to looping through all of the errors until I get to the one I want.
Question - can I condense the first two lines into one better statement?
You can write: #policyholderdetail.errors[:title]. See here.
Use
<% if #policyholderdetail.errors[:title].present? %>
Related
#complete[] and #incomplete[] are two array then I need to used with these array with checkbox complete array is by default true and incomplete by default false .
<%= form_with(model: #task, local: true) do |form| %>
<% #complete = Array.new %>
<% #incomplete = Array.new %>
<% #tasks.each do |task| %>
<% if task.complete != false then %>
<% #complete << task.name %>
<% else %>
<% #incomplete << task.name %>
<% end %>
<% end %>
<!-- complete task -->
<%= #complete %>
<!-- incomplete task -->
<%= #incomplete %><br>
<% #complete.each do |i| %><div id = "task_check">
<%= form.check_boX "#incomplete[]", incomplete.id %>
<%= form.check_box "chkbox_ary[#{i}]" , {checked:true} %>
<%= form.submit "update"%>
<%= content_tag(:strike, i)%>
<br></div>
<% end %>
<br> <hr>
<script>
<% #incomplete.each do |i| %>
<%= form.check_box "chkbox_ary[#{i}]" %>
<%= form.submit "update"%>
<%= i %><br>
<% end %>
<% end %>
If I check the box as false in complete then it should append to array called complete and if I check the box true in incomplete then it should append to second array called complete.
if I may, I'd like to point out some stylistic issues in your code first:
it is unusual to compare with false, i.e., if task.complete != false should be just if task.complete this works thanks to the concept of Truthiness in Ruby
you can just leave off the then in an if-conditional, that's what most Ruby developers do, i.e., if task.complete then can just be if task.complete
view templates (.erb files) should contain as little logic as possible, i.e., extract the creation of the #incomplete and #complete arrays into your controller or some other place
Ruby Enumerable has many methods to make common work easier, for instance splitting a collection into two collections can be done with partition, i.e., you can do #complete, #incomplete = #tasks.partition(&:complete) to populate the complete and incomplete arrays (&:complete is a shorthand for { |task| task.complete }, google Ruby symbol to proc if you want to know more about it)
I'm not sure I understand your question as it is. At the time the complete and incomplete arrays exist no user interaction takes place. I.e., nobody can click anything. When the HTML is rendered, no arrays exist anymore.
So there's two ways I can interpret your question:
You wish to move the DOM elements around => you need to use JavaScript for that
You wish to partition the checkboxes when the form is submitted => you need to show us the code in your controller that handles the form-submit for us to help you out
Which is it?
PS: I know this isn't an answer right now, but I could not write this as a comment since it's too long and a comment would have been illegible.
<% consents_checkboxes.each do |checkbox| %>
<%= checkbox.html_safe %>
<% end %>
Hello there,
can i give them a class while looping through them? I can't get it to work and tried several different ways.
This is something I would like to achieve
<% consents_checkboxes.each do |checkbox| %>
<%= checkbox.html_safe, class: 'checkbox' %>
<% end %>
thank you
You can only do it with an element. What you want to do is:
<% consents_checkboxes.each do |checkbox| %>
<p class="checkbox"><%= checkbox.html_safe %></p>
<% end %>
Of course, you can use another element (span, div etc.).
What's on consents_checkboxes? You should provide more context when you ask for something...
It looks like you have strings with the html code, right? you will have to parse the string with something like nokogiri and add a class
<%= Nokogiri.parse(checkbox).add_class('checkbox') -%>
Or you could modify the process that generates that consents_checkboxes to include the class you need. Maybe there's better options, but with only that information it's really hard to tell.
i'm trying to use a conditional inside a iteration but did not worked so, here the scenario:
in this case if the if the order or the product is present should just show the order and products with the feedback.
but even if is present show the feedback with odata and pdata.
someone know why?
<% #feedbacks.each do |feedback| %>
<% if order.present? && product.present? %>
<% order = feedback.order %>
<% product = order.product %>
<% else %>
<% odata = feedback.odata %>
<% pdata = odata.pdata %>
<% end %>
I guess this is what you are trying to do,
<% #feedbacks.each do |feedback| %>
<% if (order = feedback.order).present? && (product = feedback.product).present? %>
<%= order.title %>
<%= product.title %>
<% else %>
<%= (odata = feedback.odata).name %>
<%= odata.pdata.name %>
<% end %>
<% end %>
Note: title and name are assumed columns, replace it with your required/respected attribute.
Please go through this to understand the difference between various erb tags.
Comparison:
for the if condition you are trying to call order and product directly, which will throw error as they are related to feedback.
<% %> just executes the ruby code, you wanted to print the data so need to use <%= %>.
no need to save them in variable when you are not going to use it. I have saved them while checking the existence of the object in the condition and could use to display without querying the db.
I'm trying to display a list of elements from an Associations array:
<%= Event.find_by_id(params[:id]).attendees.to_a.map do |att| %>
<%= att.name %>
<% end %>
the output in the browser is the following:
Attendee-name ["\n"]
How can I get rid of the ["\n"] or what is a better way of manipulating the Associations arrays?
Try this out, this should be what you are going for.
<% Event.find_by_id(params[:id]).attendees.each do |att| %>
<%= att.name %>
<% end %>
Also .chomp is what you need to remove \n but try the above code first.
It was a very stupid problem, this is the solution if anyone gets stuck on the same thing:
<% Event.find_by_id(params[:id]).attendees.to_a.map do |att| %>
<%= att.name %>
<% end %>
leaving the <%= on the first line was the cause of the problem.
I have a loop of questions where each question belongs to a post and the post title is displayed above the question. If 2 ( or 3 or 4 etc) questions in a row belong to the same post I only want to display the post name once. My idea was to use an index to check if the prior questions post == the current questions post. The problem is I'm not sure how that would work.
Here is what I tried:
<% #questions.each_with_index do |question, i| %>
<% unless (i-1).comment.post == question.comment.post %>
<%= question.comment.post.title %>
<% end %>
<% end %>
This gives me an undefined method comment error since I can't call '(i-1).comment' but can I do something like that or is there a better way to do this?
One way to do this would be to use group_by to group the questions by post:
<% #questions.group_by {|q| q.comment.post}.each do |post, questions| %>
<%= post.title %>
<% for question in questions %>
<%= question.title %>
<% end %>
<% end %>
If you need to maintain the order of the questions, you could use each_cons.
<%= #questions.first.comment.post.title %>
<% #questions.each_cons(2) do |previous_question, question| %>
<% unless previous_question.comment.post == question.comment.post %>
<%= question.comment.post.title %>
<% end %>
<% end %>
Couple of minor notes:
This is a troubling Law of Demeter violation (having to traverse from the question to the comment to the post and eventually the title).
This sort of complicated data manipulation is much better in a Ruby object than in a template. Ideally, templates are dead simple.