Search code examples
htmlruby-on-railshref

How to href my ruby code in an html


I currently have the code below in my html which gets sent as an email. I would like for it to say here as a hyperlink. I have seen and know how to do link_to but I do not think that would work in html format or if it does I am not sure how to do it properly.

This code directs the user to a link in my web app with a parameter.

 <%=landlord_page_url(@user)%>

Any help or insight would be appreciated.


Solution

  • Ok so assuming you have an ActionMailer called MyMailer, with a single method send_link. Just as you would for a controller action, set any instance variables within the send_link method to be used by the email view for rendering.

    app/mailers/my_mailer.rb

    class MyMailer < ActionMailer::Base
      default from: 'myemail@mydomain.com'
    
      def send_link(user, host)
        @user = user
        @host = host
    
        mail(to: user.email, subject: "Here is your link")
      end
    end
    

    Next create a view for this just as you would for a controller actions views and write your html with any necessary erb.

    app/views/my_mailer/send_link.erb

    <p>Dear <%= @user.forename %>,</p>
    
    <p>Here is your link</p>
    
    <p><%= link_to 'Click Me', landlord_page_url(@user, host: @host) %></p>
    

    And you are done.