将JavaScript变量传递到Rails控制器中 [英] Passing a javascript variable into a rails controller
问题描述
我需要将一个javascript变量传递到Rails控制器中.这是我的应用代码:
I need to pass one javascript variable into a rails controller. Here is my app code:
view.html.erb
<script type="text/javascript">
var imageURL;
var sCode = function () {
$("#qr").html("<p align='center'><img src='https://chart.googleapis.com/chart?chs=250x250&cht=qr&chl=123&choe=UTF-8'/></p>");
imageURL = "https://chart.googleapis.com/chart?chs=150x150&cht=qr&chl=123&choe=UTF-8";
}
</script>
<div class=" modal-body" id="qr">
</div>
<div class="modal-footer" id="footer">
<%= form_tag({:controller => 'branches',:action => 'mailme' }, {:method => :post})
do %>
<%= submit_tag "Mail", :id => "mailme", :class => "btn btn-primary pull-left" %>
<% end %>
</div>
在这里,我需要将imageURL
变量从控制器branches
传递给方法mailme
.如何获得该变量?我试图用submit_tag
附加该变量,还尝试了hidden_filed_tag
来获取该变量,但没有任何效果.
Here I need to pass the imageURL
variable to method mailme
from controller branches
. How can I get that variable? I have tried to attach that variable with submit_tag
and also tried hidden_filed_tag
to get that, but nothing worked.
谢谢
推荐答案
您必须使用JavaScript来处理将以表单形式提交的隐藏字段标记.
You have to use javascript to manipulate a hidden field tag that will be submitted in the form.
表格:
<%= hidden_field_tag "image_url" %>
JS:
<script type="text/javascript">
var imageURL;
var sCode = function () {
$("#qr").html("<p align='center'><img src='https://chart.googleapis.com/chart?
chs=250x250&cht=qr&chl=123&choe=UTF-8'/></p>");
imageURL = "https://chart.googleapis.com/chart?chs=150x150&cht=qr&chl=123&choe=UTF-8";
$('#image_url').val(imageURL);
}
</script>
当然,您必须以某种方式执行sCode
中声明的功能.
Of course you'll have to execute the function declared in sCode
somehow.
在控制器中,只需访问params[:image_url]
即可获取URL
In the controller, just access params[:image_url]
to get the URL
这篇关于将JavaScript变量传递到Rails控制器中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!