Render different partials with ajax - rails 3 - ruby-on-rails

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>Universitet & Högskola</li>
Any help would be appreciated.

Try adding format to the link
example
<li><%= link_to "Grundskola", {:action => 'grundskola',:format=>:js}, :remote => true %></li>

Related

How to show two images in one slide

Am using bxslider in my rails app it works fine. It shows one image per slide but i want is to show two images per slide.
<ul class="bxslider">
<% #highlights.each do |highlight| %>
<% if highlight.area == current_or_guest_user.search_area or highlight.state == current_or_guest_user.search_state or highlight.city == current_or_guest_user.search_city %>
<li><%= image_tag highlight.image, :class => "dali" %></li>
<% end %>
<% end %>
</ul>
if i replicate the image_tag it does show two images in one slide but the same image.
You can get the records in pairs. Try this:
<ul class="bxslider">
<% #highlights.each_cons(2) do |highlight1, highlight2| %>
<% if condition %>
<li class="row">
<%= image_tag highlight1.image, :class => "dali col-xs-6" %>
<%= image_tag highlight2.image, :class => "dali col-xs-6" %>
</li>
<% end %>
<% end %>
</ul>

How to build a search form in rails for soundcloud

Hi i'm new to rails and trying to figure out how to build a search for to get info back from Soundcloud.
If i build everything static then i get back the info but i cannot get it to work with a search form
Method in Controller:
def search
#client = Soundcloud.new(:client_id => ENV["SOUNDCLOUD_CLIENT_ID"])
end
THIS WORKS
#search.html.erb
<div class="form">
<% #client.get('/tracks', :q => 'djsneak' ).each do |track| %>
<ul>
<li><%= track.title %></li>
<li><%= image_tag track.artwork_url %></li>
</ul>
<% end %>
</div>
What i would like to use:
<!--search.html.erb-->
<div class="form">
<%= form_tag("soundcloud_search", method: "get") do %>
<%= label_tag(:q, "Search for:") %>
<%= text_field_tag(:q) %>
<%= submit_tag("Search") %>
<% end %>
</div>
<div>
<h3>results</h3>
<ul>
<li><%= track.title %></li>
<li><%= image_tag track.artwork_url %></li>
</ul>
</div>
i solved it myself, but for who is interested:
<!--search.html.erb-->
<!--search form-->
<div class="form">
<%= form_tag("searchit", method: "get") do %>
<%= label_tag(:q, "Search for:") %>
<%= text_field_tag(:q, params[:q]) %>
<%= submit_tag("Search", :name => nil) %>
<% end %>
</div>
<!--Results-->
<div class="form">
Search Results
<% #client.get('/tracks', :q => params[:q] ).each do |track| %>
<ul>
<li><%= track.title %></li>
<li><%= image_tag track.artwork_url %></li>
</ul>
<% end %>
</div>

How to make links for index and show actions work in partial views

I am using a partial to render a nav bar from the layouts folder in all my pages, and within the nav bar I have links to actions such as index and show from a GradesController. I understand that in order for the links to work I must call them in that page's action in the controller as such:
def home
#grades = Grade.all
end
However as these links exist within a partial view which doesn't have a corresponding controller, how can I make these links work without getting a NoMethodError?
layouts/_navbar.html.erb
<div class="main_nav_color">
<div class="wrapper">
<header id="main_header" class="cf">
arabecademy
<nav>
<ul>
<li>
<%= link_to "#" do %>
Grade<span class="glyphicon glyphicon-chevron-down"></span>
<% end %>
<ul>
<% #grades.each do |grade| %>
<li><%= link_to "Grade" + grade.grade_number, grade_courses_path(grade, #course) %></li>
<% end %>
</ul>
</li>
<li>About</li>
<li>Contact</li>
<% if student_signed_in? %>
<li><%= link_to "Log Out", destroy_student_session_path, method: :delete %></li>
<li><%= link_to "Accout Settings", edit_student_registration_path %></li>
<% else %>
<li><%= link_to "Log in", new_student_session_path %></li>
<li><%= link_to "Sign Up", new_student_registration_path, :class => "button_sm button_orange" %></li>
<% end %>
</ul>
</nav>
</header>
</div>
</div>
To get #grades set all the time, you can add a before_filter in ApplicationController like this:
class ApplicationController
before_action :load_grades
def load_grades
#grades = Grade.all
end
end
This will make sure that every single controller action has #grades set in addition to whatever the action itself does. Your partial should be able to pick up #grades wherever you display it now.

rails css class="current"

I am trying to add a class="current" to the current page in the navigation
to the first <li>. So the html should look like
<li id="nav-home" class="current"> but only to the current page.
<% subjects.each do |subject| %>
<li id="nav-<%= subject.name %>" >
<a <%= link_to(subject.name, :action => 'show') %></a>
<ul>
<% subject.pages.visible.sorted.each do |page| %>
<li ><%= link_to(page.name, :action => 'show', :id => page.permalink) %></li>
</ul>
<% end %>
<% end %>
You can use current_page? to check if the page you are linking to is the current page.
<li id="nav-<%= subject.name %>" class="<%= current_page?( page_i_am_linking_to ) ? 'current' : '' %>" >
This will set the class to "current" or leave it empty.
I did not really understand how you build your link/ to what you are linking, that is why I used "page_i_am_linking_to". You need to replace this with something valid.
See also the Rails API
You want to have an instance variable #current_page_id that is set to the page that your rendering, then just add the class to the element you want to have it, like so (I chose to put it on the list element):
<% subjects.each do |subject| %>
<li id="nav-<%= subject.name %>" >
<a <%= link_to(subject.name, :action => 'show') %> </a>
<ul>
<% subject.pages.visible.sorted.each do |page| %>
<li class="<%= #current_page_id == page.id ? 'current' : '' %>"><%= link_to(page.name, :action => 'show', :id => page.permalink) %></li>
</ul>
<% end %>
if you want to put it on the link instead:
<li ><%= link_to(page.name, :action => 'show', :id => page.permalink, :class => #current_page_id == page.id ? 'current' : '') %></li>

How to show if rhomobile array is empty

I have a bit of code in RhoMobile that shows results of a search. I want to display a message if no results are found but as a Ruby n00b I'm not getting the message I want output.
<ul data-role="listview">
<% #employees.each do |employee| %>
<li>
<a href="<%= url_for :action => :show, :id => employee.object %>">
<%= employee.name %>
</a>
</li>
<% end %>
<% "<li>No results found</li>" if #employees.empty? %>
</ul>
How to fix this?
You're missing the =, it should be:
<%= "<li>No results found</li>" if #employees.empty? %>
Though that might not work either because the string isn't marked as HTML safe. That said, it's probably best to wrap everything in a conditional to make it more clear and avoid having HTML in a string:
<ul data-role="listview">
<% if #employees.any? %>
<% #employees.each do |employee| %>
<li>
<%= link_to employee.name, {:action => :show, :id => employee.object} %>
</li>
<% end %>
<% else %>
<li>No results found</li>
<% end %>
</ul>
I've also replaced your hand-coded link with a call to link_to.

Resources