How to get value from Javascript Input in Ruby Controller?

I currently have javascript in my view and it contains inputs so users can input values

<input type = "text" name = "date8" id = "date8" value = "" size =25 >

I want to be able to access the value in this input once a method in my controller is called. How can I do this? params[date8] does not work which makes sense since it isnt a ruby textfield. Anyone have any ideas? Thanks

It depends how you send that value to the controller.

If you use a form to submit data, then params[:date8] would work.

If you use a link, then you need to append the value to the link, like yourdomain.com

Thanks for the reply! I currently have a _form.html.erb file which contains all the javascript/html. When I do params[:date8], I see that there isn't anything in there. Just an empty string even though something was typed into it.

check the logs to find out whether other params were passed to the controller or not. Is the input in inside the form? How do you use javascript on that input field?

Currently the javascript contains functions for a calendar app so users do not have to insert the date manually

Here is some of the HTMl <div> <form name = "calendar1"> <input type = "text" name = "date1" id = "date1" value = "" size =25 readonly style = "border:0px;"> <A HREF = "#" onclick = "cal.select(document.forms['calendar1'].date1,'anchor1','MM/dd/yyyy');return false;" NAME = "anchor1" ID = "anchor1">select date</A> </form> </div>

The calendar app works fine, when I test it everything gets inserted correctly into the entry field/input. Its trying to get the controller to recognize the value of the input. Here is my controller. def create con = Mysql.new 'xx.xx.x.x','xxxx','xxxx', 'worksheet' date1 = params[:date1] rs = con.query 'INSERT INTO test VALUES ("'+date1.to_s+'")' end I know the method is called and everything is linked because the database has new entries in it. However, I only see an empty string with nothing in it.

Is it possible to pass in the value of date1 into the method when the button is pressed? That way I wont have to find a way to reference everything in the controller. If so I can I change my button_to to reflect that?

<div> <%= button_to "Create", action:"create"%> </div>

Please not.

You can either append the value to the url or put the value inside the form.

You can check whether the date1 was posted to the server. Use web inspector to see the posted data. If the date1 was not even included in the posted data, then you need to check your html and javascript. Or if the date1 was included but it is blank, then you need to figure out why it is blank.

Currently the javascript contains functions for a calendar app so users do not have to insert the date manually

Here is some of the HTMl <div> <form name = "calendar1"> <input type = "text" name = "date1" id = "date1" value = "" size =25 readonly style = "border:0px;"> <A HREF = "#" onclick = "cal.select(document.forms['calendar1'].date1,'anchor1','MM/dd/yyyy');return false;" NAME = "anchor1" ID = "anchor1">select date</A> </form> </div>

The calendar app works fine, when I test it everything gets inserted correctly into the entry field/input. Its trying to get the controller to recognize the value of the input. Here is my controller. def create con = Mysql.new 'xx.xx.x.x','xxxx','xxxx', 'worksheet' date1 = params[:date1] rs = con.query 'INSERT INTO test VALUES ("'+date1.to_s+'")' end I know the method is called and everything is linked because the database has new entries in it. However, I only see an empty string with nothing in it.

Install the ruby debugger. Then insert the debugger to start at this point:

def create con = Mysql.new 'xx.xx.x.x','xxxx','xxxx', 'worksheet'

debugger

date1 = params[:date1] rs = con.query 'INSERT INTO test VALUES ("'+date1.to_s+'")' end

Then, when the debugger starts up, check to see what params contains: