gpt4 book ai didi

javascript - 如何在 Ruby on Rails 中合并列中的重复项?

转载 作者:行者123 更新时间:2023-12-02 23:10:46 25 4
gpt4 key购买 nike

我这里有一个用于标志名称的过滤器。我想要做的是,如果我有多个标志名称,请将内容合并在一起检查所附图片。我有两个农奴名字。我只想显示一个。

enter image description here

代码:

<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>

这是 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

在oferta模型中

class Ofertum < ApplicationRecord
has_many :sigs
end

在信号模型中

class Sig < ApplicationRecord
belongs_to :ofertum
end

最佳答案

<% for item in o.sigs %>
<button class="btn" onclick="filterSelection('<%= item.name %>')"><%= item.name%><br></button>
<% end %>

而不是for循环尝试

<% o.sigs.pluck(:name).uniq.each do |name| %>
<button class="btn" onclick="filterSelection('<%= name %>')"><%= name %><br>/button>
<% end %>

关于javascript - 如何在 Ruby on Rails 中合并列中的重复项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57373695/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com