Rails use ruby in link_to - ruby-on-rails

Instead of hard coding a link description, I would like to use some Ruby code.
This is the original:
<li><%= link_to "Open Projects List", workorders_index2_path %></li>
This didn't work for me:
<li><%= link_to "<%= current_tenant.name_workorder.capitalize.pluralize %>", workorders_index2_path %></li>
Thanks for the help!

You don't need to use quotes at all:
<li><%= link_to current_tenant.name_workorder.capitalize.pluralize, workorders_index2_path %></li>

You already ARE using ruby code. <%= %> everything inside of that is pure ruby. link_to is a ruby method, and "Open Projects List" is the first parameter to that method, it's a string. Anything you can do in ruby you can send here - don't send a string, send the variable:
<li><%= link_to current_tenant.name_workorder.to_s.capitalize.pluralize, workorders_index2_path %></li>
You can also use string interpolation like you would with regular ruby:
<li><%= link_to "Open Project #{current_tenant.name_workorder}", workorders_index2_path %></li>

Related

How to embed instance method with link_to text field?

On erb:
#student.count #--> 4
<li><%= link_to "Show All", 'all' %></li>#-->Show all
I want to show
show All(3)
.
I tried
<li><%= link_to "Show All"#students.count, 'all' %></li>
or
<li><%= link_to "Show All"+#students.count, 'all' %></li>
But all didn't work
Use interpolation
<li><%= link_to "Show All(#{#student.count})", 'all' %></li>
This doesn't work because you are concatenating a string and a number (TypeError: no implicit conversion of Fixnum into String)
"Show All" + #students.count
This works
"Show All" + #students.count.to_s
TRY
<li><%= link_to "Show All(" + #students.count.to_s + ")" , 'all' %></li>
OR
<li><%= link_to "Show All(#{#student.count})", 'all' %></li>

How to embed font-awesome icons in a navbar link

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) %>

Add param to link_to show method

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]) %>

access rails url helper from deface

I am including Spree to an existing site. I am changing the spree header with Deface to render my site's header instead. Therefore I use the following Deface DSL code
<!-- replace_contents "header[data-hook]" -->
<%= render :partial => "layouts/my_site_header.html.erb" %>
And inside _my_site_header.html.erb I have something like this
<ul>
<li><%= link_to "Home", home_path %></li>
<li><%= link_to "Game", game_path %></li>
<li><%= link_to "Community", community_path %></li>
</ul>
Which gives me the following error
undefined local variable or method `home_path' for #<#<Class:0x8a73c20>:0x8af0e58>
I understood that the code get executed by Deface in the Spree scope, thus my site's url helpers are undefined. I could solve this using the full method name like Rails.application.routes.url_helpers.home_path
However, I don't really feel like adding this for all of my links. Isn't there a way to tell Spree to include the url helpers of my site? Please help!
There is a shorter version which you can use from Rails::Engine called main_app.
<ul>
<li><%= link_to "Home", main_app.home_path %></li>
<li><%= link_to "Game", main_app.game_path %></li>
<li><%= link_to "Community", main_app.community_path %></li>
</ul>
I would highly recommend using this to avoid conflicts between your application and Spree (such as your app home vs. Spree's home).

Ruby on Rails/Passenger/Capistrano error message in rendering ERB file. Whats going on

I have recently decided to deploy my site test onto my home server using a combination of Ruby on Rails, Passenger (mod_rails)/Apache and Capistrano to deploy. I am trying to render my home page, which at the moment has no database related elements and is all very simple.
The file I'm trying to render is:
<footer>
<nav class="round">
<ul>
<li><%= link_to "About", '#', %></li>
<li><%= link_to "Contact", '#', %></li>
</ul>
</nav>
</footer>
I'm sure its simple syntax or something, but here is the error log part that I believe is important:
Rendered layouts/_footer.html.erb (9.4ms)
Completed 500 Internal Server Error in 276ms
ActionView::Template::Error (/var/rails/releases/20120309031203/app/views/layouts/_footer.html.erb:4: syntax error, unexpected ')'
...pend= ( link_to "About", '#', );#output_buffer.safe_concat('...
... ^
/var/rails/releases/20120309031203/app/views/layouts/_footer.html.erb:5: syntax error, unexpected ')'
...nd= ( link_to "Contact", '#', );#output_buffer.safe_concat('...
... ^
/var/rails/releases/20120309031203/app/views/layouts/_footer.html.erb:10: syntax error, unexpected keyword_ensure, expecting ')'
/var/rails/releases/20120309031203/app/views/layouts/_footer.html.erb:12: syntax error, unexpected keyword_end, expecting ')'):
1: <footer>
2: <nav class="round">
3: <ul>
4: <li><%= link_to "About", '#', %></li>
5: <li><%= link_to "Contact", '#', %></li>
6: </ul>
7: </nav>
app/views/layouts/application.html.erb:16:in `_app_views_layouts_application_html_erb___909272938_79472450'
I hope this is enough, but if you need more, please ask.
Thanks.
the root cause is this line of code:
<li><%= link_to "About", '#', %></li>
here, the link_to() method only accept 2 parameters ("about", "#"), however there's a "," in the end. so it got errors.
Also, if you just want to render a link such as:
About
Please use "link_to_function" instead. e.g.
<li><%= link_to_function "About", '#' %></li>
What are those commas doing there, hanging at the end of the link_to calls?
<li><%= link_to "About", '#', %></li>
<li><%= link_to "Contact", '#', %></li>
Try:
<li><%= link_to "About", '#' %></li>
<li><%= link_to "Contact", '#' %></li>

Resources