[英]Passing a javascript variable into a rails controller
I need to pass one javascript variable into a rails controller. 我需要将一个javascript变量传递给rails控制器。 Here is my app code: 这是我的应用代码:
view.html.erb 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>
Here I need to pass the imageURL
variable to method mailme
from controller branches
. 在这里,我需要将imageURL
变量从控制器branches
传递给方法mailme
。 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. 我试图用submit_tag
附加该变量,并尝试使用hidden_filed_tag
来获取它,但没有任何效果。
Thanks 谢谢
You have to use javascript to manipulate a hidden field tag that will be submitted in the form. 您必须使用javascript来操作将在表单中提交的隐藏字段标记。
Form : 表格:
<%= hidden_field_tag "image_url" %>
JS : 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>
Of course you'll have to execute the function declared in sCode
somehow. 当然你必须以某种方式执行sCode
声明的函数。
In the controller, just access params[:image_url]
to get the URL 在控制器中,只需访问params[:image_url]
即可获取URL
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.