Question

I am new to RoR/Padrino. I want to add an autofil functionality to a form. Lets say when a User ID is entered The Name of the User is shown in the name field. And when a specific user type is selected a dropdown menu only shows entries matching the user type.

What kind of technology do I need to use? I read a lot about jQuery and AJAX but I really feel lost in the huge amount of questions relating this "problem". Can someone give me a hint where and what to read in order to get the needed knowledge?

Thanks in advance Patrick

Était-ce utile?

La solution

Here is an example:

$('#your_model_user_id').on('change', function() {
  $.ajax({url: "/your_model/getname/" +  $(this).val(),type: 'get'});
});

Then create an action in your_model:

def getname
  @yourmodel = YourModel.find(params[:id])
end

Create a route:

get '/your_model/getname/:id' => 'your_models#getname'

create the file app/views/your_models/getname.js.erb:

$('#your_model_username').val('<%= @yourmodel.name %>');

In a nutshell

Autres conseils

There are a few way to do that kind of stuff.

  1. Write your own javascript and do everything manually
  2. Use library like jQuery Tokeninput

Here a few links which might help:

http://railscasts.com/episodes/88-dynamic-select-menus

http://blog.sandeep.me/2011/08/dynamic-select-menus-in-rails-3.html

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top