TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Live previews with Rails and Stimulus 2

119 pointsby nilsandreyabout 4 years ago

10 comments

strzibnyabout 4 years ago
Based on the feedback, I updated the post with one more approach based on the idea from <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=26554476#26556540" rel="nofollow">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=26554476#26556540</a>. The second approach avoids Rails UJS in favour of Turbo Frame.<p>Of course there are even more ways how to do this kind of thing. Keep sharing!
rolaeabout 4 years ago
Nice. I did something similar recently with an iFrame where you have a form for configuring customer sites and it displays the preview in an iFrame.<p>I had to throw in some debouncing and special handling for hidden inputs, as changes to hidden inputs do not trigger a change event on the form.<p>In the end the markup was very simple and is very reusable (StimulusJS v1):<p><pre><code> &lt;div data-controller=&quot;iframe-preview&quot; data-iframe-preview-url=&quot;&lt;%= preview_path(@letter) %&gt;&quot;&gt; &lt;form data-target=&quot;iframe-preview.form&quot;&gt; &lt;textarea name=&quot;body&quot;&gt;My letter&lt;&#x2F;textarea&gt; &lt;&#x2F;form&gt; &lt;iframe data-target=&quot;iframe-preview.iframe&quot;&gt; &lt;&#x2F;div&gt; </code></pre> This is when StimulusJS becomes really nice, when you can compose behavior in your markup with some simple data attributes. I did not think at first that I would need this controller in other places, but a couple of weeks later, I actually needed it, and was able to reuse the controller without modification for another use case.
评论 #26558472 未加载
yuppiepuppieabout 4 years ago
As someone who is only vaguely familiar with Rails and doesnt really know anything about stimulus, it would be nice to have a &quot;What this looks like&quot; section with a gif of what the user sees. Otherwise, I dont know what this article is talking about. Im interested as there is probably some application to my daily work, but a wall of code and words doesnt tell me much very quickly.
评论 #26556121 未加载
评论 #26555605 未加载
Igor_Wiwiabout 4 years ago
good to see Rails dev related posts on HN again:)
023984398about 4 years ago
This is cool but its not using hotwire (turbo frames &#x2F; turbo streams)
评论 #26554966 未加载
stevebmarkabout 4 years ago
I&#x27;m happy I don&#x27;t have to deal with these paradigms day to day anymore:<p>- Making everything go through a &quot;controller&quot; instead of well designed clients and APIs. Mixing rendering, redirection, ajax handling as well as page serving as well as business logic all into a &quot;controller&quot; is painful. (&quot;Models&quot; and the spaghetti design patterns they come with are even worse)<p>- PHP style templates that trigger database queries inline, making them difficult and eventually impossible to optimize<p>- Scattering view code across random places instead of writing view code with its corresponding view logic in the same file&#x2F;component<p>- Wiring up functionality to templates by targeting selectors<p>- Manually setting innerHTML for dynamic functionality<p>- Dealing with rails &quot;turbo&quot; anything, which is not useful in a real world application, and dangerous (we&#x27;ve had production outages because of trying to use turbolinks)<p>- Not being able to customize behavior because you&#x27;re locked into Rails design limitations
评论 #26560327 未加载
评论 #26559961 未加载
评论 #26560635 未加载
shay_kerabout 4 years ago
Does anyone use Live Preview &#x2F; Live Views in Phoenix, but in production with a B2C product?<p>I&#x27;m a bit skeptical of Rails performance with the method the author details here, given that Ruby is much, much slower than Elixir.<p>I&#x27;m curious if even Elixir folks deploy Live Views in consumer-facing apps. But I can totally see it being used for admin interfaces or internal applications, for sure.
评论 #26558619 未加载
评论 #26558384 未加载
评论 #26558415 未加载
lucidoneabout 4 years ago
Would be great to see a paragraph or two about how to write tests for this functionality. Testing has been my pain point regarding front end development with Rails. Compelling tech all the same and a great article.
评论 #26557993 未加载
评论 #26560726 未加载
评论 #26557260 未加载
jack_rimintonabout 4 years ago
Would be great if you could add a small video&#x2F;gif of this
评论 #26555790 未加载
kaleidawaveabout 4 years ago
The post is well written but I don&#x27;t see the point in this when you can achieve the same in 145 bytes of JS (vs 80kb of stimulus) and avoid a trip to the server:<p><pre><code> document.querySelector(&quot;input&quot;).addEventListener(&quot;input&quot;, e =&gt; { document.querySelector(&quot;p&quot;).innerHTML = `&lt;strong&gt;${e.target.value}&lt;&#x2F;strong&gt;` }); </code></pre> Reusing the logic could be done if the server was node or through compiling the formatter to wasm. I struggle to see how Hotwire makes things simpler and has comparable speed to a SPA?
评论 #26557894 未加载
评论 #26557941 未加载