Adding hyperlink in mail - ruby-on-rails

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

Related

Using `_path` in Rails Mailer

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.

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))

Need help for linking a PDF on page created with rails

I ask myself the question does an pdf tag exist in rails 4 which can show
an pdf file very simple?
This solution answers me that i need to give the correct route informations:
<%= link_to 'Help', public_path%>
I already have an pdf file in my project directory app/assets/public/help.pdf
and now i wanna have a link with the simple name "Help" to have on my page.
Third edit:
After a nice first answer nothing changes and i get the same error message from rails. What i have to write in the routes.rb if i wanna use a link for my pdf file. Does anyone know how can i link an PDF from my own directory!?
In my experience with Rails, I've never seen a PDF link tag helper.
What you want, is to use asset_path in combination with link_to
<%= link_to 'Help', asset_path('data/help.pdf') %>
#=> Help
If it needs to be absolute, you could always use asset_url
<%= link_to 'Help', asset_url('data/help.pdf') %>
#=> Help
What I usually do for linking a document that can be public is I put the document in the public folder in rails and then use this for the path:
link_to 'Help', root_path << 'TheTitleOfYourPDF.pdf'

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' %>

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