Rails 3 API with RABL - ruby-on-rails-3

I am trying to use RABL to create an API - but my setup is a bit different than the standard setup - as defined in their WIKI.
I have setup an API namespace in my routes:
namespace :api do
resources :orders
end
I have my controller within /app/controllers/api/orders_controller.rb
and my RABL view within /app/views/api/orders/index.json.rabl:
When I try to visit localhost:3000/api/orders, I get the following error:
Template is missing
Missing template api/orders/index with {:handlers=>[:erb, :rjs, :builder, :rhtml, :rxml, :haml, :rabl], :formats=>[:html], :locale=>[:en, :en]} in view paths
However, If I create a new file called '/app/views/api/orders/index.html.erb' The view renders - but doesn't use RABL.
How can I make it so that it uses RABL?
Thanks

It is looking for the "html" format. Try adding .json extension to the URL or changing your routes to this.
namespace :api, defaults: {format: 'json'} do
resources :orders
end

Related

Trouble with ruby on rails routes with two word controller name

I wanted to create a controller called Database Importer.
Here's what I ran:
rails generate controller database_importer index
Which generated:
class DatabaseImporterController < ApplicationController
def index
end
def import
# to do.
# Receive the uploaded CSV file and import to the database.
CSV.foreach("parse.csv") do |row|
end
end
end
When I visit http://0.0.0.0:3000/database_importer/index I get:
Routing Error
No route matches {:action=>"import", :controller=>"database_importer"}
Try running rake routes for more information on available routes.
Here is my relevant route:
get 'database_importer/index'
Why is the routing engine trying to push me towards the import action method?
When I try to change the route to reflect the actual controller name (it doesn't have the underscore), I get:
get 'databaseimporter/index'
Routing Error
uninitialized constant DatabaseimporterController
Try running rake routes for more information on available routes.
Do you also have a route defined like:
post 'database_importer/import'
I think the form action is what is failing when Rails is trying to generate the route for the form.

How do I make ActsAsTaggableOn::Tag to be 'acts_as_indexed'?

I have a rails 3 app where I am using acts_as_taggable_on to tag a Post model. I have a search box which allows me to search through the posts, where I use acts_as_indexed. In my search, I want to also be able to search through all the tags, by extending the ActsAsTaggableOn::Tag class to support acts_as_indexed. How do I do that?
I tried the following:
# lib/tag.rb
module ActsAsTaggableOn
class Tag
acts_as_indexed :fields => [:name]
end
end
but then I get the following error when I try to call find_with_index:
> ActsAsTaggableOn::Tag.find_with_index('foo')
undefined method `find_with_index' for ActsAsTaggableOn::Tag(id: integer, name: string):Class
UPDATE: I made this work using acts_as_taggable_on's built in finders (specifically, Tag#named_like), but I would prefer to use acts_as_indexed, since I use it with all the other models in my app, and I am having to special case tag searching due to this issue.
Thanks,
It might be easier to extend the act_as_indexed declaration on the Post model so that tags related to each post are included in a search.
class Post
acts_as_taggable_on
acts_as_indexed :fields => [ <existing tag list> , :tag_list]

What is the best way to translate slugs in Rails routes

I'm trying to achieve full internationalization of my routes in a Rails3.1 app. I'm already using Francesc Pla's rails-translate-routes to localize route actions and resources. The last step is to be able to translate slugs for some of my models.
Route to be translated:
http://myhost.com/products/pharmacy --> http://myhost.com/productos/farmacia
I have a nested route of the form
# routes.rb
match 'products/:category_slug' => "products#index"
I have a model Category with an instance #<Category id: 1, slug: "pharmacy"> and I do find_by_slug category in my ProductsController.
Any ideas on how to do translate the :category_slug part of the route?
As far as I'm aware, you can call translation helpers directly from your controller as long as you namespace correctly with I18n.
So your ProductsController could contain something like the following:
class ProductsController < ApplicationController
def index
i18n_slug = I18n.t("locale.category.#{params[:category_slug]}")
#category = Category.find_by_slug(i18n_slug)
end
end
You should probably inform yourself as to potential security risks of passing the params directly into the translation engine, though I'm not aware of any. You might also consider
moving that into a before filter or into the application controller if it will be used in multiple controller actions.

Trouble in Rails 3 Adding Restful Routes to Namespaced Resources

I am trying to add a route to a controller that has been set as a resource in the 'admin' namespace like this:
namespace :admin do
resources :books do
collection do
post :process_new
end
end
end
I have added an action int the Admin::BooksController for process_new, but whenever I try to access this action using the url: .../admin/books/process_new I get the following error:
Couldn't find Book with ID=process_new
It looks like it's routing to the show action and trying to use process_new as the id. Can someone shed some light on what I may be doing wrong?
**Edit:
I changed my redirects to use the helper functions and it seems to be working.
Add get :process_new to your resources :books routes:
namespace :admin do
resources :books do
collection do
get :process_new
post :process_new
end
end
end

respond_with and namespaced controller

I'm trying to make use of rails 3's respond_to/respond_with mechanism for restful controllers. I'm finding that when I try and use those features within a namespaced controller the redirects fail and i have to specify the optional location: parameter on the respond_with.
So right now I have:
def Admin::FooController
respond_to :html, :xml
def create
#foo = Foo.new(params[:foo])
#foo.save
respond_with(#foo, location: admin_foo_path(#foo))
end
end
If I don't provide the location parameter it attempts to redirect to just foo_path(#foo)
Any ideas as to why this would be / if I'm doing something wrong or does rails 3's default responder just not handle namespaced URL routes?
respond_with(:admin, #foo)
This blog has more..