At this moment I am at products/index page, I have set up layout so now at the left side I have menu where I can choose between multiple categories. These are links, so when I click on them I am redirected to categories controller with specific categegory.
But I wan't to just render category controllers action show with specific category.
Is that possible, I need just a tip, I don't need full answer :)
Thanks
in side bar
<%= link_to products_path(:category => cat.id) %>
in products#index
#category = Category.find params[:category]
in products/index.html.erb
<% render '/categories/category' if #category.present? %>
Couldn't find Category without an ID Its because off the this metod that generates link_to these categories, how can I merge this kind off functionallity ?
<ul id="menuks">
<li id="auctions">Atkritumu pārstrādes tehnika</li>
<% #children1.each do |o|%>
<li class="submenuks"> <%= link_to (o.name), o %></li>
<%end%>
</ul>
Thanks to #rhernando I found solution.
Controller
#category = Category.find_by_name(params[:category])
Side menu
<li id="auctions">Atkritumu pārstrādes tehnika</li>
<% #children1.each do |o|%>
<li class="submenuks"> <%= link_to (o.name), products_path(:category => o.name) %></li>
<%end%>
Related
I am new to ROR and I don't understand how can I make global template which I could put in any other template. For example I have categories module and would like to create sidebar navigation and put it at homepage template. I tried to do it this way, but categories controller method side_nav is never called. Is this good practice for this type of problem or should i do it different way?
categories/categories_controller.rb
def side_nav
#categories = Category.all
end
categories/_side_nav.html.erb
<ul class="list-unstyled">
<% categories.each do |category| %>
<li><%= link_to category.title, category.title.downcase %></li>
<% end %>
</ul>
homepage/index.html.erb
<%= render 'categories/side_nav' %>
You may look at layout/application file. It's global layout in your custom project by default. And you can provide your custom layouts in your contollers.
http://guides.rubyonrails.org/layouts_and_rendering.html
What is the best approach of dealing with class="active" problems in Rails?
I've found how to deal with this for single links, but when classes has to be set to "active" in different parts of the template, how do one deal with this?
Here is my code:
module AdminHelper
def nav_link(link_text, link_path)
class_name = current_page?(link_path) ? 'active' : nil
content_tag(:li) do
link_to link_text, link_path, :class => class_name
end
end
def active_section(section)
request.fullpath.start_with?(section)
end
end
And in the layout holding the menu:
<% if active_section('/superadmin') %>
<li class="active">
<% else %>
<li>
<% end %>
<i class="fa fa-asterisk fa-fw"></i> Superadmin <span class="fa arrow"></span>
<ul class="nav nav-second-level">
<%= nav_link 'Add Account', superadmin_new_account_path %>
<%= nav_link 'List Accounts', superadmin_list_accounts_path %>
</ul>
</li>
That is, I need to check if I am at a specific part of the url structure, and outputting the li class="active" in case of that. Otherwise just outputting the li element without class. I am using a menu; MetisMenu and needs to set this class so correct section is expanded.
My code works but it feels horrible wrong way to do it.
Would be really happy if someone can give a hint on how to do this the right way!
I recommend using simple-navigation gem
Not to be 'that guy', but have you looked into HAML? It makes doing conditional classing a ton easier. It also reads/looks much cleaner. i.e.
%li{:class => ("active" if active_section('/superadmin'))}
I want to display Refinery Admin pages hierarchy on front end as a site map to my users,
i'm new to RefineryCMS, can u please point to right direction? I have attached the image, that image comes under refinery/Admin section i want to add it on my site for visitors too, u can imagine i have a controller site_maps and action index and i want to display that site map under app/views/site_maps/index.html.erb page. Hope it make sense. Thanks.
I implemented this by performing following steps.
1: In controller (mine is site_maps.rb)
class SiteMapsController < ApplicationController
def index
#pages = Refinery::Page.where(parent_id:nil)
end
end
2nd: in index.html.erb View
<div class = "row">
<%#pages.live.each do |page|%>
<% if page.show_in_menu%>
<ul>
<li>
<%= link_to (page_title_with_translations page), refinery.url_for((page.url_marketable)) %>
<%if page.children.any?%>
<%= render :partial => 'shared/page', locals: {:page_children => page} %>
<%-end %>
</li>
</ul>
<%end%>
<%end%>
</div>
3rd: in _page.html.erb
<% page_children.children.live.each do |children_page|%>
<ul>
<li>
<%= link_to (page_title_with_translations children_page), refinery.url_for((children_page.url_marketable)) %>
<%if children_page.children.any?%>
<%= render :partial => 'shared/page', locals: {:page_children => children_page} %>
<%-end %>
</li>
</ul>
<%end%>
NOTE: Without This refinery function refinery.url_for your links might be detected as spider's search and will get wrong!!
Do you mean as the homepage?
then:
mount Refinery::Core::Engine, :at => '/'
I am trying to achieve an unordered list of "Categories" in which if you click on a particular Category, all the photos(jags) that belong to that Category show on the screen. My view that includes the categories is:
<div id = "Categories">
<h2>Categories</h2>
<ul><% #cat.each do |c| %>
<li><%=link_to c.name, c,:controller => "category", :action => "show" %>
</li>
<% end %>
</ul>
my Category controller is:
def show
#jags = Jag.where("category_id = params[:id]")
if #jags.empty?
flash[:notice] = "No jags in this Category"
end
end
and lastly my show view is:
<%= render 'nav' %>
<div><% #jags.each do |j| %>
<%= image_tag j.image_url(:thumb)%>
<% end %>
</div>
The problem i am having is that I dont know how do i pass on my "particular category"(c) in the first view to the Category controller.
I tried making c an instance variable(#c) which apparently i cant do[formal argument cannot be an instance variable
'); #cat.each do |#c| ;#output_buffer.safe_concat('].
If I run this code I get an SQLlite error[SQLite3::SQLException: near "[:id]": syntax error: SELECT COUNT(*) FROM "jags" WHERE (category_id = params[:id])].
If you use RESTful controllers this should be enough:
<div id = "Categories">
<h2>Categories</h2>
<ul>
<% #cat.each do |c| %>
<li><%=link_to c.name, c %></li>
<% end %>
</ul>
Seeing as you're getting the ID for the Category in the controller, then you can just do
#category = Category.find params[:id]
in your controller. Also, clean up your link_to helper as per below.
I am trying to set an li class of active based on what page the user is on.
I have 4 navigation elements and they all look something like this:
<% if #activeLi == "home" %>
<li class="active">
<% else %>
<li>
<% end %>
<%= link_to :controller => "home" do %>
<span>Home</span>
<% end %>
</li>
and then in each controller I just set #activeLi like this:
def index
#activeLi = "about"
end
I know this is pretty basic stuff, but i'm just wondering if there is an easier way to do this?
Well I know one way you can simplify this and thats by getting rid of the need to use those nasty instance variables.
<li class="<%= controller_name == "home" ? 'active': '' %>">
<%= link_to :controller => "home" do %>
<span>Home</span>
<% end %>
</li>