0

I am trying to get a link_to to display different partial via jQuery ajax, for every nav tab link.

But can't seem to get it to work, it redirects every link to the root index.

I would like to click the links in my nav menu and display the partials: _grundskola.html.erb , _gymnasium.html.erb, _universitet.html.erb for that link in the profile-data div.

Example:

I understand that for example nav link "universitet" should call the action def universitet which calls universitet.js.erb which renders the partial _universitet.html.erb in the div.

The nav-menu - in the show.html.erb

 <ul class="nav nav-tabs">
  <li><%= link_to "Grundskola", :action => 'grundskola', :remote => true  %></li>
  <li class="active"><%= link_to "Gymnasium", :action => 'gymnasium', :remote => true %></li>
  <li><%= link_to "Universitet & Högskola", :action => 'universitet', :remote => true %></li>    
 </ul>

user_controller.rb

def universitet
    respond_to do |format|
      format.js
    end
  end

universitet.js.rb

$("profile-data").html("<%= escape_javascript(render(:partial => 'universitet')) %>");

_universitet.html.erb

<% groups = @user.friends.group_by(&:college_name) %>
<% sorted_groups = groups.sort_by{|key, values| values.count}.reverse %>
<% sorted_groups.each do |collegename, friends| %>
<% next if collegename.blank? %>
<div class="contentbox">
    <div class="box-header">
        <h3><%= collegename %></h3>
        <div class="meta-info">
            <p><i class="icon-map-marker"></i> Malmö</p>
            <p><i class="icon-user"></i><span class="count"> <%= friends.count %></span> vänner</p>
        </div>
    </div>
    <ul class="friends-list">
        <% friends.map do |friend| %>
        <li><%= image_tag(friend.image) %>
            <% end %> 
        </ul> 
    </div>
<% end %>

I get the wrong link structure to, the html output:

<li><a href="/auth/failure?action=universitet&amp;controller=users&amp;remote=true">Universitet &amp; Högskola</a></li>

Any help would be appreciated.

1 Answer 1

0

Try adding format to the link

example

<li><%= link_to "Grundskola", {:action => 'grundskola',:format=>:js}, :remote => true  %></li>
3
  • - wont work :/ give this html output: <li><a href="/auth/failure.js?action=grundskola&amp;controller=users" data-remote="true">Grundskola</a></li>
    – user592638
    Jul 27, 2012 at 19:59
  • did you put the :action and :controller inside {} ? Jul 27, 2012 at 21:52
  • 1
    I think there is some before filter or authentication mechanism in your project that causes the redirect ,check that out. Jul 28, 2012 at 8:18

Your Answer

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