rails css class="current" - ruby-on-rails

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>

Related

dynamically change routes in a partial

I have a filter on my page like so.
<ul>
<li>
<%= link_to "hot", root_path(:filter => ["hot"]) %>
</li>
<li>
<%= link_to "recent", root_path(:filter => ["recent"]) %>
</li>
<li>
<%= link_to "most popular", root_path(:filter => ["popular","day"]) %>
</li>
<li>
<%= link_to "most liked", root_path(:filter => ["liked","day"]) %>
</li>
</ul>
And the almost exact same filter(aside from the path) on another page (for followed users):
<ul>
<li>
<%= link_to "hot", followed_index_path(:filter => ["hot"]) %>
</li>
<li>
<%= link_to "recent", followed_index_path(:filter => ["recent"]) %>
</li>
<li>
<%= link_to "most popular", followed_index_path(:filter => ["popular","day"]) %>
</li>
<li>
<%= link_to "most liked", followed_index_path(:filter => ["liked","day"]) %>
</li>
</ul>
Whats the best way to roll these into one partial? Right now I have a partial for each version which seems really messy to me.
EDIT:
I got this to work as follows:
<ul>
<li>
<%= link_to "hot", url_for(:action => "index", :filter => ["hot"]) %>
</li>
<li>
<%= link_to "recent", url_for(:action => "index", :filter => ["recent"]) %>
</li>
<li>
<%= link_to "most popular", url_for(:action => "index", :filter => ["popular","day"]) %>
</li>
<li>
<%= link_to "most liked", url_for(:action => "index", :filter => ["liked","day"]) %>
</li>
</ul>
You can try to implement helper method.

What is `stringify_keys' in rails and how to solve it when this error comes

In a partial file of my application I have the following code snippet for displaying user navigation(through Devise):-
<ul class="nav pull-right">
<% if user_signed_in? %>
<li>
<%= current_user.email do %>
<i class=" icon-user icon-black"></i>
<% end %>
</li>
<li>
<%= link_to "Your Links", profiles_index_path do %>
<i class=" icon-user icon-black"></i>
<% end %>
</li>
<li>
<%= link_to "Sign out", destroy_user_session_path, :method => 'delete' do %>
<i class=" icon-user icon-black"></i>
<% end %>
</li>
<% else %>
<li>
<%= link_to "Login", new_user_session_path do %>
<i class=" icon-lock"></i>
<% end %>
</li>
<li>
<%= link_to "Sign up", new_user_registration_path do %>
<i class=" icon-home"></i>
<% end %>
</li>
<% end %>
</ul>
But I'm getting an error saying:-
undefined method `stringify_keys' for "/users/sign_in":String
Now my questions are:-
What is `stringify_keys' in general??
How do I resolve this in my code???
Thanks...
1) stringify_keys is a method that is called on a hash to convert its keys from symbols to strings. It's added by Rails - it's not a standard Ruby method. Here it is in the docs.
{:a => 1, :b => 2}.stringify_keys # => {"a" => 1, "b" => 2}
2) This means that your code is passing "/users/sign_in" somewhere that is expecting a hash. Closer inspection reveals that you are mixing and matching two forms of link_to:
# specify link contents as an argument
link_to "The text in the link", "/path/to/link", some: "options"
# specify link contents in a block
link_to "/path/to/link", some: "options" do
"The text in the link"
end
As you can see you are trying to do both:
<%= link_to "Sign out", destroy_user_session_path, :method => 'delete' do %>
<i class=" icon-user icon-black"></i>
<% end %>
and Rails expects the second argument in the block form to be the options hash, so it is calling stringify_keys on it which is causing your error.
Change those links to look like this instead:
<%= link_to destroy_user_session_path, :method => 'delete' do %>
<i class=" icon-user icon-black"></i> Sign out
<% end %>
as per the documentat stringify_keys will try to convert all keys to a string. So in simple works the method expects something with key values and convert its keys to strings.
In your case most probably your User object could be empty or a plan string. is this doesnot
work try posting the complete error log

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>

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.

Rails: making a table from a collection

I'm using the Forem blogging gem. It renders the posts of a particular topic this way.
<%= render :partial => "forem/posts/post", :collection => #posts %>
Is there a way to make the output appear in a table? I've put <td> markup around the sections in the partial below that I want to break up into a table.
The row names I would want are Name, Posted At, Message, In Reply to, and Actions.
I thought I might be able to do something like
#posts.each do |p|
<tr>
p
</tr>
but I can't figure out how to get it to work
Partial
<a name='post-<%= post.id %>'></a>
<div id='post_<%= post.id %>' class='post <%= cycle('odd', 'even') -%>'>
<td>
Name:<%= link_to_if Forem.user_profile_links, post.user, [main_app, post.user] %>
</div>
<div class='icon'><%#= avatar(post.user, :size => 60) %></div>
</td>
<td>
<a href='#post-<%= post.id %>'>
<time datetime="<%= post.created_at.to_s(:db) -%>"><%= "#{time_ago_in_words(post.created_at)} #{t("ago")}" %></time>
</a>
</td>
<td>
<%= forem_format(post.text) %>
</td>
<td>
<% if post.reply_to %>
<span class='in_reply_to'>
<%= link_to "#{t("forem.post.in_reply_to")} #{post.reply_to.user}", "#post-#{post.reply_to.id}" %>
</span>
<% end %>
</td>
<td>
<ul class='actions'>
<% if forem_user %>
<% if can?(:reply, #topic) %>
<% if #topic.can_be_replied_to? %>
<li><%= link_to t('reply', :scope => 'forem.topic'), new_topic_post_path(#topic, :reply_to_id => post.id) %></li>
<% end %>
<% if #topic.can_be_replied_to? %>
<li><%= link_to t('quote', :scope => 'forem.topic'), new_topic_post_path(#topic, :reply_to_id => post.id, :quote => true) %></li>
<% end %>
<% end %>
<% if post.owner_or_admin?(forem_user) %>
<% if can?(:edit_post, #topic.forum) %>
<li><%= link_to t('edit', :scope => 'forem.post'), edit_topic_post_path(#topic, post) %></li>
<% end %>
<li><%= link_to t('delete', :scope => 'forem.topic'), topic_post_path(#topic, post), :method => :delete, :confirm => t("are_you_sure") %></li>
<% end %>
<% end %>
</ul>
</td>
</div>
</div>
Have you tried adding instead of at the very beginning of the code for the partial? And perhaps a before the loop over posts?

Resources