从HTML传递Ruby函数中的参数 [英] Passing parameter in a Ruby function from HTML
问题描述
我有两个文件。 .rb (带有Ruby代码)和 .erb (带有一些ruby脚本的HTML文件)。我在 .rb 中从 .erb 调用Ruby函数。
.erb
< a href =<%= url_for:action =>:showProducts(i)%>>点击此处< / a>
.rb
def showProducts(param)
//一些代码
结束
我可以在不传递参数的情况下调用函数。但是当我将参数传递给函数然后调用它时,我收到一个错误。我知道这是从 .erb 调用参数化函数的不正确方法。从HTML调用参数化函数的正确方法是什么?
我找到了解决我的问题的方法:
< a href =<%= url_for:action =>:showProducts,:id =>'Hello'%> >
.rb函数:
def showProducts(param)
//某些代码
结束
I have two files. .rb (with Ruby code) and .erb(HTML file with some ruby script). I am calling a Ruby function in .rb from .erb.
.erb
<a href="<%= url_for :action => :showProducts(i) %>">Click here</a>
.rb
def showProducts(param)
//Some code
end
I am able to call a function without passing parameters to it. But as and when I pass parameters to function and then call it, I receive an error. I know this is the incorrect way to call a parametrized function from .erb. What is the correct way of calling a parameterized function from HTML?
I found the solution to my problem :
<a href="<%= url_for :action => :showProducts, :id=> 'Hello' %>">
.rb function:
def showProducts(param)
//Some code
end
这篇关于从HTML传递Ruby函数中的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!