on my root page, I have an index action that lists everything from the images model.
in the index action there are 3 filters you can apply to the list that will only show whatever matches the filter criteria. These parameters show up in the URL like this when the select boxes are selected and filter is clicked:
http://localhost:3000/gender=m&height=62&weight=130
How can I make it so, the URL will rewrite to:
http://localhost:3000/m/62/130/
These values are attributes from the images model, stored in the gender, height and weight columns. or, better yet, rewrite to something like this
http://localhost:3000/men/62-inches/130-lbs/
Here's what I tried, putting this in the routes: match "/:gender/:height/:weight", :controller => "images", :action => "index", :conditions => { :method => :get }, via: 'get'
model:
class Image < ActiveRecord::Base
validates :gender, :measure_type, presence: true
validates :image_location, presence: true, uniqueness: true
validates :weight, length: {maximum: 3}
has_attached_file :photo, styles: {
small: '240x300'
}
end
my routes.rb:
Images::Application.routes.draw do
# match "/:gender/:height/:weight", :controller => "images", :action => "index", :conditions => { :method => :get }
get 'images', to: redirect('/')
match 'upload' => 'images#new', :as => 'new_image_path', via: 'get'
match '/upload', :to => 'images#create', :via => :post, :as => :post_upload
root "images#index"
end
index.html.erb
<h1>Images</h1>
<%= form_tag root_path, method: :get do %>
<%= select_tag "gender", options_for_select(["m", "w"], params[:gender]) %>
<%= select_tag "height", options_for_select((60..70).to_a.map{|s| ["#{s} inches", s]}, selected: params[:height]), :prompt => "all" %>
<%= select_tag "weight", options_for_select((100..300).step(5).to_a.map{|s| ["#{s} lbs", s]}, selected: params[:weight]), :prompt => "all" %>
<%= submit_tag 'Filter', name: nil %>
<% end %>
<table>
<tr>
<th>Gender</th>
<th>Height</th>
<th>Weight</th>
<th>Image location</th>
</tr>
<% @images.each do |image| %>
<tr>
<td><%= image.gender %></td>
<td><%= image.height %></td>
<td><%= image.weight %></td>
<td><%= image.image_location %></td>
</tr>
<% end %>
</table>
images_controller.rb
class ImagesController < ApplicationController
def index
@images= Image.where(gender: params[:gender], height: params[:height],
weight: params[:weight])
end
end
Update 1:
I'm starting to think it might involve doing something in the routes like get '/', to: redirect('/params[gender]/params[height]/params[weight]')
Update 2:
I think I solved this, found the answer after hours searching in this post- Ruby on Rails: How to redirect page based on post params in search_field?