0

In my application I have a table named Links that contains, among other columns, Country, State and City.

Each user can post their own link, with Country, State and City as mandatory values.

My homepage lists all the Links of all users and has a dropdown that should display all the single Countries already registered in the Links, which should then filter the entries of another dropdown that should have all cities + states already registered in all the links of the selected country.

I have been able to list all the distinct cities from Links:

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

My question is how to list unique values ​for a Countries dropdown and have it filter this City dropdown. After that, I would like to know how to send the information as a URL parameter to redirect to the homepage with the filtered results.

I created a route to the root_path with ":city" as a parameter, so by following to "localhost:3000/?city=City%20Name" the homepage links are filtered by the registered city.

EDIT

I've edit my question to show schema.rb and link controller:

schema.rb links

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

EDIT 2

I've edited some previous paragraphs. Now I can show the distinct values from links cities in a dropdown and it redirects to the filtered homepage, but I want the cities filtered by a country dropdown.

  • Please show what is inside `@links` variable. – Roman Kiselenko Sep 22 '18 at 08:29
  • Hey! Please update your question and try to explain the user / link relation better. If you can, post your ActiveRecord, e.g the Schema.rb file, so that we can understand better what you try to accomplish. I understood the user / links relation but have problems understanding what the registered (e.g confirmed?!) links and country, states etc. data part plays inside your app. Especially if you want to display those. I'm pretty we'll find a solution, if you could go more into detail and post beside the schema.file also your controller. Greetings! – Prometheus Sep 22 '18 at 13:47
  • Thanks! I've edited my question with schema.rb and the links controller. – Ionã Matheus Sep 22 '18 at 14:54
  • Did you tried distinct near the uniq? for distinction a country values? like here in samples: https://stackoverflow.com/questions/9658881/rails-select-unique-values-from-a-column/22515143 – Nezir Sep 22 '18 at 15:05
  • 1
    Thanks @Nezir, I've edited my question. The cities dropdown is working but I need to filter cities by country. – Ionã Matheus Sep 23 '18 at 23:55
  • @Prometheus , thanks! I've updated my question. – Ionã Matheus Sep 23 '18 at 23:55
  • @Зелёный I've updated my question – Ionã Matheus Sep 23 '18 at 23:56
  • @IonãMatheus okay, I dont have time to write for you the sample of code but this will be more profitable to you: check this video and I hope you will get example how to filter cities: https://www.youtube.com/watch?v=j1zZ4Lgzf9s or the other videos on this specific topic https://www.google.com/search?q=rails+casts+dropdown+filter&oq=rails+casts+dropdown+filter&aqs=chrome..69i57.10739j0j7&sourceid=chrome&ie=UTF-8 – Nezir Sep 24 '18 at 11:29

0 Answers0