繁体   English   中英

Rails form_tag提交数据而无需重新加载整个页面

[英]Rails form_tag submit data without reload the whole page

我有一个称为搜索从数据库渲染数据的模型,我使用索引方法从3个模型访问数据,并将其显示在索引页面上。 我在索引页面本身中使用form_tag,

            <%= form_tag(search_path, :method => "get", :remote => true, id: "search-form") do %>
            <label for="Longitude ">Lon</label>
              <%= text_field_tag :lng, params[:lng], :id => "longitude", placeholder: "Longitude" %>
            <label for="Latitude ">Lat</label>
              <%= text_field_tag :lat, params[:lat], :id => "latitude", placeholder: "Latitude" %>

              <button class="btn waves-effect waves-light" >Process</button>


        <% end %>

我还没有为此创建任何局部视图。

我在form_tag中添加了:remote => true,我不知道在哪里以及如何为此调用ajax? 我正在从表中搜索数据,并使用'<%= escape_javascript @ center_lat.to_json%>'获取更新的数据; 在index.html.erb文件中

任何帮助将是可观的。

由于表单的remote选项设置为true,因此该请求将作为Ajax请求发布到您的控制器,以查找JavaScript。 为了满足该请求,控制器的操作应如下所示:

def MyController

  ...

  def my_action
    respond_to do |format|
      format.js { ... }
      format.html { ... }
    end
  end
  ...

format.js {...允许控制器响应您的Ajax请求。 因此,您需要呈现.js.js.erb

看看这里,将会更加启发您: http : //guides.rubyonrails.org/working_with_javascript_in_rails.html

您正在使用:method => "get"来发出获取请求,该页面将始终重新加载,请尝试将其删除并尝试是否可以运行。

另外,您的控制器需要能够应答JS调用,使您要在控制器上调用的动作具有如下所示的内容

respond_to do |f|
f.html { redirect_to tasks_url }
f.js

结束

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM