def index
if params[:price_id].present?
if params[:price_id]
@prications = Prication.where(price_id: params[:price_id])
@dresses = @prications.map(&:dress)
end
end
else
@dresses=Dress.order(id: :desc)
if params[:search]
@search_term = params[:search]
@dresses = @dresses.search_by(@search_term)
end
end
private
def dress_attributes
dress_attributes = params.require(:dress).permit(:name,:email,:phone,:description,:image,:image2,:price_ids)
end
和
Price: <%= f.collection_select :price_ids, Price.all, :id,:name, {prompt: true} %>
并在
<h6>Shop By Price</h6>
<% Price.all.each do |price| %>
<%= link_to price.name,dresses_path(params.merge(price_id: price.id)) , class: "#{'shadow' if params[:price_id].to_i == price.id}" %><br>
<% end %>
</div>
class Price < ActiveRecord::Base
has_many :prications,dependent: :destroy
has_many :dresses, through: :prications
end
class Prication < ActiveRecord::Base
belongs_to :dress
belongs_to :price
end
当我按下任何价格名称时,它不会过滤为什么它不起作用?
答案 0 :(得分:0)
可能是因为你在if条件
之间有一个额外的结局你有这个:
def index
if params[:price_id].present?
if params[:price_id]
@prications = Prication.where(price_id: params[:price_id])
@dresses = @prications.map(&:dress)
end
end
else
@dresses=Dress.order(id: :desc)
if params[:search]
@search_term = params[:search]
@dresses = @dresses.search_by(@search_term)
end
end
它应该是这样的:
def index
if params[:price_id].present?
if params[:price_id]
@prications = Prication.where(price_id: params[:price_id])
@dresses = @prications.map(&:dress)
end
else
@dresses=Dress.order(id: :desc)
if params[:search]
@search_term = params[:search]
@dresses = @dresses.search_by(@search_term)
end
end
end