首页 \ 问答 \ Ruby on Rails - 将文本字段与选择结合?(Ruby on Rails - Combine Text Field with Select?)

Ruby on Rails - 将文本字段与选择结合?(Ruby on Rails - Combine Text Field with Select?)

我想要一个文本字段,当用户按下它时,会出现一些可能的选项,如带选项(或datalist)的下拉菜单。 像这样的东西:

在这里输入图像描述

有没有任何宝石或图书馆可以为我做这件事? 还是有一个很好的例子,如何正确地做到这一点? 我尝试过搜索,但也许我没有提出正确的问题,因为我找不到太多东西......

编辑:

关于我的项目:

我正在做一个汽车网站,所以我需要按价格,年份,公里等过滤汽车......这就是为什么我需要一个用户可以放置的文本字段,例如,他们的价格范围,但同时我想要显示一些他们可以选择的选项。 这些选项是静态的,它们不是来自任何模型。

然后,我将使用:price_start和:price_ending来执行named_scope过滤数据。

这是我的模特:

create_table "vehicles", force: :cascade do |t|
  t.integer "kms"
  t.integer "power"
  t.string "transmission"
  t.string "fuel"
  t.string "category"
  t.integer "seats"
  t.integer "doors"
  t.string "color"
  t.string "condition"
  t.string "warranty"
  t.datetime "created_at", null: false
  t.datetime "updated_at", null: false
  t.integer "model_id"
  t.integer "manufacturer_id"
  t.float "cc"
  t.string "version"
  t.integer "price"
  t.integer "month"
  t.integer "year"
  t.index ["manufacturer_id"], name: "index_vehicles_on_manufacturer_id"
  t.index ["model_id"], name: "index_vehicles_on_model_id"
end

我的指数:

<%= form_tag(filter_vehicles_path, method: 'get') do %>
<h4><strong>

  <span>Fabricante</span>
  <%= collection_select :manufacturer, :manufacturer_id, Manufacturer.joins(:vehicles).uniq, :id, :name, {include_blank: true}, {class: "manufacturer_dropdown"} %>

  <span>Modelo</span>
  <%= grouped_collection_select :model, :model_id, Manufacturer.order(:name), :models, :name,  :id, :name, {include_blank: true}, {class: "model_dropdown", data: {models: @models}} %>

  <span>Preço</span>
  <%= select_tag :price_start, options_for_select([ "250 EUR", "500 EUR", "1000 EUR", "2000 EUR", "3000 EUR", "4000 EUR", "5000 EUR", "6000 EUR", "7000 EUR", "8000 EUR", "9000 EUR", "10 000 EUR", "12 000 EUR", "14 000 EUR", "16 000 EUR", "18 000 EUR", "20 000 EUR", "22 000 EUR", "24 000 EUR", "26 000 EUR", "28 000 EUR", "30 000 EUR", "32 000 EUR", "34 000 EUR", "36 000 EUR", "38 000 EUR", "40 000 EUR", "50 000 EUR", "60 000 EUR", "70 000 EUR", "80 000 EUR", "90 000 EUR", "100 000 EUR", "150 000 EUR", "200 000 EUR"]), prompt: "--de--" %>
  <%= select_tag :price_ending, options_for_select([ "250 EUR", "500 EUR", "1000 EUR", "2000 EUR", "3000 EUR", "4000 EUR", "5000 EUR", "6000 EUR", "7000 EUR", "8000 EUR", "9000 EUR", "10 000 EUR", "12 000 EUR", "14 000 EUR", "16 000 EUR", "18 000 EUR", "20 000 EUR", "22 000 EUR", "24 000 EUR", "26 000 EUR", "28 000 EUR", "30 000 EUR", "32 000 EUR", "34 000 EUR", "36 000 EUR", "38 000 EUR", "40 000 EUR", "50 000 EUR", "60 000 EUR", "70 000 EUR", "80 000 EUR", "90 000 EUR", "100 000 EUR", "150 000 EUR", "200 000 EUR"]), prompt: "--até--" %>

  <span>Ano</span>
  <%= select_tag :year_start, options_for_select(["1 990", "1 980", "1 970", "1 960", "1 950", "1 940", "1 930", "1 920", "1 910", "1 900"]), prompt: "--de--" %>
  <%= select_tag :year_start, options_for_select(["1 990", "1 980", "1 970", "1 960", "1 950", "1 940", "1 930", "1 920", "1 910", "1 900"]), prompt: "--até--" %>

  <span>Quilómetros</span>
  <%= select_tag :kms_start, options_for_select([ "5 000 km", "10 000 km", "15 000 km", "20 000 km", "25 000 km", "30 000 km", "35 000 km", "50 000 km", "75 000 km", "100 000 km", "125 000 km", "150 000 km", "200 000 km", "250 000 km", "300 000 km", "350 000 km", "400 000 km", "450 000 km", "500 000 km"]), prompt: "--de--" %>
  <%= select_tag :kms_end, options_for_select([ "5 000 km", "10 000 km", "15 000 km", "20 000 km", "25 000 km", "30 000 km", "35 000 km", "50 000 km", "75 000 km", "100 000 km", "125 000 km", "150 000 km", "200 000 km", "250 000 km", "300 000 km", "350 000 km", "400 000 km", "450 000 km", "500 000 km"]), prompt: "--até--" %>

  <%= submit_tag "Pesquisar" %>
</strong></h4>

我会感谢你的帮助。 提前致谢!


I want a text field that when the user presses it, appears some possible options, like a dropdown with options (or datalist). Something like this:

enter image description here

Is there any gem or library that can do this for me? Or is there a good example how to do this properly? I've tried to seach but maybe I'm not making the right question beacause I can't find much stuff...

EDIT:

About my project:

I'm doing a car website, so I need to filter the cars by price, year, kilometers, etc...That's why I need a text field that users can put, for example, their price range but as the same time I want to show some options that they can select too. These options are static, they dont came from any model.

I will then use the :price_start and :price_ending to do a named_scope to filter the data.

Here is my model:

create_table "vehicles", force: :cascade do |t|
  t.integer "kms"
  t.integer "power"
  t.string "transmission"
  t.string "fuel"
  t.string "category"
  t.integer "seats"
  t.integer "doors"
  t.string "color"
  t.string "condition"
  t.string "warranty"
  t.datetime "created_at", null: false
  t.datetime "updated_at", null: false
  t.integer "model_id"
  t.integer "manufacturer_id"
  t.float "cc"
  t.string "version"
  t.integer "price"
  t.integer "month"
  t.integer "year"
  t.index ["manufacturer_id"], name: "index_vehicles_on_manufacturer_id"
  t.index ["model_id"], name: "index_vehicles_on_model_id"
end

My index:

<%= form_tag(filter_vehicles_path, method: 'get') do %>
<h4><strong>

  <span>Fabricante</span>
  <%= collection_select :manufacturer, :manufacturer_id, Manufacturer.joins(:vehicles).uniq, :id, :name, {include_blank: true}, {class: "manufacturer_dropdown"} %>

  <span>Modelo</span>
  <%= grouped_collection_select :model, :model_id, Manufacturer.order(:name), :models, :name,  :id, :name, {include_blank: true}, {class: "model_dropdown", data: {models: @models}} %>

  <span>Preço</span>
  <%= select_tag :price_start, options_for_select([ "250 EUR", "500 EUR", "1000 EUR", "2000 EUR", "3000 EUR", "4000 EUR", "5000 EUR", "6000 EUR", "7000 EUR", "8000 EUR", "9000 EUR", "10 000 EUR", "12 000 EUR", "14 000 EUR", "16 000 EUR", "18 000 EUR", "20 000 EUR", "22 000 EUR", "24 000 EUR", "26 000 EUR", "28 000 EUR", "30 000 EUR", "32 000 EUR", "34 000 EUR", "36 000 EUR", "38 000 EUR", "40 000 EUR", "50 000 EUR", "60 000 EUR", "70 000 EUR", "80 000 EUR", "90 000 EUR", "100 000 EUR", "150 000 EUR", "200 000 EUR"]), prompt: "--de--" %>
  <%= select_tag :price_ending, options_for_select([ "250 EUR", "500 EUR", "1000 EUR", "2000 EUR", "3000 EUR", "4000 EUR", "5000 EUR", "6000 EUR", "7000 EUR", "8000 EUR", "9000 EUR", "10 000 EUR", "12 000 EUR", "14 000 EUR", "16 000 EUR", "18 000 EUR", "20 000 EUR", "22 000 EUR", "24 000 EUR", "26 000 EUR", "28 000 EUR", "30 000 EUR", "32 000 EUR", "34 000 EUR", "36 000 EUR", "38 000 EUR", "40 000 EUR", "50 000 EUR", "60 000 EUR", "70 000 EUR", "80 000 EUR", "90 000 EUR", "100 000 EUR", "150 000 EUR", "200 000 EUR"]), prompt: "--até--" %>

  <span>Ano</span>
  <%= select_tag :year_start, options_for_select(["1 990", "1 980", "1 970", "1 960", "1 950", "1 940", "1 930", "1 920", "1 910", "1 900"]), prompt: "--de--" %>
  <%= select_tag :year_start, options_for_select(["1 990", "1 980", "1 970", "1 960", "1 950", "1 940", "1 930", "1 920", "1 910", "1 900"]), prompt: "--até--" %>

  <span>Quilómetros</span>
  <%= select_tag :kms_start, options_for_select([ "5 000 km", "10 000 km", "15 000 km", "20 000 km", "25 000 km", "30 000 km", "35 000 km", "50 000 km", "75 000 km", "100 000 km", "125 000 km", "150 000 km", "200 000 km", "250 000 km", "300 000 km", "350 000 km", "400 000 km", "450 000 km", "500 000 km"]), prompt: "--de--" %>
  <%= select_tag :kms_end, options_for_select([ "5 000 km", "10 000 km", "15 000 km", "20 000 km", "25 000 km", "30 000 km", "35 000 km", "50 000 km", "75 000 km", "100 000 km", "125 000 km", "150 000 km", "200 000 km", "250 000 km", "300 000 km", "350 000 km", "400 000 km", "450 000 km", "500 000 km"]), prompt: "--até--" %>

  <%= submit_tag "Pesquisar" %>
</strong></h4>

I will appreciate your help. Thanks in advance!


原文:https://stackoverflow.com/questions/50645095
更新时间:2023-10-12 09:10

最满意答案

我在我的本地试过上面的例子,发现你缺少一个名为的http头

Content-Type: application/json

还要确保在请求正文中传递正确的json字符串

{"Name" : "test", "Address" : "test"}

上面的技巧将帮助你摆脱400坏请求。


I have tried above example in my local and found that you are missing one http header called

Content-Type: application/json

Also be sure you pass right json string in request body

{"Name" : "test", "Address" : "test"}

above trick will help you to get rid of 400 bad request.

相关问答

更多
  • 我正在使用WCF服务来完成一些工作,然后调用另一个WCF服务。 这是第二项服务有上述问题。 结果问题是第一项服务如何实施。 解决方案是在这里: http : //social.msdn.microsoft.com/Forums/en-US/wcf/thread/03a2b109-c400-49d4-891e-03871ae0d083/#416d8cbc-b855-46aa-8a6d-5d6b09db97b6 I was using a WCF service to do some work and then ...
  • 你的合同应该是这样的 - [OperationContract] [WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json , UriTemplate="/post")] string PostNewOrder(RootObject OrderData); RootObject应该是什么样子 - publi ...
  • 使用上面的配置,如果我导航到http:// localhost:12345 / Service1.svc / InsertData / name / surname我得到错误“找不到方法” 当您通过浏览器执行此操作时,您实际上正在执行GET请求,而您的方法不支持该请求。 当你通过Fiddler这样做时,你发出一个POST。 您可以使用WebRequest类或HttpRequest通过代码执行POST。 Using the above configuration if i navigate to http:/ ...
  • 我在我的本地试过上面的例子,发现你缺少一个名为的http头 Content-Type: application/json 还要确保在请求正文中传递正确的json字符串 {"Name" : "test", "Address" : "test"} 上面的技巧将帮助你摆脱400坏请求。 I have tried above example in my local and found that you are missing one http header called Content-Type: applic ...
  • WCF默认仅使用HTTP Post。 此链接说明如何选择使用HTTP Get https://msdn.microsoft.com/en-us/library/bb628610(v=vs.110).aspx Ok I found the problem. The problem was a bad url request. I wanted to request http://localhost:1478/GatewayJaguar/Alarms/Last but my program was listen ...
  • 请参阅第10.4.1.3节。 为OData 3 调用Action 。 短篇小说:内容类型必须是JSON。 如果调用请求包含任何非绑定参数值,则请求的Content-Type必须是'application / json',并且参数值必须在请求主体中的单个JSON对象中编码。 Please refer to Section 10.4.1.3. Invoking an Action for OData 3. Short story: The content type must be JSON. If the in ...
  • 你可以使用jQuery来做到这一点。 这里有一个很好的例子供你使用。 这是一个示例代码块。 $.ajax({ cache: false, type: "POST", async: false, url: /* YOUR URL */, data: JSON.stringify(/* YOUR POST DATA */), contentType: "application/json", dataType: "json", success: fu ...
  • 我已经尝试了上面的场景,并且几乎没有任何变化,我得到它如下所示: [OperationContract] [WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, UriTemplate = "postmethod/new")] Booking CreateBooking(Booking booking); 您可以删除WrappedRequest设置,因为您只有1个参数。 当我使用以下请求从Fid ...
  • 错误的请求响应是因为内容类型错误。 不得不从Webclient切换到httpWebRequest 然后我可以将ContentType设置为需要的"application/json" 。 我无法使用Webclient更改标题Content-Type 。 下面的代码有效: var httpWebRequest = (HttpWebRequest)WebRequest.Create("http://localhost:83/Ade.svc/post/address"); httpWebRequest.Conten ...
  • 我已经解决了我添加自定义WebContentTypeMapper的问题。 以下是我的示例编码: 创建新类以允许以RAW类型接收数据 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ServiceModel; using System.ServiceModel.Channels; namespace SampleArticle { public clas ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)