I am trying to refactor some of the code in my view
<% if controller.controller_name == "overview" %>
<div id="left-menu">
<ul>
<li>Office</li>
<hr>
<li><%= link_to "Overview", root_path %></li>
<li><%= link_to "Personnel", personnel_path %></li>
<li><%= link_to "Results", results_path %></li>
<li><%= link_to "Statistics", statistics_path %></li>
</ul>
<ul>
<li>Economy</li>
<hr>
<li>Finances</li>
<li>Contracts</li>
<li>Transfers</li>
<li>Sponsors</li>
</ul>
</div>
<% elsif controller.controller_name == "market" %>
<div id="left-menu">
<ul>
<li>Items</li>
<hr>
<li><%= link_to "Engines", market_engines_path %></li>
<li><%= link_to "Weapons", market_weapons_path %></li>
<li><%= link_to "Armor", market_armor_path %></li>
<li><%= link_to "Chips", market_chips_path %></li>
</ul>
<ul>
<li>Personnel</li>
<hr>
<li><%= link_to "Drivers", market_drivers_path %></li>
<li><%= link_to "Servicemen" %></li>
<li><%= link_to "Programmers" %></li>
<li><%= link_to "Managers" %></li>
</ul>
</div>
<% end %>
Where each <li> corresponds to a method in the controller. I would like to be able to add new methods to my controllers, and then have them dynamically inserted in the view. So is there a way to iterate over the methods in a controller?
As per the answer provided in the referenced question, Controller.action_methods seems to be the method you need, however, to integrate into your code is as follows:
<% lists = %w(items personell) %>
<% for list in lists do %>
<ul>
<li><%= list.titleize %></li>
<hr>
<% items = MarketsController.action_methods %>
<% for item in items do %>
<li><%= link_to item.titleize, eval("market_#{item}_path") %></li>
<% end %>
</ul>
<% end %>
Related
I am setting up my navbar link_to's and I'm trying to stop a link being rendered if current_path is the same as link_path or if current path is the same as root path, as root path is defined as that same as the link path, as below:
_navbarhtml.erb
<ul class="nav navbar-nav navbar-right">
<% if user_signed_in? %>
<li><%= link_to_unless_current('My Quotes', quotes_path(current_user)) do %></li>
<% end %>
<li><%= link_to_unless_current('New Quote', new_quote_path) do %></li>
<% end %>
<li><%= link_to('My Account', edit_user_registration_path) %></li>
<li><%= link_to "Sign out", destroy_user_session_path, :method => :delete %></li>
<% else %>
<li><%= link_to('Sign in', new_user_session_path) %></li>
<li><%= link_to('Sign up', new_user_registration_path) %></li>
<% end %>
</li>
routes.rb
root 'quotes#new'
Any neat suggestions as to how to nicely write this?
You can try current_page?. Create a helper method like so:
def link_to_unless_current(text, url, options={})
if current_page?(url)
# do something else? maybe create a text which does not have a link?
else
link_to text, url, options
end
end
Now, view will be like:
<%= link_to_unless_current('My Quotes', quotes_path(current_user)) %>
Feel free to change the name of helper method.
Thanks Surya, this is the way i got it to work in the end:
application_helper.rb
def link_to_unless_current_or_root(text, url)
if current_page?(url)
elsif current_page?(root_path)
else
link_to text, url
end
end
_navbar.html.erb
<li><%= link_to_unless_current_or_root('New Quote', new_quote_path) %></li>
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' %>
I currently have 3 loops im in my _header I have
if current_user.admin? && !current_user?(user)
Also
<% if logged_in? %>
And
<% else %>
It all works fine with no error if i take out
<% if current_user.admin? && !current_user?(user) %>
<li><%= link_to "Admin", users_path %></li>
</end>
When I paste it back in i get the error. I dont know whats wrong with this syntax.
Can anyone help?
<ul class="nav navbar-nav navbar-right">
<% if current_user.admin? && !current_user?(user) %>
<li><%= link_to "Admin", users_path %></li>
</end>
<% if logged_in? %>
<li><%= link_to "Dash", users_path %></li>
<ul class="nav navbar-nav">
<li class="dropdown">
Items<span class="caret"></span>
<ul class="dropdown-menu">
<li><%= link_to "New Item", new_item_path %></li>
<li><%= link_to "Edit Items", user_items_path %></li>
<li>Mass Upload Items</li>
<li>Upload Item Images</li>
</ul>
</li>
</ul>
<ul class="nav navbar-nav">
<li class="dropdown">
Profile<span class="caret"></span>
<ul class="dropdown-menu">
<li><%= link_to "View Profile", user_path(current_user) %></li>
<li><%= link_to "Edit Profile", edit_user_path(current_user) %></li>
</ul>
</li>
</ul>
<li><%= link_to "Messages", users_path %></li>
<li class="dropdown">
Settings<span class="caret"></span>
<ul class="dropdown-menu">
<li>Edit Money Back Policies</li>
<li>Edit Warranty Policies</li>
</ul>
</li>
<li><%= link_to "Log out", logout_path, method: "delete" %></li>
<% else %>
<li><%= link_to "Log In", login_path %></li>
<li>Sign Up</li>
<% end %>
</ul>
</div><!-- /.navbar-collapse -->
</div><!-- /.container-fluid -->
</header>
You're not properly closing the if you pasted :
<% if current_user.admin? && !current_user?(user) %>
<li><%= link_to "Admin", users_path %></li>
</end>
You should try to replace that by :
<% if current_user.admin? && !current_user?(user) %>
<li><%= link_to "Admin", users_path %></li>
<% end %>
structure of if loop in ruby.your end tag of if loop is incorrect.see below
<%if%>
//condition here
<%end%>
can you try this out:
<% if logged_in? && !current_user?(user) && current_user.admin? %>
<li><%= link_to "Admin", users_path %></li>
</end>
Syntex error in closing of if block syntex.
1 <% if current_user.admin? && !current_user?(user) %>
2 <li><%= link_to "Admin", users_path %></li>
3 </end>
Error at line 3, replace line 3 ie </end> with <% end %>
Im trying to add font-awsome icons to my (link_to) links within a simple navbar i created, however im not sure as to how to correctly write the code using ruby syntax as apposed to the html. This is the code I would like to add the icons to:
<nav>
<ul>
<li><%= link_to "Main", root_path %></li>
<li><%= link_to "Employee Profile", profile_path %>
<ul>
<li><%= link_to "Tasks / Activities", "#" %></li>
<li><%= link_to "Vacations", "#" %></li>
</ul>
</li>
<li><%= link_to "Projects", projects_path %></li>
<li><%= link_to "Projects Documents", project_docs_path %></li>
<li><%= link_to "Search", search_path %></li>
<li><%= link_to "Reports", reports_path %></li>
<li><%= link_to "Feedback", feedback_path %></li>
</ul>
I would like each one of my (link_to) tags to have its own font-awsome icon. As an exmample, could you provide me with the correct syntax to include the following icon to my "Employee Profile" section:
<i class="fa fa-user"></i>
Here is a link to the icon's page:
http://fortawesome.github.io/Font-Awesome/icon/user/
Thanks!
Link_to tag can be used as a 'block'. You can use in this way to include font-awesome in link_to
<%= link_to profile_path do %>
<i class="fa fa-user"></i>Employee Profile
<% end %>
My question assumes a specific answer but it may not but accurate.
I've created a route:
get 'users/:id/groups/' => 'users#groups', as: "my_groups"
but when I go to access this path, conditionally, in a view, i get an exception:
ActionController::UrlGenerationError at /
No route matches {:controller=>"users", :action=>"groups"} missing required keys: [:id]
Here is the partial with the offending line:
<% if user_signed_in? %>
<li id="groups-menu" class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">
Groups <b class="caret"></b>
</a>
<ul class="dropdown-menu">
#offending line:
<li><%= link_to 'My Groups', my_groups_path %></li>
<li><%= link_to 'Create a Group', new_group_path %></li>
</ul>
</li>
<li><%= link_to 'Logout', destroy_user_session_path, :method=>'delete' %></li>
<% else %>
<li><%= link_to 'Login', new_user_session_path %></li>
<% end %>
<% if user_signed_in? %>
<li><%= link_to 'Edit account', edit_user_registration_path %></li>
<% end %>
<% if user_signed_in? %>
<% if current_user.has_role? :admin %>
<li><%= link_to 'Admin', users_path %></li>
<% end %>
<% end %>
it seems that there is no current_user set, perhaps?
It's unclear how this could break if the user_signed_in? method is working.
You need to pass in the :id parameter to the url helper function
<li><%= link_to 'My Groups', my_groups_path(current_user) %></li>