I'm building a small static website using Ruby and Middleman. With Rails I've been able to safely encode email links with the mail_to helper and the encode: 'hex' option;
mail_to 'email#email.com', 'My Name', encode: 'hex'
But when I try this in Middleman using the same code I wind up with this in my HTML;
<a encode="hex" href="mailto:email#email.com">My Name</a>
Any suggestions? I tried adding actionpack to my gemfile, but that didn't help.
Although both helpers from Middleman and Rails are called the same, they are actually not the same in code. Moreover, encode parameter has been removed from Rails 4 and you now have to require a separate gem to use it.
I think, your best option will be to look at the code in that gem and reimplement it as a separate helper for your Middleman project.
Related
I built an api only rails app, but since i'm using a 3rd party email service, I need to be able to render the email template into a string to pass it to the mailing service wrapper gem. So far any attempt to render templates into a string returns an empty string, and i suspect it is because the application is configured to be API only. Ho do I add support to rendering templates into a string?
If this is not the case, please let me know. I'm using postmark mail service. postmark-rails gem, which integrates into standard rails mailers didn't work at all, and plain postmark gem (which uses postmark API instead of postmark SMTP server) works fine, but now my problem is producing the proper html for the email.
this is what I'm trying:
html = render_to_string(
partial: "transfer_mailer/transfer.html.erb",
locals: {
:#body => #body,
:#campaign => #campaign,
:#chat => #chat
}
)
but it returns empty string.
My setup involves Rails 5.0.1, ruby 2.3.1p112 (2016-04-26 revision 54768) [x86_64-linux], Sidekiq 4.2.7, and in production im using nginx/1.10.2 and Phusion Passenger 5.1.1. Production environment is deployed in an azure virtual machine, and I added both inbound and outbound rules for allowing traffic through ports 25, 465, 2525, and 587.
Any help would be really appreciated.
Looking at the docs for render_to_string, it looks like the path doesn't need to include .html.erb, so maybe removing that will fix it.
You can see here for some example code:
In your case, it would be:
render_to_string(
partial: '/transfer_mailer/transfer',
locals: {
:#body => #body,
:#campaign => #campaign,
:#chat => #chat
},
layout: false
)
Also make sure the partial filename starts with an underscore, even though you don't include it with render_to_string or render calls.
There is the other way to use render_to_string for resolving your issue. Please see the code below.
#body = XXX
#campaign = YYY
#chat = ZZZ
html = render_to_string(
file: 'transfer_mailer/transfer.html.erb'
)
Hope it helps.
In the blog module for refinery CMS there are some strings in the source code that I would like to translate. When I override the view for _post.html.erb of the blog module there is code like this inside:
<%= content_tag(:span,
"#{t('by', :scope => 'refinery.blog.posts.show')} #{#post.author.username}",
:class => "blog_author") if #post.author.present? %>
I would like to localize the "by" string, so that in the blog, the default english "By authorname" is replaced by a phrase in another language.
Now, I have modified the en.yml and hr.yml localisation files in the rails config/locales directory and added the translation. However, this makes no effect to the strings displayed on my page.
I have tried setting the config.i18n.default_locale variable in config/application.rb to :en and to my desired language but this also accomplishes nothing.
The furthest I came was that if I change the config.current_locale variable in initializers/refinery/i18n.rb to :de for example, that has the effect of translating the admin interface for refinery and its blog module. And yet, the strings in the blog entries remain the same.
I have also added a yml file with translations for my locale in the gems library of the refinery blog component, but it still does not work.
Any help on how to translate the strings in the refinery blog module would be appreciated. I have searched the internet on how to translate refinery, but haven't managed to find any specific information for the translation of the the blog component, only general guides for rails, which don't seem to help with the refinery blog.
I am using the following gems versions:
rails (4.2.4)
rails-i18n (4.0.7)
railties (4.2.4)
refinerycms (3.0.0 1d13007)
refinerycms-acts-as-indexed (2.0.1)
refinerycms-authentication-devise (1.0.4)
refinerycms-blog (3.0.0 5ee8336)
refinerycms-core (3.0.0 1d13007)
refinerycms-i18n (3.0.1 ff93ad9)
refinerycms-images (3.0.0 1d13007)
refinerycms-pages (3.0.0 1d13007)
refinerycms-resources (3.0.0 1d13007)
refinerycms-search (3.0.0 aa8098c)
refinerycms-settings (3.0.0)
refinerycms-wymeditor (1.0.6)
...
Thank you in advance!
P.S
I have addded a translation string to hr.yml and set the
config.i18n.default_locale = :hr
config.i18n.locale = :hr
in application.rb.
In the rails console I get:
*I18n.locale* => :hr,
I18n.translate('hello') => "Pozdrav svima"
But, when I start the application, in the rails server messages there is:
*Parameters: {"locale"=>:en}*
and no translations work... Why?
According to the rails locale guide
the application locale can be set based on the url. For localhost, the locale was set to english. In order to change this, one can modify the application_controller.rb
class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception
before_action :set_locale
def set_locale
I18n.locale = I18n.default_locale
end
end
I'm building a simple app in ruby using the Sinatra framework. It's mainly "get" based - most requests will be for listing data. However there are a couple of key screens in the app that will collect user input. I want to ensure the app is as safe as I can make it, and currently, trying to find how to implement the kind of authenticity tokens that you get in a Rails form?
Where I've got to:
Well, I know I need the tokens for csrf, but I'm unsure if I need to generate them myself or if Sinatra can do it for me - I've looked through the docs and they say that Sinatra is using Rack Protection, however, I can't find any example code for it and can't seem to figure out how to get it going - any help apprectiated - thanks!
Use the rack_csrf gem. Install it with
gem install rack_csrf
The rack_csrf gem has a Sinatra example. Below is a simpler example adapted from this page (seems offline. Archived version):
require "rack/csrf"
configure do
use Rack::Session::Cookie, :secret => "some unique secret string here"
use Rack::Csrf, :raise => true
end
Using enable :sessions instead of use Rack::Session::Cookie ... will also work in most cases (see Bill's comment).
In your view, you can get the token (or the tag) with the Rack::Csrf.csrf_token and Rack::Csrf.csrf_tag methods. If this appears lengthy, you may want to define a helper along the lines of:
helpers do
def csrf_token
Rack::Csrf.csrf_token(env)
end
def csrf_tag
Rack::Csrf.csrf_tag(env)
end
end
Small example using the helper method:
<form method="post" action="/tweet">
<%= csrf_tag %>
<input type="text" name="message"/>
<input type="submit" value="Submit a tweet!"/>
</form>
When same erb view is rendered, in a case where credentials are invalid while logging in. now after the render on submit it throws the error.
Rack::Csrf::InvalidCsrfToken at /login
Rack::Csrf::InvalidCsrfToken
Do we need to do a redirect in place of render to make this work? because in render the old csrf token is still in place. in a complete redirect we have a new token generated.
I have installed the ckeditor 3.7.1 and Paperclip gems (gem 'ckeditor', '3.7.1' gem 'paperclip') in Rails 3.2.3 by following the instructions posted on github (https://github.com/galetahub/ckeditor). It all seems to be working except that when I try to send an email with AccountMailer using the html (which contains uploaded images) generated from the ckeditor it gives me this:
<img alt=3D"logo" src=3D"/assets/logo= .png" />
when I want this:
<img alt=3D"logo" src=3D"www.mydomain.com/assets/logo= .png" />
I've found the documentation very confusing and the gem does not provide access to config.js in order to modify baseHref. I've tried changing the attachment_file.rb and picture.rb in model/ckeditor so that the url includes the domain but this breaks the uploading capability. I've also tried ckeditor_rails but this removes uploading capability and I do not have time
to create custom browsers and uploaders.
Any help is appreciated. Thanks!
Add the function in your Ckeditor::Picture Model
def url_content
if Rails.env.production?
host_url="http://your_domen"
else
host_url="http://localhost:3000"
end
host_url+url(:content)
end
Does anyone had installed Devise gem with Sinatra?
Devise is based on Warden and so it should work on Sinatra, I couldn't find any related info about how to implement it.
Devise is really just a Rails-centric wrapper with nice helpers for warden, which is the underlying Rack authentication framework. So if you're using Sinatra in conjunction with Rails, you can use Devise in your Rails app, and use warden directly in your Sinatra app, and they will see the same user session data.
So no, you can't use Devise directly within your Sinatra app, but if you're building a modular app with some pieces in Rails, and other pieces in Sinatra, you can use Devise/Warden among the components.
Devise is designed for Rails only. You can't use it with Sinatra.
You can check out:
https://github.com/maxjustus/sinatra-authentication
http://www.gittr.com/index.php/archive/sinatra-basic-authentication-selectively-applied/
https://gist.github.com/243611
There is also https://github.com/jsmestad/sinatra_warden available.
I was able to get it working. There were a few main aspects:
Get Devise working with Rails (Devise is a Rails app, won't work without it)
Setup the mapping (route) on Rack level to support both Rails and Sinatra
Share the sessions between Rails and Sinatra
Setup Warden and make it available to Sinatra
Here is most relevant part of code from /config.ru:
#
# ...
# Rest with Rails
map "/" do
run MyApp::Application
end
# Anything urls starting with /slim will go to Sinatra
map "/slim" do
# make sure :key and :secret be in-sync with initializers/secret_store.rb initializers/secret_token.rb
use Rack::Session::Cookie, :key => '<< see, initializers/secret_store.rb >>', :secret => '<< copy from initializers/secret_token.rb >>'
# Point Warden to the Sinatra App
use Warden::Manager do |manager|
manager.failure_app = AppMain
manager.default_scope = Devise.default_scope
end
# Borrowed from https://gist.github.com/217362
Warden::Manager.before_failure do |env, opts|
env['REQUEST_METHOD'] = "POST"
end
run AppMain
end
See, http://labnote.beedesk.com/sinatra-warden-rails-devise for a complete solution.