how to highlight current link in rails 3 - html

I am trying to highlight curent link. I used this question for ideas here
I have simple menu, that is based on this structure:
<li>
<%= #waste_root.name %>
<ul>
<% #wast_child_ids.each do |it| %>
<li><%= link_to it.name, products_path(:category => it.name), class: "#{cp(products_path)} additional_class" %>
</li>
<%end%>
</ul>
</li>
In Aplication helper:
def cp(path)
"current" if current_page?(path)
end
In CSS file:
.current {
color:red;
}
What I get is all links are in red color. I don't get it. For others it worked just fine.

I think your path in view should look like this:
<li>
<%= #waste_root.name %>
<ul>
<% #wast_child_ids.each do |it| %>
<li><%= link_to it.name, products_path(:category => it.name), class: "#{cp(products_path(:category => it.name))} additional_class" %>
</li>
<%end%>
</ul>
</li>
Because, you're passing a param category in products_path and hence current_page? isn't able to judge the correct relative path. Also, it'd be better if you use the _url(complete path) instead of relative path. It'll be much clear to cross check and to understand as well.

You need to pass the category.
<%= link_to it.name, products_path(:category => it.name), class: "#{cp(products_path(:category => it.name))} additional_class" %>
They're all on the products path because you're just differentiating based on parameters. So just passing the products path they all return true, you need to differentiate based on the parameters by passing them too as you've done in your link

Related

Links in navbar with Rails and Bootstrap

Say I have a navbar:
<ul class="nav navbar-nav">
<li><%= link_to 'Item 1', item1_path %>
<li><%= link_to 'Item 2', item2_path %>
...
But this way current menu item is also a link. How do I make it into a span, preserving appearance? Which doesn't look like a good options, since in this case I need to duplicate bootstrap's styles. Or make it look like a menu item without it pointing to any page?
A link_to method will always render an <a> tag, but you can render a <span> inside of the <a> tag if you use link_to ... do
This is an example taken from this documentation
<%= link_to(#profile) do %>
<strong><%= #profile.name %></strong> -- <span>Check it out!</span>
<% end %>
<!-- which renders -->
<a href="/profiles/1">
<strong>David</strong> -- <span>Check it out!</span>
</a>
I came up with the following helper:
module ApplicationHelper
def menu_item(name, path)
if current_page? path
content_tag('a', name)
else
link_to name, path
end
end
end
With that we've just got to replace link_to with menu_item in original code.

RoR Selecting data to show data

How is the easiest way to make a selectio field which will retrive other information? I have an Ajax script to make this, i think i can make it work. But srsly, i can't figure out how to make in the view or controller actions to let me select a data from the table and get other data related to it.
This is my code, this may help understand what i'm trying to say.
<%= form_for(#page) do |f| %>
<div id="jstree">
<ul>
<li> <%= f.collection_select :cliente_id, Cliente.order(:name), :id, :name, prompt: "Selecionar", :required => true %>
<ul>
<li id="child_node_1"><%= f.projeto.name %>
<ul>
<li id="child_node_2"><%= f.task.descr %></li></li></ul>
</ul>
</li>
</ul>
</div>

Rendering view for an element in rails by :id

I have a sidebar that contains links to all of a users :shopping_lists. Upon clicking on one of those links, I'd like to render a page showing the :list_items in that particular list. Here's my sidebar partial:
<aside class="sidebar-nav-fixed">
<h1>My Lists</h1>
<% if user_signed_in? %>
<% current_user.shopping_lists.each do |l| %>
<ul>
<%= link_to "#{l.name}", '#' %>
</ul>
<% end %>
<% else %>
<h5><%= link_to "Sign in to manage lists.", new_user_session_path %></h5>
<% end %>
</aside>
My question is: what path would I be putting in place of my current stub link in order to route to the correct list? Thanks in advance!
That will depend on how your routes are setup. I would expect shopping lists to always be in the context of a user, so probably something like this:
<%= link_to l.name, user_shopping_list_path(current_user, l) %>
If shopping lists are a top level route, then probably something like this:
<%= link_to l.name, shopping_list_path(l) %>
There are couple of things you can do, granted your routes are setup correctly:
The easiest is:
link_to "#{l.name}", l
Rails should create a link something similar to http://host/shopping_lists/2
The above is a shorthand for
link_to "#{l.name}", shopping_list_path(l)
To see a list of available routes and methods you can run:
bundle exec rake routes
in the root of your rails app

Link_to in rails and dynamic names from modal#show

I have a loop that creates a list of works from the modal work
//does work but want test to be <%= work. name %>
<ol class="meny-control mobile">
<% #works.each do |work| %>
<li class="" data-id="<%= work.id %>"><%= link_to 'test', work %></li>
<% end %>
</ol>
//doesnt work but want it to
<ol class="meny-control mobile">
<% #works.each do |work| %>
<li class="" data-id="<%= work.id %>"><%= link_to '<%= work.name %>', work %></li>
<% end %>
</ol>
As you would guess the <%= work.name %> throws a syntax error. How do I correctly format the link_to to display each work.name as the 'path' && the anchor's inner html as work.name.
Being new to rails, I'm still really iffy on understanding documentation properly. Could you please reference from link_to() (if even there) where this format is explained so I use this for future referencing & understanding --also how to properly edit the stack question title for future similar question.
The error is because of the nesting of <% tags and I suppose you already are aware of that. To solve your problem use the following:
<%= link_to "#{work.name}", work %>
The #{} is used to interpolate variables, i.e. replacing variables with their values within the string literals as in link_to "#{work.name}" above where work.name will be replaced by the value work.name holds.
you don't need "#{}".
you can write this:<%= link_to work.name, work %>

How can I populate a div with Rails code when a link is clicked?

I'm trying to populate some li tags with this code:
<% #events.each do |event| %>
<li>
<span class="eventname"><%= event.name %></span>
<span class="eventlocation"><%= event.location %></span>
</li>
<% end %>
And I have some links that say "today", "tomorrow" etc and when I hit today I want the events from today to show up. If I hit tomorrow I want tomorrow's to show up. I've looked everywhere through AJAX tutorials and javascript tutorials and I'm completely lost on how to do this in rails.
One way I considered doing is having this block of code be a partial, and change it slightly for each partial so that the specific events that I want to show come up. But then I'm still stuck with the same problem, I'm not sure how to show a specific partial based on the click of a link.
We will have one partial that would render a set of given events (any type) let's call it _events.html.erb
_events.html.erb:
<% events.each do |event| %>
<li>
<span class="eventname"><%= event.name %></span>
<span class="eventlocation"><%= event.location %></span>
</li>
<% end %>
Then we would create an action named fetch_events that returns a js response.
Class EventsController < ApplicationController
def fetch_events
#events = Event.where(type: params[:type])
respond_to do |format|
format.js
end
end
end
ofcourse in routes.rb
match 'fetch_events/:type' => 'events#fetch_events', as: :fetch_events
now we build out fetch_events.js.erb, this file will set the contents of the div with id 'events' to the rendered partial.
fetch_events.js.erb:
$('#events').html(<%= escape_javascript(render :events, events: #events) %>)
Now in your page view add a have div with id 'events' that will contain the response, while
the ajax could be called using link_to
<%= link_to 'today', fetch_events_path(type: :today), :remote => true %>
<%= link_to 'tomorrow', fetch_events_path(type: :tomorrow), :remote => true %>