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.
Related
I'm totally stuck on looping through a form.select in Rails (using Elastic Search/Searchkick aggs).
I can access the 'bucket' array (and thus "key" and "doc_count") when I don't pre-pend the form.select helper, but it just doesn't loop through when it's there. Not sure how I would get the options dynamically otherwise!
Does anyone know what might be going wrong (ignore the link_to stuff, I'll be changing that once I can get the actual "key")? Perhaps I'm using the form.select helper incorrectly? Thanks!
<% form.select #jobs.aggs["location"]["buckets"].each do |bucket| %>
<% if params[:location] == bucket["key"].to_s %>
<strong><%= link_to bucket["key"], request.params.except(:location)%></strong>
<% else %>
<%= link_to bucket["key"], request.params.merge(location: bucket["key"])%>
<% end %>
<% end %>
I figured it out! It was a syntax error on my part, as I forgot to use :location in the first part of the form.select, and additionally didn't wrap the block after this in [] brackets (presumably the options have to be in an array or hash with this helper).
Here is the code (simplified) which solved this for me:
<% form.select :location, [#jobs.aggs["location"]["buckets"].each do |bucket| %>
<%= bucket["key"] %>
<% end ] %>
I want to display a checkbox for all elements in a model called MyModel. Here is what I wrote:
<%= MyModel.all.each do |c| %>
<%= check_box_tag(:id) %>
<%= label_tag(:name, c[:name]) %><br>
<% end %>
It does show the checkboxes as expected but at the end, I also get a list of the model content as shown in this screenshot.
Actually, it seems to be related to <%= MyModel.all.each do |c| %> because just printing out simple text still prints the whole model table content at the end:
<%= MyModel.all.each do |c| %>
toto<br>
<% end %>
shows this screenshot
Any idea how to get rid of this list at the end?
Thanks!
<%- MyModel.all.each do |c| %>
<%= check_box_tag(:id) %>
<%= label_tag(:name, c[:name]) %><br>
<% end %>
- - evaluates code.
= - evaluates code and outputs.
When iterating through a collection of records, how can i add a radio_button to each image. here is what i have so far
<% #default_images.each do |d| %>
<%= image_tag(d.image_url(:campaign_form), 'data-image-id' => d.id)) %>
<% end %>
Im not sure on the syntax to generate a radio button with an associated image here.
I have previously done it within a form_for
f.collection_radio_buttons(:default_image_id, DefaultImage.all, :id, :image) do |b|
b.label { b.radio_button + image_tag(b.object.image) }
end
but ive already got the collection stored within #default_images in this scenario
any help appreciated
Thanks
Ended up doing this which seems to work
<% #default_images.each do |d| %>
<%= radio_button_tag :default_images, d.id %>
<%= image_tag(d.image_url(:campaign_form)) %>
<% end %>
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? %>
I have an action in the controller:
def user_detail
#user_detail = UserDetail.find_by_id(11)
end
And in the view:
<%= #user_detail -%> // displays me like #
I am trying to retrieve the contents of #user_detail: actually the hash contains {:empid=>"11111", :prjtname=>"aaaaa", :prjtrole=>"Developer"}
How do I display the user detail's empid and other values?
Since I know what question you asked earlier, I think this is the syntax you actually want to use:
<%= #user_detail.additional_info[:empid] %>
Unless of course you renamed the name of the hash :)
Another approach, if you want all the content from the hash but the keys varies from each record, you could loop through them like this:
<% #user_detail.additional_info.each_pair do |key, value| %>
<p>Key: <%= key %> Value: <%= value %></p>
<% end %>
To get simple debug output like the example you posted, this will handle it:
<%= #user_detail.inspect %>
try this <%= #user_detail.emplid %> <%= #user_detail.prjtname %> <%= #user_detail.prjtr %>
More of an extraction from #dln's answer
try using
<%= #user_detail[:emplid] %>
<%= #user_detail[:prjtname] %>
<%= #user_detail[:prjtr] %>
Hope this solves your prob