开发者

Ruby on rails: how to update an div with partial without making an ajax call to server

开发者 https://www.devze.com 2023-03-06 03:44 出处:网络
I want to update an div with contents of a partial when a user clicks on a radio button. Now the contents of th开发者_JAVA技巧at partial are static html (a form may be). So because I do not have any d

I want to update an div with contents of a partial when a user clicks on a radio button. Now the contents of th开发者_JAVA技巧at partial are static html (a form may be). So because I do not have any data to fetch from the server I am reluctant to make an ajax call to server just to fetch that partial and update the div. Here is what I have tried.

 function loadControlPanel(type)
      {
        alert(type);
        $(controlPanelDiv).appendChild("<%=  render :partial => 'layouts/agency_selection' %>");
      }


<input type="radio" name="view" value="agencies" onClick ="loadControlPanel(this.value)" /> Agencies

But this doesn't work because the render replaces the entire partial there along with the new lines which throws javascript errors in the function loadControlPanel(type).

Is there any way to overcome this? or some other simpler solution?

Thanks Shaunak


Why dont you render the static part in your layout in a hidden div, then clone that id and put it in your innerHTML using javascrtipt?

By the way just calling "render :partial" does not mean its an Ajax call.


OKay here is the work around i have found to get around this. I am calling 'gsub' on the render to replace the newlines with nothing. I am still open to more elegant solutions though.

function loadControlPanel(type)
      {
        alert(type);
        $(controlPanelDiv).innerHTML = '<%=  (render :partial => "layouts/agency_selection").gsub("\n","") %>';
      }
0

精彩评论

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