[英]Is their any way?I could get the value of dropdown to use in controller using onchange event handler in ruby on rails application
I want to call a method using onchange event handler when a user changes the value on a dropdown.Below is the code for my dropdown box. 当用户更改下拉列表中的值时,我想使用onchange事件处理程序调用方法。以下是我的下拉框的代码。
<%= select_tag :name,options_for_select(@tests.map{|u| [u.name]}.uniq),:onchange => 'update_dropdown(:name)' %>
In the controller i want a method.which takes the value of selection in dropdown as paramater: 在控制器中,我想要一个method.method。它将下拉列表中的selection值作为参数:
Below code searches database for the document with value of the parameter given from view.And return the document.What are the changes i have to make to get the selection in the controller as the dropdown values are changed! 下面的代码使用视图中给出的参数值在数据库中搜索文档,然后返回文档。随着下拉列表值的更改,我需要进行哪些更改才能在控制器中进行选择!
def update_dropdown(name)
@drop = Test.where("name" => name)
end
How to get the selection value from view into the controller as parameter? 如何从视图中将选择值作为参数获取到控制器中?
I has a mongoDatabase with documents(row) consisting key named: name .I has 4 unique values under key name .I want user to select the value using dropdown.As the user selected the dropdown.Without page refresh the documents consisting values selected with the key name should be displayed. 我有以下的重要文件(行)一个名为mongoDatabase: 名字 。我有下。我想用dropdown.As用户选择的dropdown.Without页面刷新由值与选定的文件用户选择值的键名 4个唯一值密钥名称应显示。
Ex:under key name .I has four values for 200 documents.named: 例如:在键名下 。我有200个文档的四个值。
["value1","value2","value3","value4"].These values are the options [“ value1”,“ value2”,“ value3”,“ value4”]。这些值为选项
for dropdown menu.If user selected any of the values.The documents consisting value for key name should be displayed. 如果用户选择了任何值,则应显示包含键名值的文档。
All you need to make ajax call on onchange event. 您需要在onchange事件上进行ajax调用。 Here is a link to a helpful answer stackoverflow.com/a/7952315/4136098
这是一个有用的答案的链接stackoverflow.com/a/7952315/4136098
How to get the selection value from view into the controller as parameter
如何从视图中将选择值作为参数获取到控制器中
Only way is to send the data through either an HTTP or Ajax (XML) request. 唯一的方法是通过HTTP或Ajax(XML)请求发送数据。
Because you've not explained your user story
very well, I'll explain the overall schematics on how to get it to work... 因为您没有很好地解释
user story
,所以我将解释有关如何使其工作的总体示意图...
Stateless 无状态
HTTP makes Rails applications stateless - meaning that each time you send interactions to it, it will have to rebuild the user environment each time (this is why sessions
are so important to Rails). HTTP使Rails应用程序变为无状态 -意味着每次向其发送交互时,它都必须每次都重建用户环境(这就是
sessions
对Rails如此重要的原因)。
This means that each time you want to invoke new actions / methods, you have to send a request to your server. 这意味着每次您要调用新的操作/方法时,都必须向服务器发送请求 。 The request can be sent over several protocols (
HTTP
, XML
and Websockets
) - each has the same pattern: Request > Logic > Output
可以通过多种协议(
HTTP
, XML
和Websockets
)发送请求-每个协议具有相同的模式: Request > Logic > Output
Thus, if you want to send data to your controller, you'll have to either have a full page refresh (follow the above pattern), or send the data via ajax. 因此,如果要将数据发送到控制器,则必须刷新整个页面(遵循上述模式),或者通过ajax发送数据。
In this case I'd recommend using ajax
to send a request to your controller. 在这种情况下,我建议使用
ajax
将请求发送到您的控制器。
# View
<%= select_tag :name,options_for_select(@tests.map{|u| [u.name]}.uniq), id: "select" %>
#app/assets/javascripts/application.js
$(document).on("change", "#select", function(e) {
$.get("/controller/update_dropdown", {id: $(this).val() }, function(data) {
# Do something here
});
});
This will allow you to use the following: 这将允许您使用以下内容:
#config/routes.rb
resources :controller do
get :update_dropdown, on: :collection #-> url.com/controller/update_dropdown
end
#app/controllers/controller_controller.rb
class ControllerController < ApplicationController
def update_dropdown
# params[:id] will be available
@test = Test.find params[:id]
render json: @test.to_json
end
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.