Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Able to store code in session untill reset button is clicked #2

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions app/controllers/home_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,22 @@ def index
end

def evaluate

respond_to do |format|
format.js {

if session[:code]
if params[:code]
session[:code]=session[:code]+"\n"+params[:code]
end
else
session[:code]=params[:code]
end
file = File.open('tmp/ruby.rb', 'w')
file.syswrite(params["code"])
file.syswrite(session[:code])
file.close

system('ruby tmp/ruby.rb > tmp/result.txt')

@result = File.read('tmp/result.txt')
@code = params["code"]

Expand All @@ -19,4 +27,9 @@ def evaluate

end
end

def reset
session[:code]=nil
redirect_to '/'
end
end
3 changes: 3 additions & 0 deletions app/views/home/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

<div id="editor" style="height: 500px; width: 100%" name="code"></div>
<button onclick="run()">Run</button>
<a href="/reset">
<button>Reset</button>
</a>
<script>
var editor = ace.edit("editor");
editor.getSession().setMode("ace/mode/ruby");
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
get 'home/index'
root 'home#index'
post '/evaluate' => 'home#evaluate'
get '/reset' => 'home#reset'

# The priority is based upon order of creation: first created -> highest priority.
# See how all your routes lay out with "rake routes".
Expand Down