我正在构建JSONAPI控制器。
我的路线.rb有:
namespace :api do
resources :users
end
controllers/api/users.rb:
respond_to :json
def create
@user = User.create(params[:user])
respond_with(@user)
end
当发布到api/users.json时,会创建一个新用户,但我收到一个异常,说缺少user_url方法。如果我添加:resources:userstoroutes.rb,一切都很好。发生了什么事?有其他办法解决这个问题吗?
我认为responsd_with(@user)将重定向到用户url,但在namespace:api之外没有声明用户路径,因此它会提醒该错误。
你能试试这个吗?
respond_with(@user, :location => your_path_that_will_be_redirected)