rails 3 routing question (Routing Error "No route matches …")
I'm using ruby 1.9.2 and rails 3 and I think I'm having a moderately simple problem with routing.
I have a model called AdvancedQuery
.
Its controller is AdvancedQueriesController
. Almost 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).
Here is the relevant portion of my routes.rb file
get "advanced_query" => "advanced_queries#new", as: :new_advanced_query
post "advanced_query(/:hash_value)(/:page)" => "advanced_queries#create", as: :create_advanced_query
get "advanced_query/:hash_value(/:page)" => "advanced_queries#search", as: :advanced_query_search
Here is the behavior that I expect when working with AdvancedQuery:
- User goes to http://localhost:3000/advanced_query (get request) and the browser invokes the "new" method in
advanced_queries_controller
.new.html.haml
is rendered which shows the user a standard form to fill out. - User then enters data into the search form and presses "Submit"
- "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) i开发者_如何学JAVAs used as part of the resulting URL (see step 4).
- 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
search.html.haml
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: http://localhost:3000/advanced_query/blah (where blah is a 32-character hash that's specifically associated with the query). - 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 & 4). i.e. Create a new AdvancedQuery.
Here's what happens in reality:
Steps 1 - 4 work as expected. Step 5 gives me a Routing Error "No route matches "/advanced_query"
Both the new.html.haml
and the search.html.haml
files render the same partial (called _form.html.haml
).
So, if I look at _form.html.haml
, I don't really see anything wrong:
= form_for(@advanced_query, url: create_advanced_query_path) do |f|
.actions
# other generic form-related stuff
Here is the relevant portion of my controller
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
Any thoughts on what's causing my routing error?
Thanks!
It looks like your parentheses in routes.rb might not be matched correctly - do you mean to have
post "advanced_query(/:hash_value(/:page))" => "advanced_queries#create", as: :create_advanced_query
instead of
post "advanced_query(/:hash_value)(/:page)" => "advanced_queries#create", as: :create_advanced_query
?
Section 3 here: http://guides.rubyonrails.org/routing.html might be able to help; it's not entirely clear what you're looking to pass in.
Your routes file should read:
get "/advanced_query" => "advanced_queries#new", :as => :new_advanced_query
post "/advanced_query(/:hash_value(/:page))" => "advanced_queries#create", :as => :create_advanced_query
get "/advanced_query/:hash_value(/:page)" => "advanced_queries#search", :as => :advanced_query_search
So, I figured out the error, although I'm not quite sure why the error is taking place.
Turns out that even though the form rendered on search.html.haml is listed as "POST", and even though the log-file says that it's a POST, it turns out that it's a PUT!
so, if I modify my routes.rb file like this:
get "advanced_query" => "advanced_queries#new", as: :new_advanced_query
post "advanced_query(/:hash_value(/:page))" => "advanced_queries#create", as: :create_advanced_query
put "advanced_query(/:hash_value(/:page))" => "advanced_queries#create", as: :create_advanced_query
get "advanced_query/:hash_value(/:page)" => "advanced_queries#search", as: :advanced_query_search
Then there is no Routing Error.
精彩评论