开发者

Rails 3, update data using selection from select tag

开发者 https://www.devze.com 2023-03-07 10:45 出处:网络
I am all new to rails, however quite familiar with \"regular\" MVC programming like Spring and .net. I am trying to cope with all this new way of doing stuff, but I keep getting stuck - especially whe

I am all new to rails, however quite familiar with "regular" MVC programming like Spring and .net. I am trying to cope with all this new way of doing stuff, but I keep getting stuck - especially when it comes to rendering/refreshing the same view with different data, based on a selection a user has made.

This is properly all really simple, and I can see it has been asked a lot of times - although, I cant seem to find any definite and "this is the rails way of doing it" answer.

All I want to do is the like this:

Database "sche开发者_开发百科me", with table a,b,c and their references:

C -> A

C -> B

Based on a selection of B in a drop down, I want to show all the "A's Cs" - dont mind the querying part, that is straight forward. By now I choose the first B as a "first render" of the page in my controller, but when the user selects another B in the dropdown, the view should be rerendered based on the selection...

I dont want to use custom javascript filling or some other "weird" way. I must be possible to select something and refresh the view based on the selection in RoR.

I hope you either can help me or point me in the right direction.

Thanks!


Put all these select lists in a form and in the 'onchange' event of the lists add javascript that submits the form ($('form_name').submit()). Once you submit the form, you have all the selected values as params (params[:select_list_1], params[:select_list_2]). Process them as you want, fire queries and display the view again.

You could submit the form to the same action. The action would be something like this:

def index
  if !request.post?
    # Generate default values
  else
    # Generate new resultset based on params
  end
end

EDIT (Form example): The form can be added as and :url specifies the action of form element:

<%= form_for(:select_list_form, :url => {:controller => "my_controller", :action => "index"}) do |f| %>
<!-- Your select list here -->
    <%= f.submit "Continue", :class => "form-button form-left" %>
<% end %>
<!-- Render data based on your instance variables -->


I have encounter this problem myself and the cleanest way I have found is to use this :

  • http://www.prototypejs.org/api/timedObserver/form-observer

It call an ajax request when the select list change. Then you have to set up an new action in your controller for example load_related :

def load_related
    @childreen = Model.where(....)
    render :update do |page|
         page.replace 'div id where you whant stuff to appear', :partial => '...'
    end
end

That's your first ajax request in Rails. For documentation :

  • http://api.rubyonrails.org/classes/ActionView/Helpers/PrototypeHelper.html
0

精彩评论

暂无评论...
验证码 换一张
取 消