using a javascript returned value and then adding it to a ruby database
I have a string that needs to be turned into a date attribute to be stored in a Ruby database. There is a javascript function that collects the data and submits it to a ruby method which in turn submits to the database. However one of the fields is a "Date" 开发者_Python百科field. Not a "DateTime" field so i cannot use the Date() function provied by js. The string i have is already in the format needed its simply getting the db to recognise this.
the html
<form action="/submiteffort" id="effort_form" method="post">
<input type="hidden" id="week_commencing" name="week_commencing" value="x">
<input type="hidden" id="task_id" name="task_id" value="x">
<input type="hidden" id="effort_hours" name="hours" value="0">
</form>
<!-- Javascript code to submit the hidden form -->
<script>
function submiteffort( elem, name )
{
var date = getdate();
$("#week_commencing").val(date);
$("#effort_hours").val( $( elem ).val() );
$("#task_id").val( name );
$("#effort_form").submit();
return true;
}
getDate() function
function getdate()
{
var myspan = document.getElementById('startDate');
var span_textnode = myspan.firstChild;
var span_text = span_textnode.data;
myDateParts = span_text.split("/");
var d = myDateParts[0];
var dd = days(d);
var m = myDateParts[1];
var subSection2 = m.substring(1,0);
if (subSection2 == "0") {
mm = m;
}
else {
mm = "0" + myDateParts[1];
}
var yy = myDateParts[2];
var actDate = yy + "-" + mm + "-" + dd;
return actDate;
}
submit method in the controller
def submit
effort = Effort.new
effort.user_id = 10 #current_user.id
effort.project_task_id = params[:task_id]
effort.week_commencing = params[:wc]
effort.hours = params[:hours]
effort.save
respond_to do |format|
format.html
format.js
end
end
How do i get my database to recognise that the value i want needs to the a "DATE" and submit it to the database?
If you're submitting a date in a format that Date
recognizes, the conversion should be done for you. This requires that week_commencing
is declared as a :date
type column.
Have a look at your log/development.log
to see what the params
are set to for the request in question. Make sure that the generated date is coming through correctly.
精彩评论