i am rendering an external link with this view:
<%= link_to public_web(company), class:" text-sm text-decoration-none", :target => "_blank" do %>
<i class="fa fa-globe mr-3"></i>
<span class="text-muted"><%= "#{public_web(company)}" %></span>
<% end %>
and the following helper
def public_web(company)
URI::HTTPS.build({:host => company.marketings.first.try(:website)}).to_s
end
However the url length breaks the UI design (I would like to control the maximum length)
How can I truncate it ?
Solution :
I modified the helper as follows:
def public_web(company)
response = URI::HTTPS.build({:host => company.marketings.first.try(:website)}).to_s
truncate(response, length: 25, omission: '...')
end
If you're just looking to truncate the text of the link then this is one way
<%= link_to public_web(company).truncate(20), class:" text-sm text-decoration-none", :target => "_blank" do %>
<i class="fa fa-globe mr-3"></i>
<span class="text-muted"><%= public_web(company) %></span>
<% end %>
Read more on the truncate method in Rails
Related
I have a screen where a user can select differente kinds of plans for his account. Like this:
#plans
<% Plan.all.order(:amount).each do |plan| %>
<%= render 'shared/plan_detail', {plan: plan, button_text: 'Choose this' } %>
<% end %>
#plan_detail
<div class="plan-details">
<header class= <%= plan.css %> >
<h3><%= plan.name %></h3>
<small><%= plan.visibility %></small>
<p><%= plan.card_description.html_safe %></p>
<span class="plan-price"><sup>$</sup><%= plan.amount.to_s %></span>
</header>
<article>
<%= plan.features_description.html_safe %>
<%= link_to button_text, {:controller => "accounts", :action => "update_plan", :plan => plan.id }, title: button_text, :class=>"btn btn-md btn-block btn-outline-green dialog-open" %>
</article>
</div><!-- end plan details -->
And In my controller i have:
#accounts_controller.rb
def update_plan
#plan = Plan.find(params[:plan])
current_user.plan = #plan
current_user.save
end
My routes its like this
get '/account/plans', to: 'accounts#plans', as: :update_plan
put '/account/plans', to: 'accounts#update_plan'
But I click on the button, and nothing happens. What Im doing wrong here?
This is a long shot, but seeing that your link has dialog-open I wouldn't be surprised if there was some Javascript preventing your link from working. In order to debug this further I would
a) Check browser's Javascript console for any errors
b) Remove the dialog-open class to see what happens
I have a Rails 3.2.22 app that tracks dispatch calls and each call belongs to a region and a region has many calls. In my views I originally had all calls from all regions, but now I'm filtering by regions using a simple form_tag in the view and passing the region ID as a param to the controller and back to the view.
So locally if I hit the calls index view I will trigger a url like:
http://loopify.xyz:9001/calls?utf8=%E2%9C%93®ion=1
Which will then in my views show me all calls with the region id of "1". I can switch to different regions in the views and it will display the proper filtered calls by region.
When I deployed this to my staging server, the params filtering does not work at all and shows all calls even though when I select a region I will get a URL like:
http://staging.example.com/calls?utf8=%E2%9C%93®ion=1
Here is the index action of my calls controller:
def index
if params[:region].present?
#region = params[:region]
#assigned = Call.includes(:units, :transferred_from, :transferred_to, :nature, :region, :service_level).where(region_id: params[:region]).assigned_calls.until_end_of_day
#unassigned = Call.includes(:units, :transferred_from, :transferred_to, :nature, :region, :service_level).where(region_id: params[:region]).unassigned_calls.until_end_of_day
else
params[:region] = "1"
#assigned = Call.includes(:units, :transferred_from, :transferred_to, :nature, :region, :service_level).where(region_id: params[:region]).assigned_calls.until_end_of_day
#unassigned = Call.includes(:units, :transferred_from, :transferred_to, :nature, :region, :service_level).where(region_id: params[:region]).unassigned_calls.until_end_of_day
end
#note = Note.new
#units = Unit.active.order("unit_name").map{|unit| unit.calls.where(call_status: "open").empty? ? ["#{unit.unit_name} #{unit.unit_type.unit_type} #{unit.status.unit_status}", unit.id] : ["#{unit.unit_name} (on call) #{unit.unit_type.unit_type} #{unit.status.unit_status}", unit.id] }
end
Here is my index.html.erb view:
<div id="active">
<%= render "assigned_calls" %>
</div>
<div id="inactive">
<%= render "unassigned_calls" %>
</div>
<script>
$(function() {
setInterval(function(){
$.getScript('/calls/?region=<%= params[:region] %>')
}, 20000);
});
</script>
Here is the index.js.erb file to allow ajax refresh and JS
$("#active").html("<%= escape_javascript render("assigned_calls") %>");
$("#inactive").html("<%= escape_javascript render("unassigned_calls") %>");
Here is an excerpt of the assigned partial. Both assigned and unassigned really large to display here in their entirety but will be happy to supply them in a github gist if you need more context.
<div class="page-header well">
<h3><%= pluralize(#assigned.size, "Active Call") %></h3>
</div>
<%= form_tag calls_path, :method => 'get' do %>
<%= select_tag "region", options_from_collection_for_select(Region.order(:area), :id, :area, selected: #region), prompt: "Choose Region" %>
<%= submit_tag "Select", :name => nil, :class => 'btn' %>
<% end %>
<% #assigned.each_with_index do |call, index| %>
<div class="widget">
<div class="widget-header">
<div class="pull-right">
<%= link_to 'View', call, :class => 'btn btn-primary btn-small'%>
<% if dispatch? || admin? || manager? || operations? %>
<%= link_to 'Edit', edit_call_path(call), :class => 'btn btn-info btn-small'%>
<%= link_to "Close", '#close-modal', data: {toggle: "modal", target: "#close-modal#{index}" }, class: 'btn btn-small btn-warning' %>
<%= render 'layouts/close_call_modal', call: call, index: index %>
<%= link_to "Cancel", '#cancel-modal', data: {toggle: "modal", target: "#cancel-modal#{index}" }, class: 'btn btn-small btn-danger' %>
<%= render 'layouts/cancel_call_modal', call: call, index: index %>
<%= link_to "Notes", '#note-modal', data: {toggle: "modal", target: "#note-modal#{index}" }, class: 'btn btn-small btn-primary' %>
<%= render 'layouts/note_modal', call: call, index: index %>
<% end %>
</div>
<i class="icon-phone"></i>
<h3><%= link_to call.incident_number, call %> <span><%= status(call) %></span></h3>
<% if call.wait_return == "yes" && call.parent_call_id == nil %>
<i class="icon-arrow-right dim"></i> <span class="badge badge-important" >Initial Transport</span>
<% end %>
<% if call.wait_return == "yes" && call.parent_call_id.present? %>
<i class="icon-arrow-left dim"></i> <span class="badge badge-important" >Return Transport</span>
<% end %>
<% if call.traffic_type == "Emergency" %>
<span class="badge badge-important"><%= call.traffic_type %></span>
<% else %>
<span class="badge badge-info"><%= call.traffic_type %></span>
<% end %>
<span class="badge badge-primary" ><%= call.region.try(:area) %></span>
<span class="badge badge-info" ><%= call.service_level.try(:level_of_service) %></span>
</div>
<% end %>
I'm really not sure what the problem is here. In development I'm serving the app with Thin, in staging I'm using passenger and nginx.
Is there something in my code that will not work in a production environment that I'm missing?
To recap, the region filtering works perfectly in development but once deployed to staging the filtering does not work.
I dug into the logs and here is the request from both dev (local) and prod (staging) logs:
dev:
Started GET "/calls/?region=1&_=1468278029235" for 127.0.0.1 at 2016-07-11 18:00:29 -0500
Processing by CallsController#index as JS
prod:
Started GET "/calls/?region=1&_=1468278074295" for 192.168.130.1 at 2016-07-11 18:01:14 -0500
Processing by CallsController#index as JS
I'm not sure why this even happens/works but it does. If I turn the log level in production.rb from info to debug the param filtering works fine. I saw a reference to this on an old github issue. Rails core closed it as a non-bug, but obviously there's something to this. Unfortunately 3.2 is not supported anymore so this "hack" will have to do. Now it's time to upgrade the app to 4.2.6.
I'm trying to have a Font Awesome icon be hyperlinked with the string of a Rails attribute.
I have tried this,
<%= link_to do %>
<i class="fa fa-link"><% school.website %></i>
<% end %>
this,
<i class="fa fa-link" href="<% school.website %>"></i>
and this:
<i class="fa fa-link" href="<%= school.website %>"></i>
among other variations, and can't seem to get the syntax right.
Would really appreciate some help with the syntax here, can't seem to find a specific answer to how linking with attributes works, only actual static text hrefs.
PS: I'm new to Rails / using Rails 4.2.
Try this:
<%= link_to ('<i class="fa fa-link"></i>').html_safe, desired_path %>
Considering school.website is a URL and you have included icons in CSS, the following will do:
<%= link_to(school.website) do %>
<i class="fa fa-link"></i>
<% end %>
Passing it as a block works too.
<%= link_to school.website do %>
<%= '<i class="fa fa-link"></i>'.html_safe %>
<% end %>
Assuming school.website is a URL that you would like to link to and display the link as well alongside the link icon from font awesome:
<%= link_to("<i class='fa fa-link'></i> #{school.website}".html_safe,school.website) %>
I am going to suggest something a little different.
I am personlly using glyphicons/fa a lot on my website, so I decided to create a little helper in application_helper.rb
application_helper.rb
def fa(glyph, text = nil)
html = "<i class=\"fa fa-#{glyph}\"></i>"
html << " #{text.to_s.html_safe}" if text
html.html_safe
end
And actually the correct syntax to use in views becomes either :
<%= link_to(school.website, class: "xxx") do %>
<%= fa('link') %>
<% end %>
OR (more compact)
<%= link_to(school.website, class: "xxx"){ fa('link') } %>
You can use a block as well if your link target is hard to fit into the name parameter. ERB example:
try this:
<%= link_to(school.website) do %>
<%= fa_icon "fas link" %>
<% end %>
I'm using Twitter's Bootstrap stuff and I have the following HTML:
<a class="btn" href="<%= user_path(#user) %>"><i class="icon-ok icon-white"></i> Do it#</a>
What's the best way to do this in Rails? I'd like to use <%= link_to 'Do it', user_path(#user) %> but the <i class="icon-ok icon-white"></i> is throwing me off?
Two ways. Either:
<%= link_to user_path(#user) do %>
<i class="icon-ok icon-white"></i> Do it#
<% end %>
Or:
<%= link_to '<i class="icon-ok icon-white"></i> Do it#'.html_safe, user_path(#user) %>
I had the same need recently. Try this:
<%= link_to '<i class="icon-ok icon-white"></i> Do it'.html_safe, user_path(#user) %>
You have also the possibility to create an helper method like below:
def link_fa_to(icon_name, text, link)
link_to content_tag(:i, text, :class => "fa fa-#{icon_name}"), link
end
Adapt the classes to your needs.
In normal HTML we do,
<i class="fa fa-user-plus"></i> Register
In Ruby On Rails:
<%= link_to routeName_path do %>
<i class="fa fa-user-plus"></i> Link Name
<% end %>
<%= link_to register_path do %>
<i class="fa fa-user-plus"></i> Register
<% end %>
If you want a link in rails that uses that same icon class from twitter bootstrap all you need to do is something like this.
<%= link_to "Do it#", user_path(#user), :class => "btn icon-ok icon-white" %>
Using HAML:
= link_to model_path do
%img{src: '/assets/someimg.png'}
In the gem twitter-bootstrap-rail : they create a helper glyph
def glyph(*names)
content_tag :i, nil, :class => names.map{|name| "icon-#{name.to_s.gsub('_','-')}" }
end
So you can use it like: glyph(:twitter)
and you link helper could look like: link_to glyph(:twitter), user_path(#user)
I will give this a shot since you haven't accepted an answer yet
and the other answers are not 100% what you were looking for.
This is the way to do it the Rails way.
<%= link_to(user_path(#user), :class => 'btn') do %>
<i class="icon-ok icon-white"> </i> Do it!
<% end %>
Edit: leaving my answer for future reference,
but #justin-herrick has the correct answer when
working with Twitter Bootstrap.
I think you can simplified it through a helper method if you use it frequently in your application.
put it in helper/application_helper.rb
def show_link(link_text, link_source)
link_to("#{content_tag :i, nil, class: 'icon-ok icon-white'} #{link_text}".html_safe,
link_source, class: "btn")
end
Then call it from your view file just like link_to
<%= show_link "Do it", user_path(#user) %>
If you are using the bootstrap 3.2.0, you can use this helper in your app/helpers/application_helper.rb
module ApplicationHelper
def glyph(*names)
content_tag :i, nil, :class => names.map{|name| "glyphicon glyphicon-#{name.to_s.gsub('_','-')}" }
end
end
and then, in your views:
link_to glyph(:pencil) + ' Edit', edit_post_path(#post), class: 'btn btn-warning'
def show_link (source, text)
link_to source, {'data-original-title' => 'Show', 'data-toggle' => 'tooltip', :class => 'btn btn-xs btn-success'} do
"#{text} #{content_tag :i, nil, class:' glyphicon glyphicon-eye-open' }".html_safe
end
end
Helper based on Titas Milan's suggestion, but using a block:
def show_link(link_text, link_source)
link_to link_source, { class: 'btn' } do
"#{content_tag :i, nil, class: 'icon-ok icon-white'} #{link_text}".html_safe
end
end
So I have this:
<%= link_to(image_tag(#model.picture.url(:thumb), :alt => ''), "/pages/you/#{something.id}", {:id => "y_link_#{something.id}"}) %>
Which works, but I need a span in between also like this:
<a id="y_link_2" href="/pages/you/2" class="">
<span>Apples</span>
<img src="another_small.jpg?1236340989" alt="">
</a>
How do I add
<span>Apples</span>
to the link_to?
Feed a block to your link_to call:
<% link_to("/pages/you/#{something.id}", {:id => "y_link_#{something.id}"}) do %>
<%= content_tag(:span, 'Apples') %>
<%= image_tag(#model.picture.url(:thumb), :alt => '') %>
<% end %>
Alternatively:
<% link_to("/pages/you/#{something.id}", {:id => "y_link_#{something.id}"}) do %>
<span>Apples</span>
<%= image_tag(#model.picture.url(:thumb), :alt => '') %>
<% end %>
image_tag and content_tag return basic strings, so they can be concatenated using the + operator easily:
<%= link_to(content_tag(:span, "Apples") + image_tag(#model.picture.url(:thumb), :alt => ''), "/pages/you/#{something.id}", {:id => "y_link_#{something.id}"}) %>
However, as you can see, it gets quite messy - might be worth moving it into a helper method.
For a path, use the structure like so
<%= link_to edit_section_path(#section) do %>
Edit
<span class="fa fa-list pull-right"></span>
<% end %>
Haml lends itself well to situations like these. For example:
= link_to "/pages/you/#{something.id}", id: "y_link_#{something.id} do
%span Apples
= image_tag(#model.picture.url(:thumb), alt: '')
Alternatively:
%a[something, 'y_link']{href: "/pages/you/#{something.id}"}
%span Apples
%img{src: #model.picture.url(:thumb)}
It's worth learning if you want to write your views faster and read them better.