RoR POST请求导致路由错误

时间:2013-03-10 21:40:24

标签: ruby-on-rails ruby ruby-on-rails-3

我正在尝试一个基本的API,但我处于一个十字路口,我不太确定如何继续。一般的要点是我为/authenticate.json提供了用户名和密码,并作为回报接收了访问令牌。

如果我发出GET请求,我会得到我需要的信息。如果我尝试通过POST执行它会抛出错误:

No route matches {:action=>"show", :controller=>"users", :id=>#<User access_token: "8922a3718bbe2a441a5ddddece3053a647941863b5ff2cad007...">}

的routes.rb

match '/authenticate' => 'user_functions#authenticate', :via => [:post, :get]

控制器中的身份验证定义看起来像这样(我知道它很狡猾,我对RoR / Ruby不太熟悉)

def authenticate
if params['email'] and params['password']
  userSearch = User.find(:all, :conditions => ['email = ?', params[:email]], :limit => 1)
  if userSearch.count == 1 
    hash = Digest::SHA2.new << "#{userSearch.first.password_salt}#{params['password']}"
    if hash.hexdigest == userSearch.first.password_hash
      access_array = {"access_token" => userSearch.first.access_token}
      userAccessDetails = User.find(userSearch.first.id, :select => "access_token")
      respond_with userAccessDetails
    else
      respond_with 'Invalid credentials'
    end
  else
    respond_with 'Failed'
  end
else
  respond_with 'No credentials provided'
end

如果有人能够帮助或指出我正确的方向,我会很感激。谢谢。

1 个答案:

答案 0 :(得分:0)

问题是你传递的用户对象没有id。在您的查询中,您只选择访问令牌。

尝试同时选择ID:

userAccessDetails = User.find(userSearch.first.id,:select =&gt;“access_token,id”)