9

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

<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!

3 Answers 3

9

Technically you cant pass variables between two languages.

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

<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

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

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks so much. I simply did window.open("/controller/create?mdate="+mdate+"&phone="+phone,"_self") and it worked for me. :)
You can easily pass data through a hidden field.
question is about how to pass JS variables to rails. Hidden field is not required here in anyway.
Hello shiva, is it required for those two fields (data, phone) to be inside the form? If yes then what should I give the controller name in form_for tag? In my situation I have member_code field and many buttons to create method of different controllers. I want to send member_code to respective controller each time a button (or link_to) is pressed. Is it possible?
6

Ajax code in jQuery:

$("#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 )

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

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

<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

Comments

0

You can use something like

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

It will go to BookingsController#create action with params hash:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.