Shakeeb Ahmad
Shakeeb Ahmad

Reputation: 2030

Render partial in a div on clicking a link

I have a a sidebar that like this;

<div id = "sidebar-links">
      <% @locations.each do |locs| %>
        <p style = "border-bottom: 1px black solid">
        <a href = "#" class = "sidebar-click"><%= locs['Address'] %></a></p>
      <% end %>
    </div>

I want to render a partial in another div id = "container"whenever one of the side bar links is clicked and also want to pass locs as a local variable to that partial;

How would I go about doing that?

Upvotes: 3

Views: 1451

Answers (2)

user1683039
user1683039

Reputation: 75

Try This one

<%= link_to locs['Address'], "path/to/redirect", :class => "side_bar" %>

and then in js

 $(".document").ready(function(){
     $(".side_bar").live("click", function(){
        $.ajax({
            dataType: 'html',
            type: 'get',
            url: $(this).attr('href'),
            success: function(data){
                       $(target_div).html(data);
                     }
        })
     })
  })

and in your controller action

def action
  #write your code here
  respond_to do |format|
    format.js{render: template}
  end
end

Upvotes: 0

wael34218
wael34218

Reputation: 4930

First of all try using the link_to helper method:

<%= link_to locs['Address'], ajax_link, :remote => true, :html => {:class => "sidebar-click"} %>

Then you should create a js bind to handle that ajax response, something like:

$(function() { 
  $(".sidebar-click").bind("ajax:success",  
    function(event, data){  
      $("#container").html(data);  
  });  
}); 

UPDATE: please check the following documentation: http://apidock.com/rails/ActionView/Helpers/PrototypeHelper/link_to_remote

Upvotes: 1

Related Questions