javascript 将javascript变量传递给rails控制器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20786831/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 19:18:34  来源:igfitidea点击:

Passing javascript variables to rails controller

javascriptruby-on-railsvariables

提问by CodeGeeky

Here is the problem i'm stuck on. I want to pass the javascript variables to the rails controller.

这是我遇到的问题。我想将 javascript 变量传递给 rails 控制器。

<script>
var mdate = "26 December 2013";
var phone = prompt('Enter your phone!');
if (phone) {
    //Passing mdate and phone variables to rails controller(book_date & phone)
}
else
{
    alert("Cancelled");
}
</script>

My controller

我的控制器

def new
        @booking = Booking.new
end
def create
    @booking = Booking.new(book_param)
    if @booking.save
        redirect_to root_url
    else
        flash[:notice_booking_failed] = true
        redirect_to root_url
    end
end

private
def book_param
    params.require(booking).permit(:id, :book_date, :phone)
end

Thank you in advance!

先感谢您!

采纳答案by Siva

Technically you cant pass variables between two languages.

从技术上讲,您不能在两种语言之间传递变量。

You can pass those values to rails controller by appending in url

您可以通过附加在 url 中将这些值传递给 rails 控制器

<script>
var mdate = "26 December 2013";
var phone = prompt('Enter your phone!');
if (phone) {
    //Passing mdate and phone variables to rails controller(book_date & phone)
    window.open("localhost:3000//controller/create?mdate="+mdate+"&phone="+phone,"_self")
}
else
{
    alert("Cancelled");
}
</script>

In your controller

在您的控制器中

def create
    data = params[:date]
    phone = params[:phone]
    @booking = Booking.new(book_param)
    if @booking.save
        redirect_to root_url
    else
        flash[:notice_booking_failed] = true
        redirect_to root_url
    end
end

NOTE:Make sure you configure your config/route.rb accordingly

注意:确保相应地配置了 config/route.rb

More Info http://guides.rubyonrails.org/routing.html

更多信息http://guides.rubyonrails.org/routing.html

回答by Rubyist

Ajax code in jQuery:

jQuery 中的 Ajax 代码:

$("#submit_button").submit(function(event) {

  /* stop form from submitting normally */
   event.preventDefault();

  /* get values from elements on the page: */
   var mdate = $('#mdate').val();
   var phone = $('#phone').val();

  /* Send the data using post and put the results in a div */
    $.ajax({
      url: "/BookCreate/?mdate="+mdate+"&phone="+phone,
      type: "post",
      data: values,
      success: function(){
        alert('Saved Successfully');
      },
      error:function(){
       alert('Error');
      }
    });
});

Routes : ( As I am assuming your controller name is book )

路线:(因为我假设您的控制器名称是 book )

match '/BookCreate', to: 'book#create'

For this you have to add jquery file to your code or this link

为此,您必须将 jquery 文件添加到您的代码或此链接

<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script src="http://code.jquery.com/jquery-migrate-1.2.1.min.js"></script>

enter image description here

在此处输入图片说明

回答by Anton Tsapov

You can use something like

你可以使用类似的东西

$.post("/bookings?booking[phone]=" + phone + "&booking[book_date]=" + mdate)

It will go to BookingsController#createaction with params hash:

它将BookingsController#create使用 params 哈希执行操作:

{ booking: { phone: "entered from prompt", book_date: "26 December 2013" } }