使用Rails 5,如何创建提交RESTful操作的表单?
在我的路线文件中,我有:
resources :people do
collection do
get 'image/:id', :to => "people#image", :as => 'image'
get 'ranks', :to => "people#ranks", :as => 'ranks'
get 'search/:search(.:format)', to: 'people#search'
end
end
所以我想知道如何构建一个提交“搜索”动作的表单。
我尝试了以下内容:
<%= form_tag(people_search_path) do %>
<%= text_field_tag :search %>
<%= submit_tag 'Search' %>
<% end %>
但是这会导致以下错误:
undefined local variable or method `people_search_path' for #<#<Class:0x00007ff2afc59168>:0x00007ff2afc50f68>
答案 0 :(得分:1)
如果您MY_STRING.format
,则会看到您的搜索路径没有名称:
format_name = partial(str.format, age=30)
print format_name(MY_STRING, name='sameer')
# sameer30
print format_name('another string with {name} and {age}', name='sameer')
# another string with sameer and 30
相反,如果你这样做:
rake routes
您会看到您的路径现在有一个名称:
image_people GET /people/image/:id(.:format) people#image
ranks_people GET /people/ranks(.:format) people#ranks
GET /people/search/:search(.:format) people#search
people GET /people(.:format) people#index
POST /people(.:format) people#create
new_person GET /people/new(.:format) people#new
edit_person GET /people/:id/edit(.:format) people#edit
person GET /people/:id(.:format) people#show
PATCH /people/:id(.:format) people#update
PUT /people/:id(.:format) people#update
DELETE /people/:id(.:format) people#destroy
您可以将其用作resources :people do
collection do
get 'image/:id', :to => "people#image", :as => 'image'
get 'ranks', :to => "people#ranks", :as => 'ranks'
get 'search/:search(.:format)', to: 'people#search', as: :search
end
end
。
image_people GET /people/image/:id(.:format) people#image
ranks_people GET /people/ranks(.:format) people#ranks
search_people GET /people/search/:search(.:format) people#search
people GET /people(.:format) people#index
POST /people(.:format) people#create
new_person GET /people/new(.:format) people#new
edit_person GET /people/:id/edit(.:format) people#edit
person GET /people/:id(.:format) people#show
PATCH /people/:id(.:format) people#update
PUT /people/:id(.:format) people#update
DELETE /people/:id(.:format) people#destroy
位是不必要的,您可以这样做:
search_people_path
但如果我是你,我会这样做:
(.:format)
哪个会给你:
post 'search/:search', to: 'people#search', as: :search
然后,我会按照Anees Muhammed的建议更新您的表单(我使用resources :people do
collection do
get 'image/:id', :to => "people#image", :as => 'image'
get 'ranks', :to => "people#ranks", :as => 'ranks'
get :search, to: 'people#search', as: :search
end
end
代替 image_people GET /people/image/:id(.:format) people#image
ranks_people GET /people/ranks(.:format) people#ranks
search_people GET /people/search(.:format) people#search
people GET /people(.:format) people#index
POST /people(.:format) people#create
new_person GET /people/new(.:format) people#new
edit_person GET /people/:id/edit(.:format) people#edit
person GET /people/:id(.:format) people#show
PATCH /people/:id(.:format) people#update
PUT /people/:id(.:format) people#update
DELETE /people/:id(.:format) people#destroy
):
:terms
然后,当你提交时,你应该得到类似的东西:
:search
您可以通过<%= form_tag search_people_path, method: :get do %>
<%= text_field_tag :terms %>
<%= submit_tag 'Search' %>
<% end %>
访问您的条款。
对此有BTW,没有“非RESTful”。 IMO完全一致the Guide用于添加RESTful操作。
答案 1 :(得分:0)
您应该使用search_people_path
代替people_search_path
。但我不认为改变会带你到你想要达到的目标。由于您的路线为get 'search/:search(.:format)', to: 'people#search'
,因此我们希望您为关键字:search
提供值,以构建类似/people/search/yoursearchterm
的网址search_people_path(yoursearchterm)
}。要使您的功能正常工作,请将表单更改为
<%= form_tag search_people_path, method: :get do %>
<%= text_field_tag :search %>
<%= submit_tag 'Search' %>
<% end %>
然后在您的控制器中
def search
params[:search]
end
还有到
get 'search', to: 'people#search'
已更新
由于您尝试将表单直接提交到search/:search
这样的休息路径,我相信没有直接的方法来实现相同的目标,因为表单的工作方式。但是,如果您在提交表单时只需要yourwebsite.com/search/yoursearchterm
这样的休息路径,则可以通过一些方法来实现此目的。您必须在提交表单时写一段js
并使用输入值将请求转发到您的路线,或者您可以在控制器中对路线执行重定向,如下所示。
路线:
resources :people do
collection do
get 'search', to: 'people#search_people'
get 'search/:search', to: 'people#search'
end
end
因此,首先将表单提交给search_people
操作,然后重定向到使用params进行搜索。在控制器中
def search_people
redirect_to search_people_path(params[:search])
end
这会为您提供一个网址yoursite.com/people/search/searchterm
,然后在您的search
操作中,您可以使用搜索参数。
希望这有帮助。