Implementing REST-api with different defaults format - ruby-on-rails

I have a little REST-controller named password_resets, it has only create, show and update methods.
In routes.rb:
resources :password_resets, :only => [:create, :show, :update]
and I want one of the actions can manipulate with json by default, but others not. For all actions I can do:
scope :defaults => {format: 'json'} do
resources :password_resets, :only => [:create, :show, :update]
end
but how to do the same only for one action?

you can use multiple lines to setup differents formats:
resources :password_resets, :only => [:create, :update]
resources :password_resets, :only => [:show], :defaults => { :format => 'json' }

Related

uninitialized constant UsersController

I am using devise and therefore do not need a users controller.However, i also need nested routes and my config.routes looks like this;
devise_for :admin_users, ActiveAdmin::Devise.config
ActiveAdmin.routes(self)
devise_for :users
resources :users do
resources :personal_accounts,path: "user_account", only: [:show] do
resources :deposits, only: [:new, :show, :create, :index]
resources :withdraws, only: [:new, :show, :create, :index]
end
resources :businesses do
resources :business_accounts, path: "business_account", only: [:show] do
resources :business_withdraws, only: [:new, :show, :create, :index]
resources :business_deposits, only: [:new, :show, :create, :index]
end
end
end
How can i go past this error while also maintaining my nested routes.
Thank you.
You have three levels of nested routes there, which is normally considered to be undesirable: http://edgeguides.rubyonrails.org/routing.html#nested-resources
Resources should never be nested more than 1 level deep.
This bit resources :users do will create all the named routes for the users controller, which I suspect is where your error comes from. Why do you need this? Better perhaps to specify the routes without it?
resources :personal_accounts,path: "user_account", only: [:show] do
resources :deposits, only: [:new, :show, :create, :index]
resources :withdraws, only: [:new, :show, :create, :index]
end

Refactor nested routes for common default

I have some routes for an API which all have the same defaults (format: :json):
namespace :api do
namespace :v1 do
resources :users, only: [:index, :show, :update], defaults: { format: :json }
resources :items, only: [:index, :show, :update, :destroy], defaults: { format: :json }
resources :posts, only: [:index, :show, :update], defaults: { format: :json }
resources :comments, only: [:index, :show, :update], defaults: { format: :json }
resources :flags, only: [:index, :show, :update, :create], defaults: { format: :json }
end
end
Is there a way to refactor/DRY the code to set the defaults (or even the only) in just one place for only this set of routes? The app also serves HTML at other routes, so it can't be a blanket setting for the whole app.
Move defaults: {format: :json} and the common only options at namespace level. Namespace have them as an option.
namespace :api, defaults: { format: :json }, only: [:index, :show, :update] do
namespace :v1 do
resources :users
resources :items, only: [:index, :show, :update, :destroy]
resources :posts
resources :comments
resources :flags, only: [:index, :show, :update, :create]
end
end
You can use iterator like the following:
[:users, :items, :posts, :comments, :flags].each do |res|
resources res, only:[...], defaults: {}
end
but I see you have different only so you can also pass it to iterator

Getting ActionController::RoutingError uninitialized constant while trying to use namespace in Rails routes

I have two controllers dashboard and posts. Im trying to put the post's :new, :create, :destroy, :edit, :update actions within dashboard url like this dashboard/posts/new. But the im talking to the new action in the posts controller. Not in the dash controller. Here's my routes.rb file :
resources :posts, :except => [:new, :create, :destroy, :edit, :update]
get 'dashboard', to: 'dash#show'
namespace :dashboard do
resources :posts, :only => [:new, :create, :destroy, :edit, :update]
end
get 'dashboard/posts', to: 'dash#posts'
The two controllers in question are : dash and posts
Now when I try to visit http://localhost:3000/dashboard/posts/new it says :
ActionController::RoutingError at /dashboard/posts/new
uninitialized constant Dashboard
How to fix this?
Assuming that you have two controllers DashController and PostsController.
And you want to access few of the posts routes within the scope of namespace then you can define the routes as below:
resources :posts, :only => [:index, :show]
get 'dashboard', to: 'dash#show'
scope :dashboard do
resources :posts, controller: :posts ,:only => [:new, :create, :destroy, :edit, :update]
end
get 'dashboard/posts', to: 'dash#posts'
The constant "Dashboard" can't be found. I think the problem is that you sometimes use dash and sometimes use dashboard.

Rails Adding New Page to Resource (with parameter)

I am trying to add a few new pages to a rails resource that I am creating.
What I am doing in my routes file is as follows:
resources :users, :only => [:index, :show] do
collection do
get :show_subpage1
end
end
When I look at my routes, show_subpage1 shows up, but not in the format I want. What shows up in the routes is:
show_subpage1_users GET /users/show_subpage1(.:format)
When what I WANT the route to be is:
show_subpage1_users GET /users/:id/show_subpage1(.:format)
(with the ID).
How would I go about doing that in rails?
To get:
show_subpage1_users GET /users/:id/show_subpage1(.:format)
do not define :show_subpage1 as a collection route:
resources :users, :only => [:index, :show] do
get :show_subpage1
end
or you could define it as a member route as follows:
resources :users, :only => [:index, :show] do
member do
get :show_subpage1
end
end
Also, I'm unsure why you have :only => [:index, :show] defined if you are also going to have a member route :show_subpage1. I assume you do want to add add :show_subpage1 to the only array, i.e. resources :users, :only => [:index, :show, :show_subpage1] do.
Please take a read on "Adding More RESTful Actions"
there are two ways with resources member or collection
resources :users, :only => [ :index, :show ] do
# /users/:id/profile
get 'profile', :on => :member
# /users/profile
get 'profile', :on => :collection
end
hope this helps

Rails 3.0 member routing problem

I have a resource defined in my routes file as follows:
resources :accounts, :only => [:show, :new, :edit, :create, :update], :member => {
:profile_avatar => :get
}
In turn, in my accounts#show view I have the following code:
<%= image_tag(profile_avatar_account_path(#account, :jpg), :alt => "#{#account.username}", :title => "#{#account.username}") %>
When pulling up the page I get the following error in my production log:
ActionView::Template::Error (undefined method `profile_avatar_account_path' for #<#<Class:0x7f3fdb166260>:0x7f3fdb7bc4e8>):
Does rails 3.0 not support member anymore or is there a different way of doing this?
Thank you,
Brian
It should be
resources :accounts, :only => [:show, :new, :edit, :create, :update] do
member do
get 'profile_avatar'
end
# or
get 'profile_avatar', :on => :member
end
Try:
resources :accounts, :only => [:show, :new, :edit, :create, :update] do
get => 'profile_avatar', :on => :member
end
resources :accounts, :only => [:show, :new, :edit, :create, :update] do
member do
get :profile_avatar
end
end

Resources