简体   繁体   中英

Render different partials with ajax - rails 3

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.

Try adding format to the link

example

<li><%= link_to "Grundskola", {:action => 'grundskola',:format=>:js}, :remote => true  %></li>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM