0

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" 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?

1 Answer 1

1

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.

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

6 Comments

it goes through ok until it describes values. The values for the date is set to NULL... the parameters are still ok
Can you parse the date with Date.parse? What's an example date you're receiving as a parameter?
where would i .parse??.The example im receiveing is a string. i dont want to convert the string to a Date object because that will and the time and dont want that. im getting that string in the format yyyy-mm-dd
Your comments are getting increasingly incoherent. Do you want a Date without time, or a DateTime which includes time? In either case, just pick your model: Date.parse(params[:wc]) or DateTime.parse(params[:wc]) so you can assign that to your week_commencing attribute.
the model im looking for is :date. but when i put in your code i get "can't dup NilClass"
|

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.