I am following the Rails Tutorial by Michael Hart and I am already on Chapter 7. But I wanna do something different right now, which the tutorial doesn't teach. I wanna run a script file inside my webpage. How I can do that? I saw other posts here saying to use Sinatra, but since I am following this tutorial I don't think it is such a good idea to use it because it can make everything different from the tutorial.
我正在学习Michael Hart的Rails教程,我已经在第七章了。但是我现在想做一些不同的事情,这是本教程不教的。我想在我的网页中运行一个脚本文件。我怎么做呢?我在这里看到过其他文章说要使用Sinatra,但是由于我正在学习本教程,所以我不认为使用它是一个好主意,因为它可以使所有东西与本教程不同。
Here is the simple script I wanna run on my webpage:
这是我想在我的网页上运行的简单脚本:
#Somando idades
def soma_vetor_excluindo(index,vet)
soma = 0
for i in 0..9
if(i!=index)
soma = soma + vet[i].to_i
end
end
return soma
end
def soma_vetor(vet)
soma = 0
for i in 0..9
soma = soma + vet[i].to_i
end
return soma
end
def maior_vetor(vet)
maior = 0
for i in 0..9
if(maior < vet[i])
maior = vet[i]
end
end
return maior
end
idades = (0..9).collect{rand(99)+1}
soma_idades = (0..9).collect{0} soma = 0
print "#{idades} \n"
for i in 0..9
soma_idades[i] = soma_vetor_excluindo(i,idades)
end
print "#{soma_idades} \n"
div = soma_vetor(soma_idades) / 9
resp = div - maior_vetor(soma_idades)
puts "#{resp}"
2 个解决方案
#1
3
The simplest way to do it would be to make the method soma_vetor_excluindo
, soma_vetor
, maior_vetor
, etc, controller methods, so when you send data through a form or ajax, the action would trigger, calculate the values and return you a result.
最简单的方法是使方法soma_vetor_exclusive indo、soma_vetor、maior_vetor等成为控制器方法,所以当您通过表单或ajax发送数据时,操作将触发、计算值并返回结果。
Knowing this, you can have a controller, let's say MathController.rb
, and inside it, the soma_vetor_excluindo
method:
知道这个,你可以有一个控制器,比如MathController。rb,以及其中的soma_vetor_exclusive indo方法:
class MathController < ApplicationController
def soma_vetor_excluindo
end
def soma_vetor
end
def maior_vetor
end
end
To access this, you probably need a route
, so on your routes.rb
add something like this:
要访问它,你可能需要一条路径,在你的路径上。rb添加如下内容:
get 'math/soma_vetor_excluindo/:index/:vet', to 'math#soma_vetor_excluindo'
get 'math/soma_vetor/:vet', to 'math#soma_vetor'
get 'math/maior_vetor/:vet', to 'math#maior_vetor'
This means that when your browser hit localhost/math/soma_vetor_excluindo/1/2
or the other urls, it would send a get request to the controller calling the soma_vetor_excluindo
method and putting in the parameters, params[:index]
and params[:vet]
, so theoretically the script would run.
这意味着当浏览器访问localhost/math/ soma_vetor_exclusive indo/1/2或其他url时,它将向调用soma_vetor_exclusive indo方法的控制器发送一个get请求,并输入参数params[:index]和params[:vet],因此理论上脚本将运行。
The thing is, you can adapt your controller to do something like this with very little work.
问题是,你可以调整你的控制器去做这样的事情,只需要做很少的工作。
#2
0
I believe the simplest solution is to load a page per script. First you add a path for your script into the routes.rb
with something like:
我认为最简单的解决方案是每个脚本加载一个页面。首先,将脚本的路径添加到路由中。rb类似:
get 'scripts/your_script', to 'scripts#your_script
And in the controller (app/scripts_controller.rb) you should add your code like this:
在控制器(app/scripts_controller.rb)中,您应该添加如下代码:
class ScriptsController < ApplicationController
#Somando idades
def soma_vetor_excluindo(index,vet)
soma = 0
for i in 0..9
if(i!=index)
soma = soma + vet[i].to_i
end
end
return soma
end
def soma_vetor(vet)
soma = 0
for i in 0..9
soma = soma + vet[i].to_i
end
return soma
end
def maior_vetor(vet)
maior = 0
for i in 0..9
if(maior < vet[i])
maior = vet[i]
end
end
return maior
end
def your_script
idades = (0..9).collect{rand(99)+1}
soma_idades = (0..9).collect{0}
soma = 0
answer = "#{idades} \n"
for i in 0..9
soma_idades[i] = soma_vetor_excluindo(i,idades)
end
answer << "#{soma_idades} \n"
div = soma_vetor(soma_idades) / 9
resp = div - maior_vetor(soma_idades)
answer << "#{resp}"
render(text: answer)
end
end
when you access the page scripts/your_script
, it should render a plain text presentation of your script result.
当您访问页面脚本/your_script时,它应该呈现您的脚本结果的纯文本表示。
Although this is not the most elegant solution, it should solve your problem.
虽然这不是最优雅的解决方案,但它应该能解决您的问题。
#1
3
The simplest way to do it would be to make the method soma_vetor_excluindo
, soma_vetor
, maior_vetor
, etc, controller methods, so when you send data through a form or ajax, the action would trigger, calculate the values and return you a result.
最简单的方法是使方法soma_vetor_exclusive indo、soma_vetor、maior_vetor等成为控制器方法,所以当您通过表单或ajax发送数据时,操作将触发、计算值并返回结果。
Knowing this, you can have a controller, let's say MathController.rb
, and inside it, the soma_vetor_excluindo
method:
知道这个,你可以有一个控制器,比如MathController。rb,以及其中的soma_vetor_exclusive indo方法:
class MathController < ApplicationController
def soma_vetor_excluindo
end
def soma_vetor
end
def maior_vetor
end
end
To access this, you probably need a route
, so on your routes.rb
add something like this:
要访问它,你可能需要一条路径,在你的路径上。rb添加如下内容:
get 'math/soma_vetor_excluindo/:index/:vet', to 'math#soma_vetor_excluindo'
get 'math/soma_vetor/:vet', to 'math#soma_vetor'
get 'math/maior_vetor/:vet', to 'math#maior_vetor'
This means that when your browser hit localhost/math/soma_vetor_excluindo/1/2
or the other urls, it would send a get request to the controller calling the soma_vetor_excluindo
method and putting in the parameters, params[:index]
and params[:vet]
, so theoretically the script would run.
这意味着当浏览器访问localhost/math/ soma_vetor_exclusive indo/1/2或其他url时,它将向调用soma_vetor_exclusive indo方法的控制器发送一个get请求,并输入参数params[:index]和params[:vet],因此理论上脚本将运行。
The thing is, you can adapt your controller to do something like this with very little work.
问题是,你可以调整你的控制器去做这样的事情,只需要做很少的工作。
#2
0
I believe the simplest solution is to load a page per script. First you add a path for your script into the routes.rb
with something like:
我认为最简单的解决方案是每个脚本加载一个页面。首先,将脚本的路径添加到路由中。rb类似:
get 'scripts/your_script', to 'scripts#your_script
And in the controller (app/scripts_controller.rb) you should add your code like this:
在控制器(app/scripts_controller.rb)中,您应该添加如下代码:
class ScriptsController < ApplicationController
#Somando idades
def soma_vetor_excluindo(index,vet)
soma = 0
for i in 0..9
if(i!=index)
soma = soma + vet[i].to_i
end
end
return soma
end
def soma_vetor(vet)
soma = 0
for i in 0..9
soma = soma + vet[i].to_i
end
return soma
end
def maior_vetor(vet)
maior = 0
for i in 0..9
if(maior < vet[i])
maior = vet[i]
end
end
return maior
end
def your_script
idades = (0..9).collect{rand(99)+1}
soma_idades = (0..9).collect{0}
soma = 0
answer = "#{idades} \n"
for i in 0..9
soma_idades[i] = soma_vetor_excluindo(i,idades)
end
answer << "#{soma_idades} \n"
div = soma_vetor(soma_idades) / 9
resp = div - maior_vetor(soma_idades)
answer << "#{resp}"
render(text: answer)
end
end
when you access the page scripts/your_script
, it should render a plain text presentation of your script result.
当您访问页面脚本/your_script时,它应该呈现您的脚本结果的纯文本表示。
Although this is not the most elegant solution, it should solve your problem.
虽然这不是最优雅的解决方案,但它应该能解决您的问题。