SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    Jan 2008
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Not equal to conditional

    Can anyone help with this. I have an edit form which works perfectly.

    However I am trying to add one final little bit. This will get the user_id from the cookie and then compare it to the user_id from a query. If the two are not equal to each other then I need to redirect them.

    However I can't the conditional part to work. Anyone able to point me right?

    Code:
    def edit_article
        if request.post?
          @article = Article.find(params[:id])
          if @article.update_attributes(params[:article])
            redirect_to :action => 'index'
          else
            render :action => 'edit_article'
          end
        else
          @user_id = cookies[:user_id]
          @article = Article.find(params[:id])
          if @article.user_id != @user_id
            render :text => "This is not your article. Unable to edit."
          end
        end
      end

  2. #2
    SitePoint Enthusiast
    Join Date
    Jan 2008
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Managed to get help elsewhere in the end. Figured I'd put the solution up here. In the end it was something very simple. The user_id from the cookie needed to be an integer to match up. My code now read as like the following.

    Code:
    def edit_article
       if request.post?
          @article = Article.find(params[:id])
          if @article.update_attributes(params[:article])
             redirect_to :action => 'index'
          else
             render :action => 'edit_article'
          end
       else
          @user_id = cookies[:user_id]
          @article = Article.find(params[:id])
          if @article.user_id != @user_id.to_i
             render :text => "This is not your article. Unable to edit."
          end
        end
    end

  3. #3
    SitePoint Addict
    Join Date
    Feb 2007
    Posts
    270
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry I didn't get here sooner. Yes, anything that comes from a cookie is a string, and strings don't compare with integers (the id column of a table is always an int).

  4. #4
    SitePoint Guru downtroden's Avatar
    Join Date
    Dec 2004
    Location
    illinois
    Posts
    971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    just out of curiosity…*why are you even setting the @user_id variable? Just seems like a bit of extra work to me.

    why not just do
    Code Ruby:
    def edit_article
        if request.post?
          @article = Article.find(params[:id])
          if @article.update_attributes(params[:article])
            redirect_to :action => 'index'
          else
            render :action => 'edit_article'
          end
        else
          @article = Article.find(params[:id])
          if @article.user_id != cookies[:user_id].to_i
            render :text => "This is not your article. Unable to edit."
          end
        end
      end
    your brain reacts in the same way whether you are
    looking at something or thinking about it...

  5. #5
    SitePoint Member ryan0124's Avatar
    Join Date
    Mar 2007
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    not quite sure
    06.org, Corrected.com, FlipHome.com, Skirt.info
    www.TakeDomain.com
    Best Youtube Video Downloader


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
  •