Adding ruby global variable to a html hyperlink - ruby-on-rails

I have the following code in my html.erb file in my ROR application.
<% $tmp_bug="something" %>
JIRA
I want to add that ruby variable to the end of the hyperlink. This does not seem to work.

<% $tmp_bug="something" %>
JIRA

Related

Why does commented-out ERB still throw errors?

I have an HTML file in this format:
html
more html
<%= valid erb %>
<!--
<%= incomplete_erb_with_bugs %>
-->
But I still get an exception page, because of the buggy ERB. But shouldn't commenting that section out cause that part not to be read by the browser? Is there another HTML method for actually preventing the browser from reading code?
Because its not commented.
But shouldn't commenting that section out cause that part not to be
read by the browser?
The browser does not execute Ruby or ERB - the server does before it sends the resulting HTML document to the browser.
ERB is ruby code imbedded in a file that contains literal text. The interpreter does not care about anything except the code in "erb tags".
This is just literal text
<%# this is a ruby line comment - the code below is executed: %>
<% bar do %>
<%= foo %>
<% end %>
The rest is just placed in the buffer. This is just like PHP or any other embedded language.
So a HTML comment (or CSS or JS for that matter) does not effect the ERB interpreter in any way. The interpreter does not really know or care that its creating HTML.
Is there another HTML method for actually preventing the browser from reading code?
The browser does not execute Ruby code. It just builds a document from whatever you send in the response.
So use a ruby comment <%#= incomplete_erb_with_bugs %> which will prevent the code from being executed - and it will never get sent to the browser.
you're commenting out html, but the ruby code inside it still gets evaluated. you need to comment out the code, which is done like this:
<%#= incomplete_erb_with_bugs %>
you need to comment the actual line, not the html surrounding it
html
more html
<%= valid erb %>
<%= incomplete_erb_with_bugs %>
you can also use if statements
<% if false %>
<%#= incomplete_erb_with_bugs %>
<% end %>

Sublime Text generate wrong "form_for" tag

I'm new with RoR, anytime when I used code snipet on Sublime Text to generate a "form_for" tag, the result is:
<% form_for #model do |f| -%>
<% end -%>
But as I know, it should be
<%= form_for (#message) do |f| %>
<% end %>
hope to receive help from you, thanks!
In Ruby, you can call a method and pass in the arguments with or without the (parentheses). It is generally considered better Ruby coding practice to leave them out, which is why the Sublime Text snippet is leaving them off. Also, the snippet expects you to replace #model with whatever model instance you are working with in the particular form.

How to create a link to a non-image file (like a word or excel file) using Dragonfly and Rails?

I've got a rails app with the ability to upload multiple types of files and now i'm trying to create a page to show the data to the user.
I have this snippet in my view:
<% #case.documents.each do |doc| %>
<% if doc.is_image? %>
<%= image_tag doc.document.url %>
<% else %>
<%= link_to doc.filename, doc.document.url, target: '_blank' %>
<% end %>
<% end %>
which generates a link like this for a file that is not an image.
<a target="_blank" href="/media/W1siZiIsIjIwMTQvMDcvMjgvOHM3d21ubDBtc19maWxlIl1d?sha=0813902f">myFile.xlsx</a>
The problem is that since the href doesn't link directly to a filename with an explicit extension, the browser is not sure how to handle the file (at least I think that's why it doesn't know how to handle the file).
How can I make a link to these files which have been uploaded so the browser knows how to handle files like excel, word, pdf, etc.
the easiest way is to add a document_name column to your Document model - then the urls will have the file extension.
(although having said that, the Content-Type should be correctly set anyway so not sure why the browser is having trouble)

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']}" %>

Netbeans Source Format: Can It Recognize Other Stuff? (For RoR Views)

I am in love with Netbeans Source->Format feature. Is there any way to get it to recognize stuff in these blocks
<% content_for :style do %>
<% end %>
as CSS? It would also help for autocomplete.
Ditto, of course, for
<% content_for :javascript do %>
<% end %>
blocks.
I'm not certain, but I think the source formatting is on a file basis only, not on parts of a file.
Edit: Actually, I'm wrong, the formatting can recognize various languages in the same file...obviously what's happening in an ERB file for ruby and html code. But I don't see a way of extending that.

Resources