View helpers for incorporating Elm modules into Rails views
elm-rails
makes it easy to use Elm modules in your Ruby on Rails applications. This project was heavily inspired by react-rails.
Only works with Elm 0.19.
This gem is tested against Ruby versions 2.7, 3.0, and 3.1; and Rails versions 6.1, and 7.0. It may work on other versions, and if you want to open a PR adding tests against those versions they would be welcome.
Add elm-rails to your Gemfile
and run bundle install
gem "elm-rails"
Create a new directory to house all Elm modules
mkdir app/assets/elm
Update .gitignore
to ignore elm files
/elm-stuff
Define your elm modules in the app/assets/elm
directory.
app/assets/elm/Hello.elm
module Hello exposing (..)
import Html exposing (text)
port noun : String
main =
text ("Hello " ++ noun)
Open your app/assets/javascripts/application.js
and require your Hello.elm
.
//= require Hello
Use the view helper to insert your component into your view. Pass port values as a Hash
.
<h1>This is an Elm component!</h1>
<%= elm_embed('Elm.Hello', { noun: 'World!' }) %>
That’s it!
By default, elm-rails uses elm-compiler under the hood, which will use the version of elm
available in your system path, and then installing its own copy in /tmp if needed. If you wish, you may configure this path in an initializer:
config/initializers/elm_compiler.rb
erb Elm::Compiler.elm_path = "bin/elm"