我正在使用 ruby 1.9.2 和 Rails 3,我认为我在路由方面遇到了一个相当简单的问题。
我有一个名为 AdvancedQuery
的模型。
它的控制器是AdvancedQueriesController。 几乎除了布线之外,所有事情都是以标准导轨方式完成的。我想更改 URL 的名称,还想更改其他一些内容(见下文)。
这是我的routes.rb 文件的相关部分
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
这是我在使用AdvancedQuery 时期望的行为:
- 用户转到http://localhost:3000/advanced_query(获取请求),浏览器调用
advanced_queries_controller
中的“new”方法。 new.html.haml
被呈现,向用户显示要填写的标准表单。
- 然后,用户将数据输入搜索表单并按“提交”。
- “提交”调用“创建”方法并在数据库中创建“AdvancedQuery”记录。 AdvancedQuery 对象有一个与之关联的 32 个字符的哈希,1) 标识查询,2) 用作结果 URL 的一部分(请参阅步骤 4)。
- create 方法重定向到“search”方法,其中 AdvancedQuery 对象用于搜索第二个模型(称为 BusinessModel)。然后,服务器呈现
search.html.haml
,然后显示 AdvancedQuery 的结果,并在与结果相同的页面上重新呈现原始表单,以防用户想要运行新的搜索。此处生成的 URL 为: http://localhost:3000/advanced_query/blah (其中 blah 是 32 - 与查询特定关联的字符哈希)。
- 现在,用户使用步骤 3 中生成的网页中的表单输入新的搜索词。他按下“提交”,并且应再次调用“创建”方法(即,我们重新执行步骤 3 和 4)。即创建一个新的 AdvancedQuery。
实际情况如下:
步骤 1 - 4 按预期工作。步骤 5 给我一个路由错误 “没有路由匹配“/advanced_query”
new.html.haml
和 搜索。 html.haml
文件呈现相同的部分(称为 _form.html.haml
),
因此,如果我查看 _form.html.haml
,我不会。真的没有看到任何错误:
= form_for(@advanced_query, url: create_advanced_query_path) do |f|
.actions
# other generic form-related stuff
这是我的控制器的相关部分对
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
导致我的路由错误的原因有什么想法吗
?
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) is 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!
发布评论
评论(3)
看起来你的routes.rb中的括号可能没有正确匹配 - 你的意思是用
而不是
?
第 3 部分:http://guides.rubyonrails.org/routing.html 可能能够帮助;目前尚不完全清楚您要传递的内容。
It looks like your parentheses in routes.rb might not be matched correctly - do you mean to have
instead of
?
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:
因此,我找出了错误,尽管我不太确定为什么会发生错误。
事实证明,即使在 search.html.haml 上呈现的表单被列为“POST”,并且即使日志文件表明它是 POST,但事实证明它是 PUT!
所以,如果我像这样修改routes.rb文件:
那么就没有路由错误。
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:
Then there is no Routing Error.