简体   繁体   English

它们可以吗?我可以在Rails应用程序的ruby中使用onchange事件处理程序来获取下拉列表的值以在控制器中使用

[英]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 可以通过多种协议( HTTPXMLWebsockets )发送请求-每个协议具有相同的模式: 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发送数据。


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.

相关问题 Rails应用程序中未出现onchange事件 - onchange event not appearing in rails application 我在哪里以及如何为rails 3.1 select tag onchange事件编写事件处理程序? - where and how do I code an event handler for a rails 3.1 select tag onchange event? Ruby on Rails中的Javascript onchange事件问题 - Problem with Javascript onchange event in Ruby on Rails Ruby on Rails更改本地onchange事件 - Ruby on rails change local on onchange event 更新Ruby和Rails之后,为什么会出现“在任何来源中都找不到mail-2.2.9.1”的信息? - Why do I get “Could not find mail-2.2.9.1 in any of the sources” after updated Ruby and Rails? 在轨道上的下拉式红宝石发生更改事件时 - On change event of dropdown ruby on rails 有什么办法可以从模块(不是控制器)重定向Rails中的ruby中的回调URL? - is there any way to redirect callback url in ruby on rails from module (not controller)? 如何在rails上的ruby中对Select_tag的“onchange”使用特定的controller \\ action - How to use a specific controller\action on “onchange” for Select_tag in ruby on rails 如果我在git存储库中有副本,有什么办法可以恢复我在Rails中的应用程序? - Is there any way to recover my application in ruby on rails if I have a copy at the git repository? 一个ruby on rails应用程序可以使用两个sqlite3数据库吗? - could one ruby on rails application use two sqlite3 database?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM