I want the sites navigation menu to be populated by a hash in the application helper. I've got it looking right; but the path helpers are not being evaluated (meaning my link tag's href = "root_path" instead of "/").
In my application_helper:
$navPages = {
'Home' => "root_path",
'page2' => "page2_path"
}
and in some layout partial the erb is as follows:
<ul>
<% $navPages.each do |ntext,npath| %>
<li><%= link_to ntext, npath %></li>
<% end %>
</ul>
So this setup will get the list too look right; but the href's aren't being evaluated. The first list item link tag href = "root_path" instead of the evaluated version of the string "/".... I've tried a few things to no avail. This is some fundamental lack of understanding the hash value storage and how/when the ruby path helper is being evaluated. I've tried:
1. removing the quotes from the hash (returned an unknown variable/method error)
2. "#{npath}
3. "raw"
a few other things.
Any direction will be greatly appreciated.
Thanks!
<% $navPages.each do |ntext,npath| %>
<li><%= link_to ntext, self.send(npath.to_sym)%></li>
<% end %>
Related
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 "/..."
I am trying to set variables in a series of link_to links, so that each link is associated with a value.
On one page I have a loop which creates a list of links from values in my database:
<ul>
<% #alldata.each do |x| %>
<li><%= link_to "#{x.name}", charts_path %></li>
<% end %>
</ul>
The charts_path links to a view which displays a chartkick graph:
<%= line_chart #data.group("year").sum("magnitude") %>
which gets its data from a controller saying:
def graphdata(x)
#data = Model.all.where(name: x).select("magnitude, year")
end
def charts(?variable)
#data = graphdata(?variable)
end
I would like to alter my list loop to set ?variable to x.name. For example something like:
<li><%= link_to "#{x.name}", charts_path, ?variable = "#{x.name}" %></li>
So that I have a list of links, that route to different charts with data specific to that link.
I could define a specific view for every single name:
def chart1
#data = graphdata("name1")
end
def chart2
#data = graphdata("name2")
end
But that seems like very bad programming...
I'm sorry if this question is poor, I'm extremely new to programming.
Any assistance would be greatly appreciated!
Thank you
Try Using this Code.
<%= link_to "#{x.name}", charts_path(x.name) %>
If I understand correctly, you want to pass the name of x as a querystring variable. You can easily do this with the charts_path helper:
<%= link_to "#{x.name}", charts_path(variable: x.name) %>
charts_path accepts a hash that will be passed with the request
I encountered the similar state where I got my module worked with the following method
`<%= link_to n.subject, notifications_message_path(notification_id: n.id)%>`
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']}" %>
I have a simple page that displays some 'Games'.
Heres the code:
<ul>
<%= #tvshow.games.each do |game| %>
<li><%= game.gameTitle %></li>
<% end %>
</ul>
It displays like this:
The All-Syrup Squishee
#<Game:0xb6783820>
With the #Game tag coming AFTER the list item but before the closing list tag. Any idea why it's showing up or how I could get rid of it?
<%= outputs the result as markup, in this case an instance of the Game class.
For this kind of loop you want to use <% which executes some code but does not produce markup.
Edit line 2 to read <% #tvshow.games.each do |game| %>
Remove your first = from the line with the each
Rails is printing the result of the each statement, which returns the array itself. When you output an array, you will output the .to_s of each of its contents, which in your case is the default representations of the Game objects
Here is my code, I'm trying to display a list of links to a bboy's crews in sentence form with .to_sentence
<span class="affiliation">
<% if(#bboy.crews.count > 0)%>
<span><%= if(#bboy.crews.size > 1) then "Crew".pluralize else "Crew" end %>:</span>
<%= #bboy.crews.collect{|c| link_to c.name, c}.to_sentence %>
<% else %>
<em>Independent</em>
<% end %>
</span>
The output I get is the correct links but it displays as:
Hustle Kidz and Knuckleheads Cali
rather than:
Hustle Kidz and
Knuckleheads
Cali
with the html escaped, rather than the desired links.
Am I missing something? I've tried CGI.unescapeHTML and a few others but am getting lost...
Rails 3 now automatically escapes everything, in order to output raw HTML use this:
<%= some_string.html_safe %>
or this:
<%= raw #some_html_string %>
Thanks to macek for a hint.
For additional details: http://markconnell.co.uk/posts/2010/02/rails-3-html-escaping
You can (and should) you the raw method
<%= raw #some_html_string %>
I agree with Kleber S, you should move this into a helper, because that's a lot of logic for a view
def crews_description(crews)
if crews.empty?
content_tag('em', 'Independent')
else
label = "Crew"
label = label.pluralize if crews.size > 1
crews_links = crews.map {|crew| link_to(h(crew.name), crew)}.to_sentence
content_tag('span', label) + crews_links.html_safe
end
end
and in your view:
<span class="affiliation">
<%= crews_description(#bboy.crews)
</span>
I recommend you move this block of code to an helper and then use the .html_safe method to obtain the expected results.