简体   繁体   English

如何将JavaScript变量传递到Rails控制器

[英]How to pass a JavaScript var into a Rails Controller

I'm looking to pass a JavaScript variable into a Rails Controller . 我希望将JavaScript变量传递到Rails Controller中 The interesting part is that the variable is generated inside Canman , and I cannot use it (yet) outside of it. 有趣的是,变量是在Canman内部Canman ,但我不能在其外部使用它。

This is probably just JavaScript and not necessarily related with Canman. 这可能只是JavaScript,不一定与Canman有关。 But I'm just not sure what it is happening here. 但是我只是不确定这里发生了什么。

The approach I'm following (but completely open if there is a better way) is to populate a hidden field with jQuery, just to access the data via params from the controller. 我采用的方法(如果有更好的方法,则完全开放)是使用jQuery填充隐藏字段,只是通过控制器的参数访问数据。

If possible (and if this is a good practice) I will like to avoid the form, and just call some JavaScript on click and then pass that variable to the controller. 如果可能(并且这是一个好的做法),我将避免使用该表单,只需单击click调用一些JavaScript,然后将该变量传递给控制器​​即可。


View 视图

= form_for @post do |form|
  = form.hidden_field :base64
  = form.submit


JavaScript 的JavaScript

$('form').submit(function(event){

  Caman('#canvas', img, function() {
    var imageBase64 = this.toBase64();
    alert(imageBase64); // works fine
    $('#post_base64').val(imageBase64);
  });

  alert(imageBase64); // nothing
});


PostsController PostsController

def update
  @post = Post.find(params[:id])
  raise '¯\_(ツ)_/¯'
  ...
end


post_params post_params

=> {"base64"=>""}

Also, I read that an option could be to make an AJAX request. 另外,我读到一个选项可能是发出AJAX请求。 However, I'm not sure how to proceed with that, yet. 但是,我不确定如何进行此操作。

At some point, I tried with a text_area instead of a hidden_field. 在某些时候,我尝试使用text_area而不是hidden_​​field。 The text_area got populated with the right data. 在text_area中填充了正确的数据。 However, params never got the data. 但是,参数从未获得数据。 If I got back via the browser button, the data was in the text_area, and clicking on submit one more time, populates the params as expected. 如果我通过浏览器按钮返回,则数据在text_area中,然后单击一次提交,则按预期填充参数。

在此处输入图片说明

Thanks in advance! 提前致谢!

Short answer: Ajax . 简短答案: Ajax

The goal was to send the value of a variable (a base64 image) to my rails controller, and once there, keep going just with Ruby. 目的是将变量的值(base64图像)发送到我的rails控制器,一旦到达,就继续使用Ruby。

At the end, I created a simple Ajax function to send data from my client (Image from browser) to my server (Rails Controller) via params 最后,我创建了一个简单的Ajax函数,以通过params将客户端(浏览器中的图像)中的数据发送到服务器(Rails Controller)中

save_canvas.js save_canvas.js

$(document).on('click', '.save_canvas', function() {
  event.preventDefault()
  var base64Data = canvas.toDataURL('png')

  $.ajax({
    type:    "POST",
    url:     "http://localhost:3000/pictures/",
    data:    { image: base64Data },
    success: function(post){ console.log('success') },
    error:   function(post){ console.log(this) }
  })
})

pictures_controller.rb pictures_controller.rb

  def create
    @picture = Picture.new(image: params[:image])
    @picture.save
    redirect_to @picture
  end

I got support to achieve this here 我在这里实现了支持

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM