How to embed instance method with link_to text field? - ruby-on-rails

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>

Related

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

Pagination in rails 3

In my rails application I need to display the matching tweets .lets say for example the matching results has 50 records, I need to display 10 records per page.I am getting the output which has all results but when I use pagination its showing link to different pages , but when I click the link to next page it says "string not matched". I tried different combinations like 5 per page ,but when I click the link to the next page it says "string not matched", but when I try without pagination it shows all the results
My code for the controller
class TweetsController<ApplicationController
def index
city = params[:show]
search_term = params[:text]
search_term[" "] = "%"
#tweets = Tweets.where("tweet_text LIKE? ", "%#{search_term}%").paginate( page: params[:page], per_page: 3)
My code for the view
<%= will_paginate #tweets %>
<% #tweets.each do |tweets| %>
<ul>
<li><%= tweets.id %></li>
<li><%= tweets.tweet_created_at %></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>
Anyone please help me with this
You have an error in
search_term[" "] = "%"
line. If it should replace whitespace with "%", it should be:
search_term.gsub!(/\s/, '%')

Rails use ruby in link_to

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>

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