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 ⌄</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.