button to launch erb scripts and display outputs.

37 views
Skip to first unread message

Bigmac Turdsplash

unread,
Mar 12, 2016, 2:35:27 AM3/12/16
to rubyonra...@googlegroups.com
i have a fresh rails application called basic... i have boot strap
working and looking to create a button that if pressed will launch a
ruby script and display its live output as it happens...

<button type="button" class="btn btn-default">button</button>

<%= 100.times do |i|%>
<%= print i*i%>
<%= sleep 1%>
<%=end%>

i hope some one can point me in the right direction... i love ruby, but
never tried rails...

--
Posted via http://www.ruby-forum.com/.

Colin Law

unread,
Mar 12, 2016, 3:44:56 AM3/12/16
to Ruby on Rails: Talk
On 12 March 2016 at 07:34, Bigmac Turdsplash <li...@ruby-forum.com> wrote:
> i have a fresh rails application called basic... i have boot strap
> working and looking to create a button that if pressed will launch a
> ruby script and display its live output as it happens...
>
> <button type="button" class="btn btn-default">button</button>
>
> <%= 100.times do |i|%>
> <%= print i*i%>
> <%= sleep 1%>
> <%=end%>
>

The web page and any javascript in it run in the browser. Which
computer do you want the ruby script to run on? The server running
your rails app or the client PC running in the browser?

If you answer is the PC running the browser then, for a start, how
would you even know that ruby is installed on that PC? It is for
exactly that sort of task that javascript was invented.

Colin

Bigmac Turdsplash

unread,
Mar 12, 2016, 12:47:11 PM3/12/16
to rubyonra...@googlegroups.com
Colin Law wrote in post #1182139:
no, i want to run ruby code on the server and display the console output
to the viewer in real time as each line is displayed

<%=# if button is pressed=>
<button type="button" class="btn btn-default">button</button>
<p><%=100.times do |x| puts x; sleep 1; end=></p>

i dont know what im doing, but im excited to learn...

Colin Law

unread,
Mar 12, 2016, 4:23:31 PM3/12/16
to Ruby on Rails: Talk
Basically no, you can't do that. There are ways you could make it
look as if that is what is happening, but that would not be the sort
of thing a beginner could knock off easily. I would get the hang of
the conventional web architecture before trying to push the
boundaries.

Colin

Bigmac Turdsplash

unread,
Mar 12, 2016, 8:05:56 PM3/12/16
to rubyonra...@googlegroups.com
Colin Law wrote in post #1182154:
> On 12 March 2016 at 17:46, Bigmac Turdsplash <li...@ruby-forum.com>
> wrote:
>>> exactly that sort of task that javascript was invented.
>> i dont know what im doing, but im excited to learn...
> Basically no, you can't do that. There are ways you could make it
> look as if that is what is happening, but that would not be the sort
> of thing a beginner could knock off easily. I would get the hang of
> the conventional web architecture before trying to push the
> boundaries.
>
> Colin

Maybe just a example for if button is pressed then <=%puts 'hi WORLD'=>

Bigmac Turdsplash

unread,
Mar 12, 2016, 9:55:46 PM3/12/16
to rubyonra...@googlegroups.com
Bigmac Turdsplash wrote in post #1182156:
I found gem puma, seems like the right place to start

Colin Law

unread,
Mar 13, 2016, 4:35:40 AM3/13/16
to Ruby on Rails: Talk
Puma is the web server that is used as standard by the latest rails
versions in development mode. I don't see how that is going to help
you.

Perhaps if you explained what you are actually trying to achieve (at a
higher level) we may be able to provide a solution.

Colin

Bigmac Turdsplash

unread,
Mar 13, 2016, 3:08:30 PM3/13/16
to rubyonra...@googlegroups.com
Colin Law wrote in post #1182158:
With puma i could print live ruby output to the client each line would
print as it happens.

But i hope to print data to a <div> or <p> when a button is pressed <%=
puts '<p> hi </p>' =>

Colin Law

unread,
Mar 13, 2016, 3:31:07 PM3/13/16
to Ruby on Rails: Talk


On 13 Mar 2016 19:08, "Bigmac Turdsplash" <li...@ruby-forum.com> wrote:
>
> Colin Law wrote in post #1182158:
> > On 13 March 2016 at 02:55, Bigmac Turdsplash <li...@ruby-forum.com>
> > wrote:
> >>>> boundaries.
> >>>>
> >>>> Colin
> >>>
> >>> Maybe just a example for if button is pressed then <=%puts 'hi WORLD'=>
> >>
> >> I found gem puma, seems like the right place to start
> >
> > Puma is the web server that is used as standard by the latest rails
> > versions in development mode.  I don't see how that is going to help
> > you.
> >
> > Perhaps if you explained what you are actually trying to achieve (at a
> > higher level) we may be able to provide a solution.
> >
> > Colin
>
> With puma i could print live ruby output to the client each line would
> print as it happens.

I would be very interested to know how to do that.

Colin

>
> But i hope to print data to a <div> or <p> when a button is pressed  <%=
> puts '<p> hi </p>' =>
>
> --
> Posted via http://www.ruby-forum.com/.
>

> --
> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/eaf977148895c72331d409f0899dbae3%40ruby-forum.com.
> For more options, visit https://groups.google.com/d/optout.

Bigmac Turdsplash

unread,
Mar 13, 2016, 6:50:50 PM3/13/16
to rubyonra...@googlegroups.com
I have found the right module actioncontroler::live

Colin Law

unread,
Mar 14, 2016, 4:18:31 AM3/14/16
to Ruby on Rails: Talk
On 13 March 2016 at 22:50, Bigmac Turdsplash <li...@ruby-forum.com> wrote:
> I have found the right module actioncontroler::live

That will not let you do what you said you wanted to do, which was put
the following on a web page
<%= 100.times do |i|%>
<%= print i*i%>
<%= sleep 1%>
<%=end%>
and have it update the screen in real time.

I said that was not possible and asked you to tell us what you were
actually trying to achieve. Had you answered that question we could
probably have saved you a lot of time by pointing you in the right
direction.

Colin

Bigmac Turdsplash

unread,
Mar 14, 2016, 12:16:21 PM3/14/16
to rubyonra...@googlegroups.com
https://youtu.be/O9w_hxoxtW0

Here is a demo that I can make work for my situation.
With the actioncontroler::live Module.

10.times do |X|
Response.stream.write("hi world #{X}")
Sleep 1
End
Ensure
Response.stream.close

As for a button to trigger the event, ill have to make due with a
href=>/liveStream-hiWorld.ntml.erb

Colin Law

unread,
Mar 14, 2016, 4:05:29 PM3/14/16
to Ruby on Rails: Talk
On 14 March 2016 at 16:15, Bigmac Turdsplash <li...@ruby-forum.com> wrote:
>
> Here is a demo that I can make work for my situation.
> With the actioncontroler::live Module.
>
> 10.times do |X|
> Response.stream.write("hi world #{X}")
> Sleep 1
> End
> Ensure
> Response.stream.close
>
> As for a button to trigger the event, ill have to make due with a
> href=>/liveStream-hiWorld.ntml.erb

Please quote the message you are replying to, this is a mailing list
not a forum (though you may be accessing it via a forum like
interface) so it is not clear to what you are replying. Thanks.

You specifically requested that the script be embedded in the the web
page. Your example is just live streaming from the controller.

I don't think that href will work very well. If you look for examples
of using the live feature you will find how to stream into your web
page.

Colin

Bigmac Turdsplash

unread,
Mar 15, 2016, 8:10:04 PM3/15/16
to rubyonra...@googlegroups.com
Colin Law wrote in post #1182193:
/messaging/index.html.erb
<script> source = new EventSource("/messaging");
source.addEventListener("message", function(response) {
// Do something with response.data
}, false);
</script>

Im using the actioncontroller::live example found on the domentation
site, i have things setup properly with routes.rb...
I can successfully append data to a <div> in the clients browser.
But the browser will keep streaming in a endless loop. Maybe this is
expected behaviour?

Bigmac Turdsplash

unread,
Mar 15, 2016, 9:25:17 PM3/15/16
to rubyonra...@googlegroups.com
Bigmac Turdsplash wrote in post #1182233:
ensure
response.stream.close

it seems that this part of the controller does nothing.

if i add to my index.html.erb source.close(); then it will effectively
close the stream. so with that said, i could do a if expressions on the
client side "if stream_kill, then source.close():"

but this seems a little hacky?

Colin Law

unread,
Mar 16, 2016, 4:28:10 AM3/16/16
to Ruby on Rails: Talk
Can you copy/paste the code you are using to do the streaming in the
controller please.

Colin
Reply all
Reply to author
Forward
0 new messages