Rails 4 Generating application wide dynamic nav-bar links - ruby-on-rails

I am trying to generate links in my nav-bar based on records.
I want to post a link in the patrols section of my app that correspond to a patrol route that an admin generated?
I have tried to place
#patrol_routes = PatrolRoute.all in the application controller
and then i want something like
<% #patrol_routes.each do |patrol_route| %>
<%= link_to patrol_route.name, patrol_route_path %> so that it takes me to the show page of the patrol route i want to access?
<% end %>
Is this possible? i have tried to google and research it, but I'm not finding anything, perhaps I'm not hitting the correct key words?

Assuming you have a route (as in config/routes.rb) named patrol_route you should do:
<% #patrol_routes.each do |patrol_route| %>
<%= link_to patrol_route.name, patrol_route_path(patrol_route) %>
<% end %>

Related

Rails link from database appends to current controller

I have a model, Notification, that has two fields: text and link. In my view for notifications, I have the following:
<% #notifications.each do |notification| %>
<li>
<%= notification.text %>
<%= link_to "View", notification.link %>
</li>
<% end %>
Examples of links include:
"foos/4/bars"
"about"
"foos"
However, when I attempt to follow the link, if I am in the "baz" controller, the result is an attempt at "baz/foos/4/bars", or "baz/about", rather than just "foos/4/bars" or "about".
Is there a better way to do this, or a way to disable the appending of the link to the current controller?
You trying to get a relative path to your current controller.
Try doing this ->
<%= link_to "View", "/" + notification.link %>
Thanks to #Kumar Abinash. The path was relative without a prepending "/". Simply changed links in the database to "/..."

My webpage is showing extra table information outside the loop in ruby on rails while retrieving rows

Please help me.
My code is
<%= Region.find(:all).each do |myregion| %>
<li><%= t(myregion.name) %></li>
Database has only two records: japan and korea.But, it shows other information outside the loop like [#, #]... i dont want these information on my website unnecessarily.
How to remove this?
instead of
<%= Region.find(:all).each do |myregion| %>
use
<% Region.find(:all).each do |myregion| %>
<%= %> # render string
<% %> # execute code

Dynamically adding pages to rails app

This may be a more fundamental aspect of Rails but I am very new.
I have a basic app which is sort of a basic craigslist clone.
Users - I used devise for this, users can sign up/sign in/sign out/edit their profile.
Listings - Users can add multiple listings. Currently I have a few basic fields populating the database (title, content, phonenumber, price, location).
What I want to be able to do is the following:
I want to have a page which lists all of the ads. Currently I can do this by accessing the database and displaying all the contents. Like this:
<h2>LISTINGS</h2>
<% #users.each do |u| %>
<% u.listings.each do |i|%>
<%=u.email %>
<%=i.title %>
<%=i.content %>
<%=i.number %>
<%=i.price %>
<%=i.location %>
<% end %>
<% end %>
What I want to do is have this page only list the titles, each title would link to the appropriate ad. Users could then access their ad with a URL. Ideally the URL would be the title similar to what stackoverflow does "IE in the URL the title is included with "-" instead of spaces" but that is a minor concern at this point.
How is the best way to go about doing this? I may be using incorrect terminology here as I am having trouble finding information.
I think that you should use as below code -
Eg :
<% #users.each do |u| %>
<%= link_to u.name, :controller => 'user', :action => 'show', :id => u.id %>
<% end %>
I hope that you can solve your problem by using this way.

Is It Possible To Use Variables To Build a link_to Command? (Rails 3.2.11)

I am creating a section of my Rails application for the visually impaired. This requires me to create it using only text and links in order to make it easier for people using speech readers to navigate through. I want to use fields from an existing model to dynamically build a link_to command. I would like to be able to build a variable using several fields on the model that contains the text that a user clicks on and another field from the model which contains the link.
Here is the code in my controller:
MediaLibrary.find(:all, conditions: ["media_type_id < ?", 3], limit: 5).each do |media_item|
#audio_links["link_text"] = "Audio of #{MediaCreator.find(media_item.media_creator_id).name} #{media_item.media_created.to_time.strftime('%A, %B %d, %Y')} at #{media_item.meeting_time}
#{media_item.am_pm} - #{media_item.name}"
#audio_links["link"] = media_item.link
end
Here is the code in my view:
<% #audio_links.each do |audio_link| %>
<li>
<%= link_to audio_link["link_text"], '#{audio_link["link"]}' %>
</li>
<% end %>
I have also tried the following:
<% #audio_links.each do |audio_link| %>
<li>
<%= link_to 'audio_link["link_text"]', '#{audio_link["link"]}' %>
</li>
<% end %>
And this:
<% #audio_links.each do |audio_link| %>
<li>
<%= link_to '#{audio_link["link_text"]}', '#{audio_link["link"]}' %>
</li>
<% end %>
I have tried a few more variations but I either get the can't convert String into Integer error on the link_to command when I attempt to display the screen or the links display with the text being displayed as the following. When this happens I get other errors when I click the link.
#{audio_link["link_text"]}
I have done a lot of searches on Stack Overflow and throughout the web. I have not found a single example of this being done anywhere. I have seen in older posts where there was a set_path command (2010) but nothing for recent posts. I have used html_safe! before and will add that to my code. I do not know if there is a problem with my code or if I am attempting something that is not possible. I sincerely hope this is possible because it will make it easier for people with speech readers to know what they are clicking on.
Any help with this would be appreciated.
You can't do string interpolation in single quotes. Replace the single quotes with double quotes and your variables will be expanded properly.
For example:
<%= link_to audio_link["link_text"], "#{audio_link['link']}" %>

Creating Links on Rails

I've just started using rails yesterday, so this is a kinda noob question
for example, a user is at www.example.com/name
and I want to make several links to www.example.com/name/:id
So I tried something like this:
<% #items.each do |item| %>
<%= link_to item.name, '/name' :id %>
<% end %>
I know, it was a complete guess on how I should write the code, but the restful code sends to a completely wrong link. How should I write this three lines?
Use the route helper:
<% #items.each do |item| %>
<%= link_to item.name, item_path(item) %>
<% end %>
ps: when you have a simple question like this one, take a look at this guide, you'll often find the answer.
Try
<%= link_to item.name, item_path(item) %>
item_path is a URL helper method which spits out the link to show a name.
URL helpers have the general form:
{action}_{class}_path({object or object_id})
If {action}_ is omitted, then the default action is assumed (normally show).

Resources