jbearden
jbearden

Reputation: 1869

Rails 3: Controller returns class method nil

So I got this class method:

  def self.auth(id, key_code_in)
    post = Post.where(:id => id).first   
    if post.key_code == key_code_in
      post
    else
      nil
    end   
  end

It loves the console:

1.9.3p194 :001 > authtest = Post.auth('5032f3254ff9fcf10100001b', 42745590875)
=> #<Post _id: 5032f3254ff9fcf10100001b, _type: nil, created_at: 2012-08...

But hates the controller:

  #GET posts/:id/remove_form
  def remove_form
  end

  #POST posts/:id
  def code
    @id = params[:id]
    @key = params[:destroy_code]    
    @post = Post.auth(@id, @key)

    render :text => params[@post].inspect
  end

Gets me nil, every time. I made sure the params are being passed from the view form_tag and they are. So ruling that out... what on earth is going on? Any help would be greatly appreciated by a plethora of up votes.

Upvotes: 0

Views: 331

Answers (3)

praveen k
praveen k

Reputation: 65

Try this, If your using the id properly.

def code @id = params[:id].to_i @key = params[:destroy_code]
@post = Post.auth(@id, @key)

render :text => @post.inspect

end

Upvotes: 1

niconoe
niconoe

Reputation: 772

I think I get it !

Why are you accessing params[@post] ? The result of this method should simply be in @post.

Upvotes: 1

niconoe
niconoe

Reputation: 772

Isn't the parameter a String (your comparison is expecting an Integer).

You can convert it with the to_i method.

Upvotes: 1

Related Questions