1

I am really in a bind on how to get this last step of my app done.

I want to dynamically replace the main content without needing to refresh the page.

The application.html.erb looks like this:

<div id="wrapper">
        <!-- ><div id="headcontainer">
            <header>
            </header>
        </div> -->
        <div id="maincontentcontainer">
            <div id="maincontent">
                <div class="section group">
                    <div class="col span_1_of_7">
                        <%= render :partial => "shared/menu" %>
                    </div>
                    <div id="replace">
                        <%= yield %>
                    </div>
                </div>
            </div>
        </div>

I have my menu partial which when clicked I want to dynamically replace the content of the #replace div

<ul class='kwicks kwicks-vertical'>
    <li id='panel-1'><%= link_to_unless_current image_tag('keyIssues.png'), :remote => true %></li>
    <li id='panel-2'><%= link_to_unless_current image_tag('pollVsLeader.png'), :remote => true  %></li>
    <li id='panel-3'><%= link_to_unless_current image_tag('keyBubble.png'), :remote => true  %></li>
    <li id='panel-4'><%= link_to_unless_current image_tag('dataSource.png'),  :remote => true  %></li>
    <li id='panel-5'><%= link_to_unless_current image_tag('group14.png'), :remote => true  %></li>
</ul>

I had some Jquery that I was using to remove the div and replace it.. but it is replacing the div with the fill contents of the page.. a page within a page here is some of the code:

$(function(){
  $('#panel-1').click(function(){
    $("#remove").remove();
    $.get('../key_issues/index.html', function(html){
      $('.col.span_6_of_7').remove();
      $('.section.group').append(html);  
    });
  });
});

I have tried the ajax-rails gem but i cant get that to work either.. can someone offer some advice please?

1 Answer 1

4

In simple way

<li id='panel-1'><%= link_to image_tag('keyIssues.png'), some_path_to_url_controller , :remote => true %></li>

Controller code

def some_method
 //your code
 respond_to do |format|
  format.js
 end
end

some_method.js.erb

$("#replace_div").html("<%= j render partial: 'your_partial' %>");

For more info railscast

Sign up to request clarification or add additional context in comments.

7 Comments

Thanks for the reply.. Can you explain the controller code please..?
making your link to remote true, it will search for the response in js format. You do what ever you want to display in the replace div.
Specifically, link will submit an Ajax request to call a remote function in controller, which subsequently queries some data and returns a JS object to the page. replace the content/ partial with the ajax.
Sorry if this seems like a dumb question,.. where is some_method called from (i cant see it anywhere) ? Does that go in application_controller.js?
the href in your link_to image_tag, is 'some_path_to_url_controller' where you basically put your path to app/controller method, adjust this with your own routes and methods. then click the link it definitely reach the controller/method.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.