How do I link to an external stylesheet in Ruby on Rails? - ruby-on-rails

I'm trying to link to the YUI reset stylesheet in my RoR layout using the following statement...
<%= stylesheet_path("http://yui.yahooapis.com/2.8.1/build/reset/reset-min.css") %>
but this path is being echoed on my page instead of being applied. I got this syntax from the the rails docs. What am I doing wrong?
Thanks!

Try this
<%= stylesheet_link_tag 'application','http://yui.yahooapis.com/2.8.1/build/reset/reset-min.css' %>

Try stylesheet_link_tag as described in the Rails API Docs

Related

link_to works local, but doesn't work on Heroku

I have a portfolio application built with Ruby 2.3.0 and Rails 5.0.0 that has links to external urls that work locally but do not work when deployed to Heroku.
<%= link_to image_tag(project.index_image), project.url %>
There are two different types of links that are not working and producing different symptoms.
External Application Links
In production the links to my other applications successfully redirect to the desired urls but the url's do not update in the browser to the new page.
Github links
The links to github do not work and redirect to a blank screen. That screen displays the following error in the developer console:
Refused to display 'https://github.com/my_repo' in a frame because an ancestor violates the following Content Security Policy directive: "frame-ancestors 'none'".
Try to creating a helper class for custom URL like below
On the helpers/application_helper.rb
def custom_link(image, url)
link_to image_tag(image), "#{url}"
end
And the view look like this
<%= custom_link project.index_image, project.url %>
This should work I hope because it's working my own.
Hope it helps
You can try the following:
<%= link_to project.url do %>
image_tag(project.index_image)
<% end %>

How to safely store Google Api keys with rails and use them in scripts?

I'm trying to safely store the Google api key that I'm using in a rails project. I tried storing it in a my secrets.yml file but if I then say in my google script &key=ENV['google_api'], it can't fetch it.
I'm pretty sure this is not the way but after googling the topic, I can't really find a clear answer on this. Anyone who can offer some help?
Thanks,
Thomas
UPDATE, here's the code I'm using to fetch the key in my layouts file:
Issue, I'm still receiving the error that my api_key is "nil".
<script>
var GOOGLE_API_KEY="#{ENV['GOOGLE_API_KEY']}"
</script>
<%= javascript_include_tag "//maps.google.com/maps/api/js?v=3.23&key=#{ENV['GOOGLE_API_KEY']}&libraries=geometry,places&language=&hl=&region=&callback=initMap"%>
You will want to put the script call in your application.html.erb file so ruby can parse the ENV['google_api']. Here is an example application file (in HAML):
!!!
%html
%head
%title Pursuit
= stylesheet_link_tag 'application', media: 'all'
= csrf_meta_tags
:javascript
var GOOGLE_API_KEY="#{ENV['GOOGLE_API_KEY']}"
= javascript_include_tag 'application'
= javascript_include_tag "//maps.googleapis.com/maps/api/js?libraries=places&key=#{ENV['GOOGLE_API_KEY']}"
%body
= render 'layouts/menu'
.row
.col.m12.l10.offset-l1
= yield
I was using the figaro gem. For me worked adding the environmental variable to application.yml file.
GOOGLE_API_KEY: there_put_the_key
Then on top of the
app/views/layouts/application.html.erb
(If you want to add source to all of views else you can add this line to specific view) simply add this line
<%= javascript_include_tag "//maps.google.com/maps/api/js?key=#{ENV['GOOGLE_API_KEY']}" %>

link_to in liquid template

I am new in Ruby on rails and facing issue while creating link_to using Liquid gem. I want to create a link in the email template like:
{{link_to 'Submit', profile_accounts_url(:account_id => account.id)}}
However, link is not created in email template. Please help me. Thanks in advance.
I didn't use liquied before, but you can check the syntax for link_to here.
using Liquid variables inside of a liquid tag call
http://richonrails.com/articles/liquid-templating-engine
However, I think you're using rails' syntax for link_to instead of liquid's syntax. Maybe you should just use something like:
<%= link_to "Submit", profile_accounts_path(account) %>
which will redirect to the show page.

Rails image broken link

I'm trying to link to an image in rails and its not working. I can link to assets/file.css as well as assets/file.js but assets/file.jpg doesnt work.
I tried a fresh rails new project_name and when i open the homepage and the rails logo doesnt show.
As a side question,
How do I get rails to load only the application.js and application.css
I see that if i remove ?body=1 from the URL it compresses them all into one file, but when I view the page using the following code it leaves those files blank and includes them one by one as separate scripts.
<%= stylesheet_link_tag "application", :media => "all" %>
<%= javascript_include_tag "application" %>
Ok so that's an expected behavior, you have to use Rails helpers:
<%= image_tag "file.jpg" %>
Your css files should be .erb. Within it, use:
.class { background-image: url(<%= asset_path 'image.png' %>) }
Reference is para 2.2.1 here.
I just checked my code and my CSS looks like this:
background-image:url(/assets/pinstripes.jpg);
with the image uploaded to app/assets/images, of course. This looks like what you're using, but I can't tell for sure.

Using reCaptcha with Rails 2.3.12

So I'm trying to get reCaptcha to render on a partial form view that uses HAML. I have tried using the :ruby filter and then adding <%= recaptcha_tags %> but that didn't work, neither has anything else that I've found. Is there a way to implement this?
*Revision
Ahem, more specifically, can anyone tell me what I need to have for the <%= recaptcha_tags %> helper? Every thing I find on this subject just says "Add <%= recaptcha_tags %> wherever you want it to appear!" and absolutely nothing on what the helper should contain.
*Another Revision
I am indeed trying to use Ambethia. I tried using just = recaptcha_tags but that didn't work, I got an error saying it was an undefined variable or method. I installed the Ambethia/reCaptcha as a plugin using script/plugin install git://github.com/ambethia/recaptcha.git and I put config.gem "ambethia-recaptcha", :lib => "recaptcha/rails", :source => "http://gems.github.com" in environment.rb along with my public/private keys.
*Started Over
Okay, got rid of everything I had done initially. Can anyone help me with this? I follow all of the tutorials I can find on it, but none of them explain how to implement/create the helpers for <%= recaptcha_tags %> or <%= verify_recaptcha %>. I'm obviously new to RoR and implementing reCaptcha of any kind, so I'm sorry I'm asking for my hand to be held but I am honestly lost and am not finding any guidance anywhere! Thanks so much anyone and everyone.
did you try simply:
= recaptcha_tags
You don't mention the plugin you're using. I'm assuming this one. If that's the case, the recaptcha_tags helper will return the HTML for the captcha, and you'd insert it into whichever forms you wanted the captcha to appear on.
The <%= %> around recaptcha_helper aren't part of the helper, but rather the way you insert content into erb templates (and other templating languages resembling erb). In Haml you don't need the surrounding tag. It's just =.
I had the same problem and I finally solved it realizing that my form was called asynchronously.
I added:
= recaptcha_tags :ajax => true
and captcha appeared.
Hope this could help the original question.

Resources