Using `_path` in Rails Mailer - ruby-on-rails

I'm currently working with a mailer in which I need to link an index view. In my app, this is reachable by reminders_path, but when I used that in my mailer (<%= link_to "Memory Enhancer", reminders_path %>) the link didn't work.
After some googling and some SO searches, most things seemed to recommend something like this:
<%= link_to "Take Me To My Memory Enhancer", Rails.application.routes.url_helpers.url_for(controller: "reminders", action: "index") %>
However, the link still isn't functional. What is the "proper" rails way to do this?

Use reminders_url, as you want a full url, not just the path.

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 create a href tag with link_to and action 'create'

I want to create a href tag like href=contacts/create. In my contacts_controller, I have a create GET action. I know this is against rails convention. I still need to create the above link using options = {controller=> 'contacts', action=>'create'}. It works for any other arbitrary action name
You can the hardcoded path option:
<%= link_to "Create", "contacts/create" %>
or the Rails generated path option:
<%= link_to "Create", { controller: "contacts", action: "create" } %>
This is not just against Rails' convention, but against sounds HTTP usage. This often causes serious problems that you can't predict in advance. Web crawling is just one of them, where something like the Google bot accidentally creates a new contact in your database, simply by crawling the page. Or script kiddies who find you have a create link, and send 100,000 clicks to it in quick succession.
Numerous other issues happen like this, including, at one well-known time, Google Chrome pre-fetching GET urls from the page to "speed up the user experience"; this was felt far and wide by sites that had used this technique. It's not an idle warning or a style issue: this can have a disastrous impact on your site.
First off this is really bad idea since GET requests should be idempotent. You're not just flouting convention - you're setting yourself and your users up for a really bad time since for example pressing the back and forward buttons will cause resources to be created - over and over. And there is guaranteed a better way to solve whatever you are trying to do such as:
# a "discrete form"
<%= button_to "Create contact", contacts_path, method: :post %>
# or use the rails ujs
<%= link_to "Create contact", contacts_path, method: :post %>
If you ABSOLUTELY have do this:
Rails.application.routes.draw do
get "contacts/create"
end
You can now do:
<%= link_to "Create", { controller: 'contacts', action: 'create' } %>
Congratulations, you broke the internets.
Like you mentioned, this is against rails convention, but if absolutely necessary, you can do this from your controller:
options = {controller=> 'contacts', action=>'create'}
view_context.link_to url_for(options)
If you need the href to only be the path, you can do:
options = {controller=> 'contacts', action=>'create'}
view_context.link_to url_for(options.merge(only_path: true))

How to get root path of rails app

I am novice to rails. I have been using LAMP for a long time. I am looking for something like {{URL::to('/')}} from Laravel.
I DO NOT want to use link_to which creates entire anchor tag as it defeats the purpose of isolating views. I want to get a value to put in href tag.
This is a link
Rails has has the root_path and root_url helpers
You also seem quite confused about link_to it does not compromise the "isolation" of your views. Its a helper method which makes it possible to create very dynamic html without doing a lot of interpolation.
<%= link_to t('home'), root_path, class: 'btn btn-success' %>

Adding hyperlink in mail

My requirement is to add a link to mail sent from application developed in Ruby On Rails. Clicking that link in mail need to route user to that particular record in the application.
Can someone please help me with this some sample code.
In your view template use _url. E.g.:
<%= link_to 'Edit User', edit_user_url(#user) %>
it will return the complete URL.
You can use a simple link_to method in the mail or you could use a simple anchor tag. The latter you would have to build up.
If you need information on how to build links and paths check the rails guides. This has an example on the first section of the guide.
e.g. <%= link_to 'Patient Record', patient_path(#patient) %>
Besides this you need to provide more information if you want more help.
If you are starting with Ruby On Rails I would recommend looking at the videos on rails cast.
For your email problem you can look at sending-html-email
Also in the email I like to use the link_to helper like this
<%= link_to 'Click Me', something_url(#user.id), %>
I hope that this works. And Happy Coding

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.

Resources