0

I'm building JSON api controllers.

my routes.rb has:

namespace :api do
  resources :users
end

controllers/api/users.rb:

respond_to :json
def create
  @user = User.create(params[:user])
  respond_with(@user)
end

when posting to api/users.json a new user is created but I'm getting an exception saying user_url method is missing. if i add: resources :users to routes.rb everything is fine. what's happening? any other way to solve this ?

1 Answer 1

1

I think respond_with(@user) will redirect to user url but there is no users path declared outside namespace :api, so it alerts that error.

Could you try this?

respond_with(@user, :location => your_path_that_will_be_redirected)
Sign up to request clarification or add additional context in comments.

1 Comment

ok, I found out that I should use render :json for what I'm trying to do.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.