SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast
    Join Date
    Oct 2005
    Posts
    31
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    update_attributes does not work

    I have problems using the update_attributes function in my controller.
    Sometimes works and sometimes doesn't. I can always make it work if I
    put a breakpoint just before the update_attributes function. What does
    that mean?. Why is it doing this?.
    Please help. If I don't get this working I'll quit Rails for good, it's
    caused me a lot of trouble, I'm starting not to like it.
    Thanks.

    Here is my code:

    Code:
      def update
        @persona = Persona.find(params[:id])
        @persona.fecha_modificacion = Time.now
        @persona.modificacion_user_id = 1
    
        if @persona.fecha_creacion? == nil
          @persona.fecha_creacion = @persona.fecha_modificacion
          @persona.creacion_user_id = @persona.modificacion_user_id
          @persona.eliminado = 0
        end
    
        #breakpoint
        if @persona.update_attributes(params[:persona])
          flash[:notice] = 'Persona was successfully updated.'
          redirect_to :action => 'ver', :id => @persona
        else
          render :action => 'list'
        end
      end

  2. #2
    SitePoint Guru Majglow's Avatar
    Join Date
    Aug 1999
    Location
    B-Town
    Posts
    645
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Your post is a bit vague, what does "sometimes it works sometimes it doesn't" mean? Does validations fail sometimes, does it claim to have worked and doesn't? Check your development.log file and see what happened in detail with SQL queries and maybe post the entire request here.
    Ohai!

  3. #3
    SitePoint Guru
    Join Date
    Jan 2001
    Location
    Alkmaar, Netherlands
    Posts
    710
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    where do you show validation errors?

    It sounds like sometimes your persona object does not validate because of values coming from form and it just does NOT save.

    I dont know where you show your persona form but if it fails you should render that action (actually I would make one function for showing and updating object) and in that page just use

    <%= error_messages_for("persona") %>

  4. #4
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Please do abandon Rails: less competition.

    Your code looks good but one thing is strange: why do you redirect to the list action if update_attributes fails?

    You need to give more code/information before we can find the error.

    Also, why do you use spanish identifiers? If you'd used updated_at instead of fecha_modificacion you could have dropped this line:

    Code:
    @persona.fecha_modificacion = Time.now
    I don't understand why you have this code:

    Code:
    if @persona.fecha_creacion? == nil
      @persona.fecha_creacion = @persona.fecha_modificacion
      @persona.creacion_user_id = @persona.modificacion_user_id
      @persona.eliminado = 0
    end
    I think it belongs in a separate create action?


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
  •