减少导轨中的 url 长度
[英] reducing url length in rails
本文介绍了减少导轨中的 url 长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这个表格:
全局数据管理</h1></br></br><h2>输入条件,点击查找"选项,根据条件搜索用户.</br>或单击列表"选项以列出所有数据.</br></br></br><%= form_for(:find_field, url: find_field_path , method: :get) do |f|%><div class="row"><div class="span8 offset1"><div类=行"><div class = "span4"><div class="field"><%= f.label :data_type_choice, "数据类型" %><%= f.select :data_type_choice, [["all","all"],["2D","2D"],["3D","3D"],["2D3C","2D3C"],["3D3C","3D3C"]] , :name =>零%>
</br></br><h3>在</h3>内完成:</br><div class="field"><%= f.label :from_date_choice , "起始日期" %><%= f.date_select :from_date_choice , :name =>零%>
<div class = "span4"><div class="field"><%= f.label :basin_choice, "盆地" %><%= f.select :basin_choice, [["all","all"],["Cauvery","Cauvery"],["KG-PG","KG-PG"],["others","其他"]] , :name =>零%>
</br></br><h3> </h3><div class="field"><%= f.label :to_date_choice, "截止日期" %><%= f.date_select :to_date_choice , :name =>零%>
</br></br><%= f.submit " Search ", class: "btn btn-large btn-primary" %>
<%结束%>
一切正常,所有参数都正确传递等等,但在我试图显示结果的视图中,我得到一个像这样的巨大网址:
http://localhost:3000/global/data/find?utf8=%E2%9C%93&find_field%5Bdata_type_choice%5D=2D&find_field%5Bfrom_date_choice%281i%29%5D=2012&find_field%5Bfrom_date_choice%282i%29%5D=7&find_field%5Bfrom_date_choice%283i%29%5D=9&find_field%5Bbasin_choice%5D=KG-PG&find_field%5Bto_date_choice%281i%209%5D=9&find_field%5Bbasin_choice%5D=KG-PG&find_field%5Bto_date_choice%281i%209%2D%29%5D=7&find_field%5Bto_date_choice%283i%29%5D=9&commit=++Search++
我尝试使用:name => nil",如此railscast一>为什么会发生这种情况,我如何将 url 大小减少到http://localhost:3000/global/data/find"?
这是我在 fields_controller 中的 find 方法:
def find@data_type_choice = params[:find_field][:data_type_choice]@basin_choice = params[:find_field][:basin_choice]@from_date_choice = params[:find_field][:from_date_choice]@to_date_choice = params[:find_field][:to_date_choice]结尾
我还想通过 find.html.erb 显示表单中输入的数据,如下所示:
<%= @data_type_choice %>dsfgdfsg</h1>
如果我将其设为发布请求,则不会呈现 erb!我能做什么?
谢谢:)
解决方案
只需将您的方法从 GET 更改为 POST 并相应地更改路由.
试试
<%= form_for(:find_field, url: find_field_path , method: :post) do |f|%>
您的路由文件应与 POST 请求匹配.
i have this form :
<h1> Global data management </h1> </br> </br>
<h2>Enter the conditions and click "find" option to search for users based on conditions. </br> Or click the "List" option to list all the data. </h2>
</br></br></br>
<%= form_for(:find_field, url: find_field_path , method: :get) do |f| %>
<div class="row">
<div class="span8 offset1">
<div class = "row">
<div class = "span4">
<div class="field">
<%= f.label :data_type_choice, "Data type" %>
<%= f.select :data_type_choice, [["all","all"],["2D","2D"],["3D","3D"],["2D3C","2D3C"],["3D3C","3D3C"]] , :name => nil%>
</div>
</br></br>
<h3> COMPLETED WITHIN </h3>:</br>
<div class="field">
<%= f.label :from_date_choice , "From date " %>
<%= f.date_select :from_date_choice , :name => nil%>
</div>
</div>
<div class = "span4">
<div class="field">
<%= f.label :basin_choice, "Basin" %>
<%= f.select :basin_choice, [["all","all"],["Cauvery","Cauvery"],["KG-PG","KG-PG"],["others","others"]] , :name => nil %>
</div>
</br></br>
<h3> </h3>
<div class="field">
<%= f.label :to_date_choice , "To date " %>
<%= f.date_select :to_date_choice , :name => nil %>
</div>
</div>
</div>
</br></br>
<%= f.submit " Search ", class: "btn btn-large btn-primary" %>
</div>
</div>
<% end %>
everything works perfectly, all the parameters are passed correctly etc, but in the view where im trying to display the result, i get a HUGE url like so:
http://localhost:3000/global/data/find?utf8=%E2%9C%93&find_field%5Bdata_type_choice%5D=2D&find_field%5Bfrom_date_choice%281i%29%5D=2012&find_field%5Bfrom_date_choice%282i%29%5D=7&find_field%5Bfrom_date_choice%283i%29%5D=9&find_field%5Bbasin_choice%5D=KG-PG&find_field%5Bto_date_choice%281i%29%5D=2012&find_field%5Bto_date_choice%282i%29%5D=7&find_field%5Bto_date_choice%283i%29%5D=9&commit=++Search++
ive tried using the ":name => nil" as shown in this railscast
why is this happening and how can i reduce the url size to just "http://localhost:3000/global/data/find" ?
EDIT:
this is my find method in fields_controller:
def find
@data_type_choice = params[:find_field][:data_type_choice]
@basin_choice = params[:find_field][:basin_choice]
@from_date_choice = params[:find_field][:from_date_choice]
@to_date_choice = params[:find_field][:to_date_choice]
end
i also want to display the data entered in the form through find.html.erb like so:
<h1><%= @data_type_choice %> dsfgdfsg</h1>
if i make it a post request, the erb is not being rendered! what can i do?
thanks :)
解决方案
Just change your method from GET to POST and change the routes accordingly.
Try
<%= form_for(:find_field, url: find_field_path , method: :post) do |f| %>
Your routing file should match the request as POST.
这篇关于减少导轨中的 url 长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文