'How to merge duplicates in a column in Ruby on Rails?
I have this filter here for sign name. What I want to do is if I have more than one name of the sign to merge the content together Check the image attached. I have two serfer names. I want to display just one.
Code:
<div class="categories">
<% @oferta.each do |o| %>
<ul class="cat">
<li class="pull-left"><h2><%= link_to o.offer,o %></h2><br><h4><%= o.description %></h4>
<div class="main">
<% if o.sigs.exists? %>
<div id="myBtnContainer">
<button class="btn active" onclick="filterSelection('o')">All</button>
<% for item in o.sigs %>
<button class="btn" onclick="filterSelection('<%= item.name %>')"><%= item.name%><br></button>
<% end %>
</div>
<div class="row">
<% for item in o.sigs %>
<div class="column <%= item.name %>">
<div class="content">
<%= image_tag item.image.url(), style: "width:100%"%>
<h4><br><%=link_to item.name,item %></h4>
<p><%= item.comment %></p>
</div>
</div>
<% end %><br><br>
</div>
<% end %><br>
</div>
<% end %>
</div>
here is the oferta_controller.rb
class OfertaController < ApplicationController
before_action :find_ofertum, only: [:destroy,:edit,:update,:show]
def new
@ofertum= Ofertum.new
end
def create
@ofertum= Ofertum.new(ofertum_attributes)
if @ofertum.save
redirect_to oferta_path, notice: "Thank you... Your offer was created successfully."
else
flash.now[:error] = "Please correct the form"
redirect_to new_ofertum_path
end
end
def index
@oferta = Ofertum.all
end
def show
@ofertum = Ofertum.find(params[:id])
@sig = @ofertum.sigs.build
end
private
def ofertum_attributes
ofertum_attributes = params.require(:ofertum).permit([:offer,:description,:sign,:place,:sig])
end
def find_ofertum
@ofertum = Ofertum.find(params[:id])
end
end
in oferta model
class Ofertum < ApplicationRecord
has_many :sigs
end
in sig model
class Sig < ApplicationRecord
belongs_to :ofertum
end
Solution 1:[1]
<% for item in o.sigs %>
<button class="btn" onclick="filterSelection('<%= item.name %>')"><%= item.name%><br></button>
<% end %>
instead of for loop try
<% o.sigs.pluck(:name).uniq.each do |name| %>
<button class="btn" onclick="filterSelection('<%= name %>')"><%= name %><br>/button>
<% end %>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | teja rajana |