如何使用 rails 创建搜索框,以便我可以使用 $.ajax 方法动态获取数据
我的代码是:-
<%= form_for '/search' do |f| %>
<%= f.text_field :source, :class => 'form-control', id: 'from' %>
<%= f.submit %>
<% end %>
// source is city names
如何在此处调用 Ajax 以及它将如何呈现到控制器并返回结果,请告诉我这是如何工作的,我无法为此找到合适的插图
好的,也许这可以帮助您:
1(
# config/application.rb
class Application < Rails::Application
config.action_view.embed_authenticity_token_in_remote_forms = true
# [...]
end
2(
<%= form_for '/search', remote: true do |f| %>
<%= f.text_field :source, :class => 'form-control', id: 'from' %>
<%= f.submit %>
<% end %>
3( 在您的控制器操作中
def search
Model.where(name: params[:source])
respond_to do |format|
format.html { redirect_to '/search' }
format.js
end
end
4(然后创建搜索.js.erb
Your js here