Note that there are some explanatory texts on larger screens.

plurals
  1. POrails 3 routing question (Routing Error "No route matches …")
    text
    copied!<p>I'm using ruby 1.9.2 and rails 3 and I think I'm having a moderately simple problem with routing.</p> <p>I have a model called <code>AdvancedQuery</code>. Its controller is <code>AdvancedQueriesController</code>. <em>Almost</em> everything is done in the standard rails way except for the routing. I wanted to change the names of the URLs and I wanted to change a few other things (see below).</p> <p>Here is the relevant portion of my routes.rb file</p> <pre><code>get "advanced_query" =&gt; "advanced_queries#new", as: :new_advanced_query post "advanced_query(/:hash_value)(/:page)" =&gt; "advanced_queries#create", as: :create_advanced_query get "advanced_query/:hash_value(/:page)" =&gt; "advanced_queries#search", as: :advanced_query_search </code></pre> <p>Here is the behavior that I <strong>expect</strong> when working with AdvancedQuery:</p> <ol> <li>User goes to <a href="http://localhost:3000/advanced_query" rel="nofollow">http://localhost:3000/advanced_query</a> (get request) and the browser invokes the "new" method in <code>advanced_queries_controller</code>. <code>new.html.haml</code> is rendered which shows the user a standard form to fill out.</li> <li>User then enters data into the search form and presses "Submit"</li> <li>"Submit" invokes the "create" method and creates an "AdvancedQuery" record in the database. The AdvancedQuery object has a 32-character hash associated with it that 1) identifies the query and 2) is used as part of the resulting URL (see step 4).</li> <li>The create method redirects to the "search" method where the AdvancedQuery object is used to search a 2nd model (called BusinessModel). The server then renders <code>search.html.haml</code> then shows the results of the AdvancedQuery and it re-renders the original form on the same page as the results in case the user wants to run a new search. The URL generated here is: <a href="http://localhost:3000/advanced_query/blah" rel="nofollow">http://localhost:3000/advanced_query/blah</a> (where blah is a 32-character hash that's specifically associated with the query).</li> <li>Now the user enters a new search term using the form from the web-page generated in Step 3. He presses "submit" and the "create" method should be invoked again (i.e. we re-do Steps 3 &amp; 4). i.e. Create a new AdvancedQuery.</li> </ol> <p>Here's what happens in reality:</p> <p>Steps 1 - 4 work as expected. Step 5 gives me a Routing Error <strong><code>"No route matches "/advanced_query"</code></strong></p> <p>Both the <code>new.html.haml</code> and the <code>search.html.haml</code> files render the same partial (called <code>_form.html.haml</code>).</p> <p>So, if I look at <code>_form.html.haml</code>, I don't really see anything wrong:</p> <pre><code>= form_for(@advanced_query, url: create_advanced_query_path) do |f| .actions # other generic form-related stuff </code></pre> <p>Here is the relevant portion of my controller</p> <pre><code> def new @advanced_query = AdvancedQuery.new end def create advanced_query = AdvancedQueryBuilder.build_advanced_query_from_post(request, params, current_user) redirect_to(advanced_query_search_path(hash_value: advanced_query.hash_value)) end def search return render :bad_request unless request.get? @advanced_query = AdvancedQuery.find_by_hash_value_and_user_id(params[:hash_value], current_user.id) @results = BusinessModel.advanced_search(@advanced_query) end </code></pre> <p>Any thoughts on what's causing my routing error?</p> <p>Thanks!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload