Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6229723
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T09:37:19+00:00 2026-05-24T09:37:19+00:00

Having one of those ‘I’m clearly doing something wrong’ moments. It feels like I’m

  • 0

Having one of those ‘I’m clearly doing something wrong’ moments. It feels like I’m trying to do something basic, and fighting the framework, so I’m appealing for help!

I’m using Rails 3 and am a bit confused about how to make a search form that results in pages with clean URLs.

My application allows you to search from routes from any location to another location.

For example, a valid URL would be /routes/A/to/B/, or /routes/B

My routes.rb:

match 'routes/:from/to/:to' => 'finder#find', :as => :find
match 'routes/find' => 'finder#find'

My search form:

<% form_tag('/routes, :method => 'post') do %>
  <%= label_tag(:from, "From:") %>
  <%= text_field_tag(:from) %>
  <%= label_tag(:to, "To:") %>
  <%= text_field_tag(:to) %>
  <%= submit_tag("Go") %>
<% end %>

Controller:

class FinderController < ApplicationController
  def index
  end

  def find
    if params[:from].blank? or params[:to].blank?
      render :action => "invalid_results" and return
    end
    @from = Location.find_by_code(params[:from].upcase)
    @to = Location.find_by_code(params[:to].upcase)
    if @from.nil? or @to.nil?
      render :action => "invalid_results" and return
    end

    @routes = Route.find_all_by_from_location_id_and_to_location_id(@from, @to)

  end
end

When I use :method => 'get' in my form_tag, the application works, but the URL is hideous. And, of course, with :method => 'post', the variables are no longer visible which is bad for bookmarking. How do I tell Rails to use my pretty URLs after POSTing the form?

I am VERY new at Rails so your patience is appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T09:37:20+00:00Added an answer on May 24, 2026 at 9:37 am

    Your routes are given an automatic named path that you can see by typing rake routes. For example:

    new_flag GET    /flags/new(.:format)      {:action=>"new", :controller=>"flags"}
    

    You can refer to the path by using new_flag_path or new_flag_url

    Your form_tag entry is kinda screwy. Instead of having a separate find method you could also use the index method, but that’s your choice.

    You may find it easier to use a standard redirect_to to redirect to a prettier URL based on inputs. If you don’t want the redirect, then you’ll need to use jQuery to change your form’s action method dynamically. Searches typically use ugly GET parameters.

    So I would change your code to look like this:

    routes.rb

    get 'routes/:from/to/:to' => 'finder#routes', :as => :find_from_to
    post 'routes/find' => 'finder#find', :as => :find
    

    _form.html.erb

    <% form_tag find_path, :method => :post do %>
      <%= label_tag(:from, "From:") %>
      <%= text_field_tag(:from) %>
      <%= label_tag(:to, "To:") %>
      <%= text_field_tag(:to) %>
      <%= submit_tag("Go") %>
    <% end %>
    

    finder_controller.rb

    class FinderController < ApplicationController
      def index
      end
    
      def find
        if params[:from].blank? or params[:to].blank?
          render :action => "invalid_results" and return
        end
        @from = Location.find_by_code(params[:from].upcase)
        @to = Location.find_by_code(params[:to].upcase)
        if @from.nil? or @to.nil?
          render :action => "invalid_results" and return
        end
    
        redirect_to find_from_to_path(@from, @to)
    
      end
    
      def routes
         @routes = Route.find_all_by_from_location_id_and_to_location_id(params[:from], params[:to])
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am having one heck of a hard time trying to figure this out.
I'm having one hell of a time trying to get my databinding to work
I'm having one of those bizarre this used to work and then it stopped
I am having one of those nasty works in debug but not in release
I'm having one of those days.... Here's my class: /// <summary> /// Represent a
I am trying to design an edifact parser, I was planning on having one
I'm having one of those procrastination overstimulated panic attacks about choosing a method of
I'm getting kind of confused right now, having one of those days I guess.
Before I get pointed to one of those 'VS.' questions like below... ASP.NET webforms
Having one of those days, I can't get my head around what is going

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.