简体   繁体   English

将JavaScript变量传递给Rails控制器

[英]Passing javascript variables to rails controller

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! 先感谢您!

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

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>

在此处输入图片说明

You can use something like 您可以使用类似

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

It will go to BookingsController#create action with params hash: 它将使用参数散列转到BookingsController#create操作:

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

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

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