Loop through hash in rails - ruby-on-rails

I have some functionality where I client can create custom fields, what I'm trying to do now is to loop through that data but I can't seem to get it to work.
Here are the params when we create or update a record, I've removed some as I'm only trying to loop through custom_fields_attr
Parameters: {... "custom_fields_attr"=>{"1"=>"testing12", "2"=>"", "3"=>"", "4"=>""}}, ...}
I have the following code:
<!-- start of custom fields -->
<% if Contact.has_custom_fields %>
<div class="form-block">
<h2 class="txt-title-alt">Custom Fields</h2>
<% Contact.custom_fields.in_groups_of(2).each do |field| %>
<div class="l-row-block clearfix">
<% field.each do |item| %>
<div class="l-06col l-ml-12col l-md-12col">
<div class="field field_display l-row-block clearfix">
<p class="like_p clearfix">
<strong class="l-06col">
<%= item.label %>:
</strong>
<span class="value l-06col">
<%= item.value %>
</span>
</p>
</div>
</div>
<% end %>
</div>
<% end %>
</div>
<% end %>
<!-- end of custom fields -->
I have a custom fields table which is where we get the label from but I need to get the value from the custom_fields_attr hash
Edit
The output of #contact.custom_fields_attr is
>> #contact.custom_fields_attr
=> {"1"=>"test field123"}

Instead of doing field.each do |item| try field.each do |key, value|. This lets you access the key and value of each hash pair in your block.

Could you tell me what is the output of Contact.custom_fields?
Can you tell, how the client is creating the custom fields, because if they are params send to the controller, you can access the params in the controller, in this case you want "custom_fields_attr", you can call it by params["custom_fields_attr"] and then to loop through it params["custom_fields_attr"].each do |key, value| calling in each time key = "1" and value = "testing12" then key = "2" and value = "" and etc.

Related

Rails Helper create unintended Array ouput

I have created a rails helper method, So far so good the only problem is it created an unexpected array ouput.
I did try the key value pair using the each method but the array still there.
I'm trying to figure it out how to remove the unexpected array
My application_helper.rb
def bid_items(origin, destination)
item = Item.where(item_deliver_from: origin).where(item_deliver_to: destination).where(shopper_id: current_user)
end
My search_results.html.rb
<%= bid_items(trip.origin, trip.destination).each do |item| %>
<div class="card border-0">
<%= image_tag item.cover_image_url(:cover_image_medium), class: "card-img-top" %>
<div class="card-body">
<h5 class="card-title"><%= item.name %></h5>
</div>
</div>
<% end %>
Though I got the intended results still, there's an unexpected array
check the image
I just want to remove this area
See Red X
Remove = sign from <%= bid_items(trip.origin, trip.destination).each do |item| %>
So, make it <% bid_items(trip.origin, trip.destination).each do |item| %>

display results with a show more link

I'm trying to figure out a way to always show 5 jobs in my Rails app and then have a link that when clicked will show all of the remaining jobs. Should I do something in my jobs_controller where I'm just getting the first 5 and then getting the rest, or is it better to do it in the view somehow?
<!-- company -->
<div class="vertical-space">
<b>Company Name</b><br />
<div class="sub-text">
<% Job.by_company_count.size.each do |name, count| %>
<div class="indent"><%= link_to name, filtered_jobs_path(company: name) %>
(<%= count %>)
<br>
</div><!--./indent-->
<% end %>
</div><!--./sub-text-->
</div>
To pass data to the view, set an instance variable of the controller, in the corresponding action:
#jobs = Job.by_company_count
And then access and iterate #jobs in view:
<% #jobs.each do |name, count| %>
<div class="indent"><%= link_to name, filtered_jobs_path(company: name) %>
(<%= count %>)
<br>
</div><!--./indent-->
I'm assuming that you have a scope by_company_count defined in Job model!

dynamic bootstrap tabs with rails

I am trying to get rails to generate dynamic navigation tabs that refer to groups user is enrolled at. Basically, what I want to achieve is to dynamically have tabs named after groups that user is enrolled at (which is working fine) and then showing the content of each group by clicking on its tab (which is not working properly for some reason). The page loads data correctly but toggling between tabs doesn't work
Here is the code in the view
<div class="tabbable tabs-left">
<div class="row">
<div class="col-md-4">
<ul class="nav nav-pills nav-stacked">
<% current_user.group.each do |group| %>
<li><a href="#<%= group.name %>" data-toggle="tab">
<%=group.name %></a></li>
<% end %>
</ul>
</div>
<div class="col-md-8">
<div class="tab-content">
<% current_user.group.each do |group| %>
<div class="tab-pane fade <%= 'in active' if current_user.group.first == group %>" id="<%=group.name%>">
<% if current_user.group_feed(group.id).any? %>
<ol class="microposts">
<%= render current_user.group_feed(group.id) %>
<%= group.name %>
</ol>
<% end %>
</div>
<% end %>
</div>
</div>
</div>
</div>
Is there something that I am missing?
The problem is group.name producing an invalid html id attribute.
html ids should not start with a number(numbers anywhere else are ok), and have no spaces. Example:
Invalid:
1foo
aaa b
Valid:
foo1
aaa-b
group.name.parameterize will remove any odd chars(#£$ etc) and replace spaces with "-" so use that.
You also want to make this unique as things with names like: "foo" and "foo!" will parameterize to the same thing: "foo".
I'd go with:
id="<%=(group.name.gsub(/[0-9]+/, "")+group.id.to_s).parameterize%>"
This code, removes any number from the name(it only really applies at the start of the id) then adds the id on the end making it unique.

Rails - how to get array index in view using dom_id?

I have an array attribute in the form and wanting to iterate over this and append each index value to dom id.
I've looked up in the API for use of dom_id but I can't locate one that deals with array attribute.
If dom_id is not meant for working with array index, is there any other way to get array indexes in the view?
<div id="ingredient<%= dom_id(?) %>">
I guess you want something like following
<% array.each_with_index do |arr, index| %>
<div id="ingredient<%= index %>">
<% end %>
<% custom_array.each_with_index do |item,index| %>
<div id="<%= ingredient_#{index} %>">
<!--other contents goes here -->
</div>
<% end %>
Otherwise you can try id = " ingredient_<%= index>"

How can I reference the name of the previous array item in this Rails ERB mark-up loop?

I am a Rails noob. I am generating a menu in an ERB template, looping through the array "elements":
<% #elements.each do |element| -%>
<div class="category <%= element.category.color %>">
</div>
<% end %>
I want to add an additional class to each item (except the first, obviously), referencing the element.category.color of the PREVIOUS item, so that the final mark-up looks like:
<div class="category blue">I am the first, no extra class</div>
<div class="category green after-blue">I come after blue</div>
<div class="category yellow after-green">I come after green</div>
This is all basically a work-around to avoid using next-sibling CSS selectors, which are slow as hell in some of the browsers I need to support, and causing rendering problems when changing the background colours of the items.
Can I add the class I want directly in the ERB, or will I need to also use the controller to calculate it?
You can do this
<% #elements.each_with_index do |element, index| -%>
<div class=<%= "category #{element.category.color} " + ("after-#{#elements[index-1].category.color}" unless index == 0) %>>
</div>
<% end %>
Another way:
<% prev = '' %>
<% #elements.each do |element| -%>
<div class=<%= "category #{element.category.color}#{' after-' + prev if prev.present?}" %>>
<% prev = element.category.color %>
</div>
<% end %>

Resources