简体   繁体   中英

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.

<%= 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:
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.
Ex:under key name .I has four values for 200 documents.named:
["value1","value2","value3","value4"].These values are the options
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. Here is a link to a helpful answer 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.

Because you've not explained your user story very well, I'll explain the overall schematics on how to get it to work...


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).

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

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.

# 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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