简体   繁体   中英

Non-RESTFul Rails XML api

I have a small API that I am writing in rails 3. This is not a restful application so I use a controller called api that has some methods such as:

def users
  @users = User.all
    respond_to do |format|
           format.any do
                  render :xml => @users.to_xml
            end
    end
  end
end

My routes (with comments etc removed) file looks like this:

resources :shows
resources :users

resources :comments 

devise_scope :user do
    get "/login" => "devise/sessions#new"
    get "/logout" => "devise/sessions#destroy"
    get "/register" => "devise/registrations#new"
end


root :to => 'home#index'

match ':controller(/:action(/:id(.:format)))'

When I call api/users I get the XML wrapped in HTML tags (in the body actually) but if I call api/users.xml I get a 406 error?

Do I need to change my routes to accommodate the XML call?

Thanks,

s

I added a new route to routes.rb and it worked:

match 'api/users.:format' => 'api#users', :constraints => {:format => /(xml)/}

j

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM