rails instead link_to - render it - ruby-on-rails

I have controller for home, about, contact
I created navigation for that
<li><%= link_to 'Home' , homepage_path %></li>
<li><%= link_to 'About' , about_path %></li>
<li><%= link_to 'Contact' , contact_path %></li>
And it's working well, But now I created a navigation for my admin panel and I wanted to make it more organized so I did it with partials:
(on my /views/admin/index.html.erb )
<ul class="admin-nav">
<li rel="posts">Posts</li>
<li rel="pages">Pages</li>
</ul>
<div class="posts panel">
<%= render "posts/index" %>
</div>
<div class="pages panel">
<%= render "pages/index" %>
</div>
But now let's say I'm clicking on the posts button on my admin panel, It no longer need to go to the posts_path, it needs to render it, Can I do that?

Related

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.

How do I check which controller is used to render a partial in Rails?

I have a simple partial for my footer that looks like this:
<footer class="footer">
<nav>
<ul>
<li><%= link_to "Download History", report_histories_path(format: "csv") %>
<li><%= link_to "Help", help_path %></li>
<li><%= link_to "About", about_path %></li>
</ul>
</nav>
</footer>
The first link allows the user to download some data used to power reports as a CSV, but I only want this to link to appear if the reports_controller is used to render the partial.
I've tried using
<% if params[:reports] %>
<li><%= link_to "Download History", report_histories_path(format: "csv") %>
<% end %>
as well as
<% if current_page?(url_for(:controller => 'reports')) %>
<li><%= link_to "Download History", report_histories_path(format: "csv") %>
<% end %>
but neither show the link.
You can use params[:controller] for that. Also, params[:action] will contain current action.
You could also use controller_name or action_name. Or specifically, to answer your question <% if controller_name == 'reports' %>

Add Show User Link to Navbar Devise

In a class, I'm built a Pintrest style app, and I recently set up a show action for my users. The link works fine on the individual pins, but when I try to add the users#show link to the Navbar called "Profile" it does not navigate anywhere. The Profile link should navigate to users#show, but the URL is showing for Profile is my pins#index which is my root route. Here are the links in my Navbar under _header.html.erb.
<div class="nav-collapse collapse">
<ul class="nav pull-right">
<% if user_signed_in? %>
<li><%= link_to "Add Trip", new_pin_path %></li>
<li><%= link_to "About", about_path %></li>
<li><%= link_to "Profile", #user %></li>
<li><%= link_to "Logout", destroy_user_session_path, method: :delete %></li>
<% else %>
<li><%= link_to "About", about_path%></li>
<li><%= link_to "Login", new_user_session_path %></li>
<% end %>
</ul>
</div>
Here is my users_controller.rb
class UsersController < ApplicationController
def show
#user = User.find(params[:id])
#pins = #user.pins.page(params[:page]).per_page(20)
end
end
But the show user link on the pins in _pin.html.erb which is rendered in my pins#index works fine.
<div class="box">
<p class= "description">
<%= pin.description %>
</p>
<%= link_to (image_tag pin.image(:medium)), pin %>
<p class= "author">
<strong>
Posted by <%= link_to pin.user.name, pin.user %>
</strong>
</p>
</div>
Lastly, if I copy the same link that is in _pin.html.erb <%= link_to pin.user.name, pin.user %> to the Navbar, I get undefined local variable or methodpin' for #<#:0x00000100c82520>`.
Here is my routes.rb incase that helps.
devise_for :users
match 'users/:id' => 'users#show', as: :user
You need to use current_user instead of #user, since #user will be nil in most cases (except on your users#show page.
It might also be a good idea to create a /profile route that always loads the current user.

How to use mixpanel with twitter bootstrap dropdown menu?

My code looks like this:
<div class="navbar">
<div class="navbar-inner">
<div class="container">
<ul class="nav pull-right">
<% if user_signed_in? %>
<li class="dropdown" id="menu7">
<a class="dropdown-toggle" id="username" data-toggle="dropdown" href="#">
<%= current_user.full_name %>
<b class="caret"></b>
</a>
<ul class="dropdown-menu">
<li><%= link_to('Edit my account', edit_user_registration_path,
:id => 'edit_account') %></li>
<li><%= link_to('Logout', destroy_user_session_path, :method => 'delete',
:id => 'logout_link') %></li>
</ul>
</li>
<% else %>
<li><%= link_to('Login', new_user_session_path, :id => 'login_link') %></li>
<li><%= link_to('Sign up', new_user_registration_path, :id => 'signup_link') %></li>
<% end %>
</ul>
</div>
</div>
And my javascript events are these:
mixpanel.track_links('#username', 'Username clicked')
mixpanel.track_links('#edit_account', 'Edit account link clicked')
mixpanel.track_links('#logout_link', 'Logout link clicked')
I don't know why (i really looks bizarre!) only the event 'Username clicked' is working. Has anyone experienced something similar?
One possibility is that you need to include the link tag in your selector:
mixpanel.track_links('#username a', 'Username clicked')
mixpanel.track_links('#edit_account a', 'Edit account link clicked')
mixpanel.track_links('#logout_link a', 'Logout link clicked')
That's how it's done in the Mixpanel doc.
Another option is you may be going about this the wrong way. Instead of tracking clicks on links, maybe you want to use the ruby API and put your trackers in the controller, as shown in this part of the doc.
Thus, you would do something like this in your Users controller:
def edit
track_event "edits account"
end
And in sessions conroller:
def destroy
track event "logs out"
end

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>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>

Resources