SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Enthusiast
    Join Date
    Mar 2005
    Posts
    64
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    newbie ? regarding datetime_select and accessing parameters

    I have a newbie ?. I have a form that looks like this:

    <%= start_form_tag :action=>"save_scheduling" %>
    <%= datetime_select("scheduled_at", "written_on") %>
    <%= submit_tag("update") %>
    <%= end_form_tag %>


    and a controller script that looks like this with the column
    scheduled_at set to a datetime field:
    def save_scheduling
    mail=Mail.find(5)
    #scheduled_at=DateTime.now
    mail.update_attributesscheduled_at=>params[:scheduled_at])
    end

    when, i submit, I always get:
    scheduled_at: 0000-00-00 00:00:00
    in MySQL, even when there are time. If I manually set the scheduled_at
    value and change the update_attributes, the time gets set to now. It
    seems like a problem accessing the params variable correctly. The
    variable does appear to get set to a ruby DateTime value correctly as
    the null value (set as default) is gone.

    any help would be appreciated.

    -jon

  2. #2
    SitePoint Guru silver trophy Luke Redpath's Avatar
    Join Date
    Mar 2003
    Location
    London
    Posts
    794
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What do you get if you log the param value?

    Code:
    logger.info "Date Value: #{params[:scheduled_at].inspect}"

  3. #3
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is incorrect:

    Code:
    mail.update_attributes(:scheduled_at => params[:scheduled_at])
    because params[:scheduled_at] is a Hash. The correct code is:

    Code:
    mail.update_attributes(:scheduled_at => params[:scheduled_at][:written_on])
    It may be better to change the code to this:
    Code:
    <%= start_form_tag :action=>"save_scheduling" %>
    <%= datetime_select("mail", "scheduled_at") %>
    <%= submit_tag("update") %>
    <%= end_form_tag %>
    Code:
    def save_scheduling
      mail = Mail.find(5)
      mail.attributes = params[:mail]
    end
    I hope this works...

  4. #4
    SitePoint Enthusiast
    Join Date
    Mar 2005
    Posts
    64
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    so I made these changes:

    <%= datetime_select("mail", "scheduled_at") %>

    when I log via:
    logger.info "Date Value: #{params[:mail].inspect}"

    for setting via form to November 19, 2006 10:37, I get this in development.log,
    Date Value: {"scheduled_at(5i)"=>"37", "scheduled_at(1i)"=>"2006", "scheduled_at(2i)"=>"12", "scheduled_at(3i)"=>"14", "scheduled_at(4i)"=>"10"}

    and when I try doing
    Code:
    def save_scheduling
      mail = Mail.find(5)
      mail.attributes = params[:mail]
    end
    It is still trying to insert null. I know it's suppoosed to instantiate a DateTime object but it doesn't appear to be. Or do I need to do any extra handling of the data? Is "mail" a reserved word that might be causing problems?

    thx for the help. I'm kinda blocking on this project right now because of this.

    -jonathan

  5. #5
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Me stupid :P

    mail.attributes = ... doesn't save the object so the database won't change.

    You can do:

    Code:
    def save_scheduling
      mail = Mail.find(5)
      mail.attributes = params[:mail]
      mail.save
    end
    or (better):

    Code:
    save_scheduling
      mail = Mail.find(5)
      mail.update_attributes(params[:mail])
    end
    update_attributes is mail.attributes = ... and save() combined in one method.

  6. #6
    SitePoint Enthusiast
    Join Date
    Mar 2005
    Posts
    64
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Great Thanks!!!!

    I added the hidden field like this:

    Code:
    <%= start_form_tag :action=>"save_scheduling" %>
    <%= hidden_field "mail","id",:value=>params[:id] %>
    <%= datetime_select("mail", "scheduled_at") %>
    <%= submit_tag("update") %>
    <%= end_form_tag %>
    and updated the controller to:

    Code:
     def save_scheduling
       mail=Mail.find(params[:mail][:id])
       mail.update_attributes(params[:mail])
     end
    but had a couple of ?'s.

    1. Is this only working because the scheduled_at is the same in the form as in the database (I'm assuming so because that would be quite a guess if there were multiple datetimes in the row).

    2. also, it looks like the hidden field where I passed the id needs two fields. Is that true? could i just pass an id or is there a further shortcut to passing that value since it would seem to be a rails convention.

    thanks again for the help.

  7. #7
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    1. Yes you're right. The update_attibutes() methods updates the database with a hash of values from the form fields.

    2. I don't understand what you mean by "two fields" but the Rails convention is to pass the id with GET:

    Code:
    <%= start_form_tag :action => "save_scheduling", :id => params[:id] %>
    <%= datetime_select("mail", "scheduled_at") %>
    <%= submit_tag("update") %>
    <%= end_form_tag %>
    You can, of course, pass an arbitrary id instead of params[:id].

  8. #8
    SitePoint Enthusiast
    Join Date
    Mar 2005
    Posts
    64
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok. thanks. sorry for all the ?'s but I'm trying to get up to speed asap.

    One more question. Is there an easy way to validate that a user submitted datetime is in the future? I've googled for about 15 minutes but don't see an easy sol'n.

    thanks,

    jonathan

  9. #9
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can compare dates with the normal comparison operators. So:

    Code:
    if params[:scheduled_at] < Date.today
      # ...
    else
      # ...
    end
    You can also put this code in validations in your model (this is better).
    Last edited by Fenrir2; Nov 17, 2006 at 08:50.

  10. #10
    SitePoint Enthusiast
    Join Date
    Mar 2005
    Posts
    64
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hmm....

    I'm trying to use the errors.add method but even doing the simplest thing doesn't seem to be giving me a result:

    Code:
    class Mail < ActiveRecord::Base
     protected
     def validate
        errors.add(:scheduled_at,"The date needs to be in the future" )
     end
    ultimately, I think I'll be doing this:
    Code:
    if value < Time.now
       errors.add("title", "You must pick a time in the future")
    end
    since it seems to be a Time value.

    -j

  11. #11
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Are you testing the return value of save()?
    Code:
     def save_scheduling
       @mail = Mail.find(params[:mail][:id])
       @mail.attributes = params[:mail]
    
       if @mail.save
         # valid
       else
         # invalid
       end
     end


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •