Ruby on Rhodes using ajax call

家住魔仙堡 提交于 2019-12-12 03:45:27

问题


I am trying to build a dynamic dropdown in ruby on rhodes.There are basically two dropdowns on my screen and i am using ajax to get the values of the second dropdown from the database depending on the value selected in the first dropdown..I am a newbie to ruby and do not know the syntax on how to use ajax in ruby on rhodes..

JavaScript Code I am using...

$.post("/app/Settings/dropdown", 
 { value:a }, 
 function(data){ 
alert(data);

});

-----Partial Controller Code

enter code here

def dropdown

 @a = @params['value']
  puts @a
 if @a.eql?"Auto"
   mystring="auto1|auto2|"
  else
   mystring="personal1|personal2|"
  end
end

I can get any parameter sent via ajax call to controller..My Question is how to send back the data from controller to function in that ajax call so that i can use that information to create a dynamic dropdown..I want to send this mystring to function(data)??


回答1:


In Rhodes, controller actions can only render other actions or return a string consisting of partials. So, in order to populate a dropdown using AJAX, you'll have to render the view associated with the action which will returned as response to the AJAX call.

Controller 'dropdown' action:-

def dropdown
    @a = @params['value']
    if @a.eql?"Auto"
        @optionList[:auto1]="auto1"
        @optionList[:auto2]="auto2"
    else
        @optionList[:personal1]="personal1"
        @optionList[:personal2]="personal2"
    end
    render :action => "dropdown"
end

'dropdown.erb' view:-

<% optionList.each do |key, value| %>
    <option value="<%= key %>"><%= value %></option>
<% end %>

AJAX call:-

$.post(
    "/app/Settings/dropdown", 
    { value:a },
    function(data){
        data = data.replace("<div>","");
        data = data.replace("</div>","");
        alert(data);
    }
});

Make sure you replace the div tags in the AJAX response, since Rhodes automatically surrounds AJAX responses with div tags.



来源:https://stackoverflow.com/questions/14208807/ruby-on-rhodes-using-ajax-call

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!