Reder partial in Rails applicaton inside the page OR by making an AJAX call to controlle?

We need to to replace a certain area of the page with another page content and it is very useful in Ajax calls their are different ways by witch we can render partials as:

Name of the partial is as _user_profile.html.erb

1. Render partial with parameters from the page

<%= render (:partial=>'user_profile',:locals=>{:data=>@user}) %>

2. Render partial without parameters

<%= render :partial=>"user_profile" %>

3. Extract value from the object inside javascript

var phone=document.getElementById("phone").value;

inside body their is a button tag with if phone and and is using the object passed to this  partial to give value to the text field

<td><%=text_field_tag :phone ,user.mobile_number %></td>

we can call the java script function by using below tag

<%= button_tag "Update",:onclick => "update_contact()"  %></td>

also if you want to make an Ajax call to the :controller you can do by writing it inside the script tag

<script type="text/javascript">
function update_about_you(){
var about_you=document.getElementById("aboutyou").value;

alert("inside about you"+about_you);
$.ajax({
 url:"users/update_about_you",
                          data:'about_you=' + encodeURIComponent(about_you),
                          cache:false,
                          success:function (data) {
                          $('#about_you').html(data);
                          }
       });
}
</script>

If we want to use Drop down list in side form tags and without form tags we can use it as

<%= form_for @user, :url => {:controller=>"login_activity",:action => "register"}, :html => {:class => "nifty_form"} do |f|
<%= f.collection_select(:role_type, @role_types, :id, :name,{:prompt=>true}) %>
 %>
here @role_types is the object of the class as @role_types=RoleTypes.find(:all)

Now, If we want to use drop down tag without form tag it is as simple as:

here sexes in same as @sexes 
<%=collection_select(:sex, :id, sexes,:id, :name, :prompt => true)%>
it will convert into the HTML from and appears as:

<select id="sex_id" name="sex[id]">
<option value="">Please select</option>
<option value="2">Female</option>
<option value="3">Male</option>
</select>

Calling controller Ajax way

  function registered_users() {
        $.ajax({url:'<%= url_for :controller => 'administrators',:action => 'registered_users' %>',
            cache:false,
            success:function (data) {
                $('#content').html(data);
            }
        })
    }

.

Advertisements

3 thoughts on “Reder partial in Rails applicaton inside the page OR by making an AJAX call to controlle?

Add yours

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

Up ↑

%d bloggers like this: