列出集合中的唯一值

时间:2018-09-22 05:56:06

标签: ruby-on-rails

在我的应用程序中,我有一个名为Links的表,其中包含Country,State和City等列。

每个用户都可以发布自己的链接,其中国家,州和城市为必填值。

我的主页列出了所有用户的所有链接,并具有一个下拉菜单,该下拉菜单应显示已在链接中注册的所有单个国家/地区,然后应过滤另一个下拉菜单的条目,该下拉菜单应已在所有所选国家/地区的链接。

我已经能够从链接中列出所有不同的城市:

  <li class="dropdown">
    <a href="#" class="dropdown-toggle" id="drop3" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Select city&nbsp;&nbsp;⌄</a>
    <ul class="dropdown-menu" aria-labelledby="drop3">
      <% Link.select(:city).distinct(&:city).uniq do |link|%>
        <li><%= link_to link.city, root_path(city: link.city), class: "btn-purple" %></li>
      <% end %>
    </ul>
   </li>

我的问题是如何为国家下拉列表列出唯一值并使其过滤此城市下拉列表。之后,我想知道如何将信息作为URL参数发送到具有过滤结果的首页。

我以root_path作为参数创建了到":city"的路线,因此通过跟随"localhost:3000/?city=City%20Name",主页链接被注册的城市过滤。

编辑

我已经编辑了问题以显示schema.rb和链接控制器:

schema.rb链接

create_table "links", force: :cascade do |t|
  t.string "title"
  t.string "url"
  t.datetime "created_at", null: false
  t.datetime "updated_at", null: false
  t.integer "user_id"
  t.integer "cached_votes_total", default: 0
  t.integer "cached_votes_score", default: 0
  t.integer "cached_votes_up", default: 0
  t.integer "cached_votes_down", default: 0
  t.string "image_file_name"
  t.string "image_content_type"
  t.integer "image_file_size"
  t.datetime "image_updated_at"
  t.text "description"
  t.string "country"
  t.string "city"
  t.string "address"
  t.integer "street_number"
  t.string "neighborhood"
  t.string "route"
  t.string "administrative_area_level_1"
  t.integer "postal_code"
  t.index ["cached_votes_down"], name: "index_links_on_cached_votes_down"
  t.index ["cached_votes_score"], name: "index_links_on_cached_votes_score"
  t.index ["cached_votes_total"], name: "index_links_on_cached_votes_total"
  t.index ["cached_votes_up"], name: "index_links_on_cached_votes_up"
  t.index ["user_id"], name: "index_links_on_user_id"
end

links_controller.rb

class LinksController < ApplicationController
  load_and_authorize_resource
  before_action :set_link, only: [:show, :edit, :update, :destroy]
  before_action :authenticate_user!, except: [:index, :show]

  # GET /links
  # GET /links.json
  def index
    @links = Link.all
    if params[:tag]
      @links = Link.tagged_with(params[:tag])
    end
    if params[:city]
      @links = Link.where(city: params[:city])
    end
  end

  def new
    @link = current_user.links.build
  end

  # POST /links
  # POST /links.json
  def create
    @link = current_user.links.build(link_params)
    @link.user_id = current_user.id
    respond_to do |format|
      if @link.save
        format.html { redirect_to @link, notice: 'Link was successfully created.' }
        format.json { render :show, status: :created, location: @link }
      else
        format.html { render :new }
        format.json { render json: @link.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /links/1
  # PATCH/PUT /links/1.json
  def update
    respond_to do |format|
      if @link.update(link_params)
        format.html { redirect_to @link, notice: 'Link was successfully updated.' }
        format.json { render :show, status: :ok, location: @link }
      else
        format.html { render :edit }
        format.json { render json: @link.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /links/1
  # DELETE /links/1.json
  def destroy
    @link.destroy
    respond_to do |format|
      format.html { redirect_to links_url, notice: 'Link was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  def upvote
    @link = Link.find(params[:id])
    @link.upvote_by current_user
    redirect_back fallback_location: '/'
  end
  def downvote
    @link = Link.find(params[:id])
    @link.downvote_by current_user
    redirect_back fallback_location: '/'
  end

def comments
    @link = Link.find(params[:link_id])
      @link.comments.count
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_link
      @link = Link.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def link_params
      params.require(:link).permit(:title, :url, :image, :description, :tag_list, :address, :country, :city, :route, :street_number, :neighborhood)
    end
end

编辑2

我已经编辑了一些前面的段落。现在,我可以在下拉列表中显示链接城市的不同值,并将其重定向到已过滤的首页,但是我希望通过国家下拉列表对城市进行过滤。

0 个答案:

没有答案