I have my navigation menu in application.html and the menu has class="active" . That depends on the page the user is on. How can I dynamically figure out which item needs to have the class:
Here's how my top menu bar is:
<ul>
<li class="active">Home</li>
<li>Page1</li>
<li>Page2</li>
<li>Page3</li>
</ul>
One way would be to have an #active_page instance variable in the controllers, which you set on each action. You may also do this already with a title (#title maybe?), so perhaps you could use that instead. Then in the template:
<li class="<%= "active" if #active_page == "Home" %>">...</li>
The easiest (and possibly the most rudimentary) is to use current_page?. You can read up in the docs to see how it works. As I've experienced, it does not always produce what you want.
There is also a gem, navigasmic, which works pretty well if your needs get fairly complex.
There are a number of ways to accomplish what you are trying to do. Try out current_page? first and if you find yourself ripping your hair out, move on to something like navigasmic.
you can create a siple helper like this
def nav_link(name, url)
selected = url.all? { |key, value| params[key] == value }
link_to(name, url, :class => (selected ? "youarehere tabs" : "tabs"))
end
<div id="tabs" class="tabs2">
<%= nav_link %>
<%= nav_link %>
<%= nav_linl %>
</div>
then add the necessary css styles
Related
I have this markup in my view file:
<ul class="nav navbar-nav navbar-right">
<li class="nav-item">Restaurants</li>
<li class="nav-item">Contact</li>
<li class="nav-item">Logout</li>
</ul>
To conditionally display the Logout link, I put the single line if
<li class="nav-item">Logout</li> <% if condition %>
This did not work and gave error.
The full if form is working normally
<% if condition %> <li class="nav-item">Logout</li> <% end %>
Any ideas why the shorthand does not work in views?
CASE 1:
<li class="nav-item">Logout</li> <% if condition %>
The html statement is executed and then the ruby code injection is encountered. The preceding HTML code is not syntactically related to the if condition
following it. If you were to change it into something like:
<% '<li class="nav-item">Logout</li>'.html_safe if condition %>
It would work as you are expecting it to. Here the html block is a part of the statements that need to be executed after the if condition has been evaluated to true
* html_safe is required to allow the block to be treated as an HTML block and not a string.
CASE 2:
The full if form is working normally
<% if condition %> <li class="nav-item">Logout</li> <% end %>
The if condition is encountered first, if it evaluates to false, all code between if and end is skipped and execution continues from the end of the block. Hence the HTML is not rendered.
Sure, that part before the if is not a Ruby expression. It has nothing to do with the if, so it expects the full form.
Anyway, this should work in pure Ruby but it's not so good, I know.
<%= content_tag(:li, class: 'nav-item') { link_to('Logout', your_path) } if condition -%>
Ok so I've encountered a very very weird problem that has got me staying up late but I finally figured out whats wrong however, the solution is not ideal and I would like to know what is happening. Better yet, how to fix it.
I have a page seperated with many div tags with the id class. I have other pages that are done the same way with no problems at all when I try to link to a particular section of a page from another page with the following methods. However, the problem occurs when the page Im linking to has not enough content.. but still is at least scrollable length.. When clicking on the links that way, instead of moving to the desired section, it just moves towards the bottom.
For instance if you clicked on this website, http://aquaticshelp.com/aquascape/getting-started#aquascape-main-plants and clicked on the "Preparing your plant section" you will see what I mean. This is a very undesirable behavior and Im not sure where to go from here.
<%= link_to 'Section 1', route_path(anchor: "section-1"), class: "h4" %>
<div id="section-1">
</div>
<div id="section-2">
</div>
<div id="section-3">
</div>
EDIT: Added some screen shots for clarity of question
Below is where you click and contains the following code
<%= link_to aquascape_plants_prep_path(anchor: "aquascape-plants-prep-intro") do %>
<%= image_tag("aquascaping/start/plant_prep_img.jpg", size:"140x140", class: "img-circle center-block")%>
<%= link_to 'Preparing the Plants', aquascape_plants_prep_path(anchor: "aquascape-plants-prep-intro"), class: "h4" %>
<% end %>
The following is where it directs me to regardless of the anchor tag
Where I want it to go, and it does if I click on the following
<%= link_to 'Intro', "#aquascape-plants-prep-intro" %>
I'm trying to create a page where links are generated dynamically and added into existing DIV element in my masterpage.
Currently, I'm using Html.ActionLink() to display links, but I want to do it dynamically when the page loads.
In my code behind, I cannot reference menu item to add links to it. Is there anything I'm missing?
This is the fragment from my master page now.
<div id="menu" runat="server">
<ul>
<li><%: Html.ActionLink("Page1","Page1","Home") %></li>
<li><%: Html.ActionLink("Page2","Page2","Home") %></li>
<li><%: Html.ActionLink("Page3","Page3","Home") %></li>
</ul>
</div>
Right now, I'm just trying to come up with a basics so, I'm simply trying to get just one link displayed. Later on, I will want to replace all the ActionLinks to be generated from code behind.
This is the part from my master.cs to generate just one link for now:
String str = Url.Action("Page1", "Home");
HyperLink hp = new HyperLink();
hp.NavigateUrl = str;
hp.Text = "Page1";
menu.Controls.Add(hp);
I found the solution. What I needed to do is just to find my div control on the page and add the link control to it:
HtmlGenericControl div = this.FindControl("menu") as HtmlGenericControl;
div.Controls.Add(hp);
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
In my ruby on rails application, I have a controller hiring and in the view tree hiring controller have one page new and I add another page called viewhiring also
and I have added two tabs in both hiring and new page i.e below:
<div id="nave">
<ul id="menu">
<li class="sub">View Hiring
</li>
<li class="sub">Hiring //new page that I call hiring
</li>
</ul>
</div>
And I want to redirect from Hiring page to 'View Hiring' page using anchor View Hiring but its not working.
kindly help me, waiting for reply.
Thanks.
Helpers
You should read up about Rails URL helpers - you shouldn't be using <a href=""> in your view files, you can use <% link_to %> instead (as pointed out by #deepti Kakade):
<%= link_to "Hiring", templates_path %>
You really need to use all the rails helpers (there are many more than just link_to) in place of your HTML because they firstly help you keep your code DRY, but secondly ensure you're keeping up with the latest Rails developments
One of the main benefits of using a framework such as Rails is that it gives you the ability to focus on creating an amazing system, rather than worrying about small coding complications
--
Routes
Secondly, you have to consider your routes
#config/routes.rb
resources :hiring #-> hiring_path / domain.com/hiring/index
Rails' routing uses a resourceful structure - meaning it allows you to build a set of routes around "resources" in your application. Simply, "resources" are your controllers; but they're really your individual data records:
In this sense, you should look at which path you're using, as it will correspond directly to your routes
Use link_to, it generates the anchor tag of html, for example
<%= link_to "linktext", action_path %>
your action_path is nothing but the href.
try this
<div id="nave">
<ul id="menu">
<li class="sub"> <%= link_to 'View Hiring', hiring_viewhiring_path %></li>
<li class="sub"><%= link_to 'Hiring', templates_path %></li> //new page that I call hiring
</ul>
</div>
Hope it helps .
'View Hiring'= is the name posted .
hiring_viewhiring_path = is the path
eg. welcome_index_path
this path is in \app/views/welcome/index.html
Did you get it ?
Hope this helps
link_to(body, url, html_options = {})
# url is a String; you can use URL helpers like
# posts_path
Using Refinery CMS to create product pages in our prototype. An admin can add a link to the main product page, and it will display similar to
Product Links
www.example.com/product/1
www.example.com/product/2
here is a screenshot of how it currently is being displayed
However, there will not always be a case when the ink is added. And this looks weird to have that field but no links in there because every element has margin-bottom:30px;
So my question is how do I make the element not show up at all if nothing is passed to it. Here is the code for the element:
HTML
<div class="contentPageElement">
<h3>Product Links</h3>
<%= link_to #discussion.link.to_s, #discussion.link %>
</div>
you can either put it in helper,or do something like this.
<% unless #discussion.link.empty? %>
<div class="contentPageElement">
<h3>Product Links</h3>
<%= link_to #discussion.link.to_s, #discussion.link %>
</div>
<% end %>
I think this is what you're looking for: http://apidock.com/rails/ActionView/Helpers/UrlHelper/link_to_unless