过滤模型不工作

时间:2015-06-16 02:02:19

标签: html ruby-on-rails

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

new.html.erb

Price:  <%= f.collection_select :price_ids, Price.all, :id,:name, {prompt: true} %>

并在

index.html.erb

   <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

prication模型

class Prication < ActiveRecord::Base
  belongs_to :dress
  belongs_to :price
end

当我按下任何价格名称时,它不会过滤为什么它不起作用?

1 个答案:

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