I have the current code in my traders.index.html file
<ul>
<% #traders.each do |trader| %>
<li><%= link_to trader.name, trader %></li>
<%end%>
</ul>
I want to add an extra parameter to be sent through, I tried
<li><%= link_to trader.name, trader, {:restricted => params[:s]} %></li>
But this doesn't send the parameter, whats the actual format of the link_to to get this done?
You can do:
<%= link_to trader.name, trader_path(trader, restricted: params[:s]) %>
Related
I am looking to pass this into the controller so I don't have to repeat or ask for path names.
<ul class="nav navbar-nav">
<li><%= link_to 'Breakfast', '/breakfast', class: ('active' if request.path == breakfast_path) %></li>
<li><%= link_to 'Lunch', '/lunch', class: ('active' if request.path == lunch_path) %></li>
<li><%= link_to 'Deli', '/deli', class: ('active' if request.path == deli_path) %></li>
<li><%= link_to 'Contact', '/contact', class: ('active' if request.path == contact_path) %></li>
</ul>
Gems will not be considered an answer...
Thanks!
I'm not sure passing it into the controller is a good solution - it's concerned with view logic so I think the view is the right level to handle it on. Perhaps creating a helper method that wraps link_to would be the right approach. Here's an idea without having tested it:
# YourHelper
def nav_link_to(label, path)
link_to(label, path, class: 'active' if path == request.path)
end
You could use the active_link_to gem:
<% food = %w(breakfast lunch deli contact) %>
<ul class="nav navbar-nav">
<% food.each do |meal| %>
<li><%= active_link_to meal.titleize, eval("#{meal}_path") %></li>
<% end %>
</ul>
This is my current page view below:
<%= link_to "Back", root_path, :class => "pull-right time-padding" "btn btn-primary btn-sm"%>
<div class="entries">
<% #entries.each do |entry| %>
<h2><%= link_to entry.competition.name, entries_path %></h2>
<li><%= link_to entry.competitor.full_name %></li>
<br/>
<% end %>
</div>
I would like to display only unique entry.competition.name and then all the associated entry.competitor.fullname. Currently it loops over every entry of both and displays them.
I checked how to display individual listings? Ruby on Rails and how to display a link to individual microposts? (ruby on rails 3), but am not sure how those connect to the function I am seeking.
I am looking at link_to entry.competition.name.uniq, but I cannot grok how to implement something like this.
This will get one value for each competition name:
#entries.reduce({}){|m,e| m[e.competition.name] = e.competition; m}.values
You can do likewise for full.name
How can I go about embedding font-awesome icons into my Ruby <%= link_to code?
The below does NOT work, is it possible to accomplish the below some how so that it actually works?
<li><%= link_to "<span class="fa fa-minus-circle fa-1x"></span> Settings</span>", destroy_user_session_path, :method => :delete %></li>
Thanks!
Johnson
have you try this:
<li><%= link_to your_path do %><span class="fa fa-minus-circle fa-1x"></span> Settings <% end %></li>
for instance with font-awesome in some of my codes:
<li><%= link_to edit_contact_path(#contact) do %><i class="fa fa-pencil-square-o"></i> Edit<% end %></li>
<li><%= link_to #contact, method: :delete, data:{confirm: "Delete this contact?"} do %><i class="fa fa-exclamation-triangle"></i> Delete<% end %></li>
Here is the documentation, see the section with link_to ... do .... end
link to documentation
I've always done it including the class in the Ruby code like this,
<li><%= link_to " Sign Up", '#', class: "fa fa-user-plus" %></li>
This includes the icon on the left of the text so you have to include the space preceding "Sign up" in order to get it to look right.
Better option for you is use this gem:
https://github.com/h4b00/awesome_link
After installation you can simply use(for example):
<%= awesome_link('fa-pencil-square-o', root_path, method: :update) %>
coordinates GET /coordinates(.:format) coordinates#index
POST /coordinates(.:format) coordinates#create
new_coordinate GET /coordinates/new(.:format) coordinates#new
edit_coordinate GET /coordinates/:id/edit(.:format) coordinates#edit
coordinate GET /coordinates/:id(.:format) coordinates#show
PUT /coordinates/:id(.:format) coordinates#update
DELETE /coordinates/:id(.:format) coordinates#destroy
tweets_search GET /tweets/search(.:format) tweets#search
tweets_index GET /tweets/index(.:format) tweets#index
class TweetsController<ApplicationController
def index
#include 'coordinates_controller.rb'
include SearchHelper
include ParamasHelper
#sql=a.search
#tweets=Tweets.paginate_by_sql(sql, :#page, :per_page => #per_page ).all
end
end
In my Rails app, I have two tables named Coordinates and Tweets. I have four actions to be rendered.
My routes.rb file
Tweetsblog2::Application.routes.draw do
resources :tweets, :coordinates
get "tweets/show"
get "tweets/index"
match "/tweets/show" => "tweets#show"
match "/tweets/index" => "tweets#index"
Whenever I navigate to http://localhost:3000/tweets, it's showing tweets/index instead of tweets/show and the same error I am getting with different names.
When I navigate to http://localhost:3000/tweets/show, it's giving ArgumentError in TweetsController#show.
When I navigate to http://localhost:3000/tweets/index its giving ArgumentError in TweetsController#show same thing
My code for show.html.erb:
<%= form_tag({controller: "tweets", action:"index" }, method: "get") do %>
<%= label_tag(:search, "search for:") %>
<%= text_field_tag(:show) %>
<%= text_field_tag(:search) %>
<%= submit_tag("get results ") %>
<% end %>
My code for index.html.erb:
<%= will_paginate #tweets %>
<% #tweets.each do |tweets| %>
<ul>
<li><%= tweets.id %></li>
<li><%= tweets.tweet_created_at %></li>
<li><%= tweets.tweet_id %></li>
<li><%= tweets.tweet_source %></li>
<li><%= tweets.tweet_text %></li>
<li><%= tweets.user_id %></li>
<li><%= tweets.user_name %></li>
<li><%= tweets.user_sc_name %></li>
<li><%= tweets.user_loc %></li>
<li><%= tweets.user_img %></li>
<li><%= tweets.longitude %></li>
<li><%= tweets.latitude %></li>
<li><%= tweets.place %></li>
<li><%= tweets.country %></li>
<% end %>
</ul>
It's not routing to the proper page. Please help me, I am stuck with this.
you have to write
resources :tweets, except: [:index,:show]
because you declared you resource first, so rails is trying to match its default routing instead of your custom action:
get "tweets/index"
-updating per comment-
Tweetsblog2::Application.routes.draw do
resources :coordinates
get "tweets/show" => "tweets#show"
get "tweets/index" => "tweets#index"
Removing the resources :tweets should fix your issue and allow you to use just call the 2 options. The initial resources :tweets tells rails that you wanted resourceful routes (index shows all of the resource, show a specify one, etc..). So just building out the 2 non-resourceful routes as above sounds like what you want. The way I normally do something like this is to just include the search form in in the index page and if there are no search params then show all tweets.)
Right now I have a navigation partial that looks like this (x10 buttons)...
<% if current_controller == "territories" %>
<li><%= link_to "Territories", {:controller => 'territories'}, :class => 'active' %></li>
<% else %>
<li><%= link_to "Territories", {:controller => 'territories'} %></li>
<% end %>
<% if current_controller == "contacts" %>
<li><%= link_to "Contacts", {:controller => 'Contacts'}, :class => 'active' %></li>
<% else %>
<li><%= link_to "Contacts", {:controller => 'Contacts'} %></li>
<% end %>
Is there a more elegant/DRY solution for doing this?
In a similar vein to what Chuck said:
<% TARGETS.each do |target| %>
<li>
<%= link_to target.humanize,
{ :controller => target },
class => ('active' if current_controller == target)) %>
</li>
<% end %>
It's pretty easy to see where the repetition is in there. It's all of the general form:
<% if current_controller == XXXXX %>
<li><%= link_to XXXXX, {:controller => XXXXX}, CLASS %></li>
<% else %>
[do the same stuff minus ":class => 'active'"]
<% end %>
So we want XXXXX and CLASS to be variables (since those are the only things that change) and the rest can be a simple template.
So, we could do something like this:
%w(Contacts Territories).each |place|
<% class_hash = current_controller == place ? {:class => 'active'} : {}
<li><%= link_to place, {:controller => place}, class_hash)</li>
Check out rails-widgets on github. It provides a ton of convenience helpers for rails UI stuff (tabnavs, tooltips, tableizers, show hide toggle, simple css progressbar) in addition to navigation.
Here are the docs
Check out link_to_unless_current. Not exactly what you asked for, but it's close.
Also, you could put this kind of logic in a helper to abstract it out of the view.
Check out the simple-navigation plugin. It's an 'easy to use' rails plugin for creating navigations for your rails apps.
A slightly different version w/ link_to_unless_current:
<ul>
<% links.each do |link| -%>
<li><%= link_to_unless_current link.humanize, { :controller => target } %></li>
<% end -%>
</ul>
A good resource for stuff like this are the rails docs.