Active Admin CollectionAction route being skipped, action treated as ID - ruby-on-rails

I'm trying to create a collection_action in ActiveAdmin which allows me to rearrange the position after a drop and add. I want to be able to drag and drop the list in the index and have the new positions sent via Ajax through a post request
This is what I have so far:
ActiveAdmin.register Subscriber do
collection_action :reorder, :method => :post do
end
controller do
def reorder
#item = ResponsiveWeb.find(params[:id])
#item.position = params[:position]
#item.saved
render nothing: true
end
private
def item_params
params.require(:responsive_web).permit(:id, :position)
end
end
These are the routes:
Prefix Verb URI Pattern Controller#Action
new_admin_user_session GET /admin/login(.:format) active_admin/devise/sessions#new
admin_user_session POST /admin/login(.:format) active_admin/devise/sessions#create
destroy_admin_user_session DELETE|GET /admin/logout(.:format) active_admin/devise/sessions#destroy
admin_user_password POST /admin/password(.:format) active_admin/devise/passwords#create
new_admin_user_password GET /admin/password/new(.:format) active_admin/devise/passwords#new
edit_admin_user_password GET /admin/password/edit(.:format) active_admin/devise/passwords#edit
PATCH /admin/password(.:format) active_admin/devise/passwords#update
PUT /admin/password(.:format) active_admin/devise/passwords#update
admin_root GET /admin(.:format) admin/dashboard#index
batch_action_admin_admin_users POST /admin/admin_users/batch_action(.:format) admin/admin_users#batch_action
admin_admin_users GET /admin/admin_users(.:format) admin/admin_users#index
POST /admin/admin_users(.:format) admin/admin_users#create
new_admin_admin_user GET /admin/admin_users/new(.:format) admin/admin_users#new
edit_admin_admin_user GET /admin/admin_users/:id/edit(.:format) admin/admin_users#edit
admin_admin_user PATCH /admin/admin_users/:id(.:format) admin/admin_users#update
PUT /admin/admin_users/:id(.:format) admin/admin_users#update
DELETE /admin/admin_users/:id(.:format) admin/admin_users#destroy
admin_dashboard GET /admin/dashboard(.:format) admin/dashboard#index
sort_admin_responsive_web POST /admin/responsive_webs/:id/sort(.:format) admin/responsive_webs#sort
reorder_admin_responsive_webs POST /admin/responsive_webs/reorder(.:format) admin/responsive_webs#reorder
However when I reorder I get the 404 not found error and when I check the error logs it says, Couldn't find ResponsiveWeb with id=reorder.
Any help will be greatly appreciated!

To declare a collection action, active admin provide another dsl. Here is an example. You can try that.
collection_action :reorder, method: :post do
# your resources or whatever you need
end
One thing, active admin resource DSL provide a permit_params, so you don't need to declare it on your controller.
Hope this answer helps.
Here is the reference:
http://activeadmin.info/docs/8-custom-actions.html

Related

Active Admin : How to restrict active admin routes in production

I developed a normal (Post) like rails application that has features of posting articles with pictures then I opted for active admin in regulating the post as in (admin user), so the application doesnt have any other user expect the admin that will regulate post at backend, how do I restrict these admin routes page like if a normal web visitor attempts to visit the routes it wont be accessible ?
new_admin_user_session GET /admin/login(.:format) active_admin/devise/sessions#new
admin_user_session POST /admin/login(.:format) active_admin/devise/sessions#create
destroy_admin_user_session DELETE|GET /admin/logout(.:format) active_admin/devise/sessions#destroy
new_admin_user_password GET /admin/password/new(.:format) active_admin/devise/passwords#new
edit_admin_user_password GET /admin/password/edit(.:format) active_admin/devise/passwords#edit
admin_user_password PATCH /admin/password(.:format) active_admin/devise/passwords#update
PUT /admin/password(.:format) active_admin/devise/passwords#update
POST /admin/password(.:format) active_admin/devise/passwords#create
admin_root GET /admin(.:format) admin/dashboard#index
batch_action_admin_admin_users POST /admin/admin_users/batch_action(.:format) admin/admin_users#batch_action
admin_admin_users GET /admin/admin_users(.:format) admin/admin_users#index
POST /admin/admin_users(.:format) admin/admin_users#create
new_admin_admin_user GET /admin/admin_users/new(.:format) admin/admin_users#new
edit_admin_admin_user GET /admin/admin_users/:id/edit(.:format) admin/admin_users#edit
admin_admin_user GET /admin/admin_users/:id(.:format) admin/admin_users#show
PATCH /admin/admin_users/:id(.:format) admin/admin_users#update
PUT /admin/admin_users/:id(.:format) admin/admin_users#update
DELETE /admin/admin_users/:id(.:format) admin/admin_users#destroy
batch_action_admin_articles POST /admin/articles/batch_action(.:format) admin/articles#batch_action
admin_articles GET /admin/articles(.:format) admin/articles#index
POST /admin/articles(.:format) admin/articles#create
new_admin_article GET /admin/articles/new(.:format) admin/articles#new
edit_admin_article GET /admin/articles/:id/edit(.:format) admin/articles#edit
admin_article GET /admin/articles/:id(.:format) admin/articles#show
PATCH /admin/articles/:id(.:format) admin/articles#update
PUT /admin/articles/:id(.:format) admin/articles#update
DELETE /admin/articles/:id(.:format) admin/articles#destroy
admin_dashboard GET /admin/dashboard(.:format) admin/dashboard#index
admin_comments GET /admin/comments(.:format) admin/comments#index
POST /admin/comments(.:format) admin/comments#create
admin_c
the only way i have seen is by integrating an authorization system in AA, here is the link:
https://activeadmin.info/13-authorization-adapter.html#managing-access-to-pages

How to configure routes.rb for get access to localhost:3000/admin

I follow the tutorial of active_admin at this url: activeadmin
I tried to add it to a project that already exists followings this differents steps:
Add it to my Gemfile gem 'activeadmin', github: 'activeadmin'
Run the follow command because i already install devise rails g active_admin:install --skip-users
And rake db:migrate && rails server
My problem is that when i want to access to localhost:3000/admin i get this error Unknown Action AbstractController::ActionNotFound
Here is my routes.rb
Rails.application.routes.draw do
config = ActiveAdmin::Devise.config
config[:controllers][:sessions] = 'sessions'
config[:controllers][:registrations] = 'registrations'
config[:as] = 'admin'
devise_for :users, config
# Login / Logout routes
namespace :v1, path: '/' do
devise_scope :user do
post '/sessions', to: 'sessions#create'
post '/registrations', to: 'registrations#create'
delete '/sessions', to: 'sessions#destroy'
end
# User routes
get '/user', to: 'users#show'
patch '/user/update', to: 'users#update'
end
ActiveAdmin.routes(self)
end
I'm using rails 4.2.1 and ruby 2.2.2
UPDATE 09/06
rake routes
new_admin_user_session GET /admin/login(.:format) sessions#new
admin_user_session POST /admin/login(.:format) sessions#create
destroy_admin_user_session DELETE|GET /admin/logout(.:format) sessions#destroy
admin_user_password POST /admin/password(.:format) active_admin/devise/passwords#create
new_admin_user_password GET /admin/password/new(.:format) active_admin/devise/passwords#new
edit_admin_user_password GET /admin/password/edit(.:format) active_admin/devise/passwords#edit
PATCH /admin/password(.:format) active_admin/devise/passwords#update
PUT /admin/password(.:format) active_admin/devise/passwords#update
cancel_admin_user_registration GET /admin/cancel(.:format) registrations#cancel
admin_user_registration POST /admin(.:format) registrations#create
new_admin_user_registration GET /admin/sign_up(.:format) registrations#new
edit_admin_user_registration GET /admin/edit(.:format) registrations#edit
PATCH /admin(.:format) registrations#update
PUT /admin(.:format) registrations#update
DELETE /admin(.:format) registrations#destroy
v1_sessions POST /sessions(.:format) v1/sessions#create
v1_registrations POST /registrations(.:format) v1/registrations#create
DELETE /sessions(.:format) v1/sessions#destroy
v1_user GET /user(.:format) v1/users#show
v1_user_update PATCH /user/update(.:format) v1/users#update
admin_root GET /admin(.:format) admin/dashboard#index
admin_dashboard GET /admin/dashboard(.:format) admin/dashboard#index
batch_action_admin_admin_users POST /admin/admin_users/batch_action(.:format) admin/admin_users#batch_action
admin_admin_users GET /admin/admin_users(.:format) admin/admin_users#index
POST /admin/admin_users(.:format) admin/admin_users#create
new_admin_admin_user GET /admin/admin_users/new(.:format) admin/admin_users#new
edit_admin_admin_user GET /admin/admin_users/:id/edit(.:format) admin/admin_users#edit
admin_admin_user GET /admin/admin_users/:id(.:format) admin/admin_users#show
PATCH /admin/admin_users/:id(.:format) admin/admin_users#update
PUT /admin/admin_users/:id(.:format) admin/admin_users#update
DELETE /admin/admin_users/:id(.:format) admin/admin_users#destroy
admin_comments GET /admin/comments(.:format) admin/comments#index
POST /admin/comments(.:format) admin/comments#create
admin_comment GET /admin/comments/:id(.:format) admin/comments#show
and the log
F, [2015-06-09T10:45:02.609120 #2451] FATAL -- :
AbstractController::ActionNotFound (AbstractController::ActionNotFound):
/home/snoobie/.rvm/gems/ruby-2.2.2/bundler/gems/activeadmin-0b4b22871fd3/lib/active_admin/base_controller.rb:29:in `only_render_implemented_actions'
I hope someone can explain me what i'm doing wrong.
Thank you !
I solved it by using rails_admin, more easly to agregate to the project. If someone need more informations about the process to add rails_admin in rails project. Please let a comment and i will explain it in details.

Routing Error | No route matches [POST] "/subscriptions/create"

I'm new to Ruby on Rails and I'm working on an application that contains form used to create a new nominee ( store it in the database)
I get the problem with this instruction of the form:
<%= form_for :subscription, :url => {:controller => "subscriptions", :action => "create"} do |f| %>
this is the error:
Routing Error
No route matches [POST] "/subscriptions/create"
rake routes
Prefix Verb URI Pattern Controller#Action
subscriptions_create GET /subscriptions/create(.:format) subscriptions#create
subscriptions_index GET /subscriptions/index(.:format) subscriptions#index
articles_index GET /articles/index(.:format) articles#index
new_admin_user_session GET /admin/login(.:format) active_admin/devise/sessions#new
admin_user_session POST /admin/login(.:format) active_admin/devise/sessions#create
destroy_admin_user_session DELETE|GET /admin/logout(.:format) active_admin/devise/sessions#destroy
admin_user_password POST /admin/password(.:format) active_admin/devise/passwords#create
new_admin_user_password GET /admin/password/new(.:format) active_admin/devise/passwords#new
edit_admin_user_password GET /admin/password/edit(.:format) active_admin/devise/passwords#edit
PATCH /admin/password(.:format) active_admin/devise/passwords#update
PUT /admin/password(.:format) active_admin/devise/passwords#update
admin_root GET /admin(.:format) admin/dashboard#index
batch_action_admin_admin_users POST /admin/admin_users/batch_action(.:format) admin/admin_users#batch_action
admin_admin_users GET /admin/admin_users(.:format) admin/admin_users#index
POST /admin/admin_users(.:format) admin/admin_users#create
new_admin_admin_user GET /admin/admin_users/new(.:format) admin/admin_users#new
edit_admin_admin_user GET /admin/admin_users/:id/edit(.:format) admin/admin_users#edit
admin_admin_user GET /admin/admin_users/:id(.:format) admin/admin_users#show
PATCH /admin/admin_users/:id(.:format) admin/admin_users#update
PUT /admin/admin_users/:id(.:format) admin/admin_users#update
DELETE /admin/admin_users/:id(.:format) admin/admin_users#destroy
batch_action_admin_contacts POST /admin/contacts/batch_action(.:format) admin/contacts#batch_action
admin_contacts GET /admin/contacts(.:format) admin/contacts#index
POST /admin/contacts(.:format) admin/contacts#create
new_admin_contact GET /admin/contacts/new(.:format) admin/contacts#new
edit_admin_contact GET /admin/contacts/:id/edit(.:format) admin/contacts#edit
admin_contact GET /admin/contacts/:id(.:format) admin/contacts#show
PATCH /admin/contacts/:id(.:format) admin/contacts#update
PUT /admin/contacts/:id(.:format) admin/contacts#update
DELETE /admin/contacts/:id(.:format) admin/contacts#destroy
admin_dashboard GET /admin/dashboard(.:format) admin/dashboard#index
admin_comments GET /admin/comments(.:format) admin/comments#index
POST /admin/comments(.:format) admin/comments#create
admin_comment GET /admin/comments/:id(.:format) admin/comments#show
articles GET /articles(.:format) articles#index
POST /articles(.:format) articles#create
new_article GET /articles/new(.:format) articles#new
edit_article GET /articles/:id/edit(.:format) articles#edit
article GET /articles/:id(.:format) articles#show
PATCH /articles/:id(.:format) articles#update
PUT /articles/:id(.:format) articles#update
DELETE /articles/:id(.:format) articles#destroy
welcome_sponsor GET /welcome/sponsor(.:format) welcome#sponsor
welcome_photo GET /welcome/photo(.:format) welcome#photo
welcome_index GET /welcome/index(.:format) welcome#index
root GET / welcome#index
best regard
As you can see from the lines below:
Prefix Verb URI Pattern Controller#Action
subscriptions_create GET /subscriptions/create(.:format) subscriptions#create
you have got a route for subscriptions_create but it needs to be POST instead of GET. Simply update this route in your routes.rb file in your config folder to be a POST instead of a GET.
As you can see from this line
subscriptions_create GET /subscriptions/create(.:format), that route is only available when using GET HTTP method.
You need to change your routes.rb file for subscriptions to accept POST instead of GET. The easiest way would be to add resources :subscriptions, which will add all standard CRUD routes for resource.

Devise Edit user error prohibited this user from being saved

I keep getting the following error when i try to submit my edit user page. I am using devise and think something is wrong with my strong parameters. I have already ready through the devise docs etc but i have confused myself with this so some guidance would be most appreciated!
Thanks
class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception
before_filter :configure_permitted_parameters, if: :devise_controller?
protected
def configure_permitted_parameters
devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:name, :email, :password) }
devise_parameter_sanitizer.for(:account_update) { |u| u.permit(:name, :line1, :line2, :town, :county, :postcode)}
end
private
def after_sign_in_path_for(resource)
edit_user_registration_path(current_user) #basically whichever path you think meets your needs
end
end
These are my routes
Prefix Verb URI Pattern Controller#Action
subscriptions POST /subscriptions(.:format) subscriptions#create
new_subscription GET /subscriptions/new(.:format) subscriptions#new
subscription GET /subscriptions/:id(.:format) subscriptions#show
DELETE /subscriptions/:id(.:format) subscriptions#destroy
content_fruit GET /content/fruit(.:format) content#fruit
content_veg GET /content/veg(.:format) content#veg
content_mix GET /content/mix(.:format) content#mix
new_admin_user_session GET /admin/login(.:format) active_admin/devise/sessions#new
admin_user_session POST /admin/login(.:format) active_admin/devise/sessions#create
destroy_admin_user_session DELETE|GET /admin/logout(.:format) active_admin/devise/sessions#destroy
admin_user_password POST /admin/password(.:format) active_admin/devise/passwords#create
new_admin_user_password GET /admin/password/new(.:format) active_admin/devise/passwords#new
edit_admin_user_password GET /admin/password/edit(.:format) active_admin/devise/passwords#edit
PATCH /admin/password(.:format) active_admin/devise/passwords#update
PUT /admin/password(.:format) active_admin/devise/passwords#update
admin_root GET /admin(.:format) admin/dashboard#index
batch_action_admin_admin_users POST /admin/admin_users/batch_action(.:format) admin/admin_users#batch_action
admin_admin_users GET /admin/admin_users(.:format) admin/admin_users#index
POST /admin/admin_users(.:format) admin/admin_users#create
new_admin_admin_user GET /admin/admin_users/new(.:format) admin/admin_users#new
edit_admin_admin_user GET /admin/admin_users/:id/edit(.:format) admin/admin_users#edit
admin_admin_user GET /admin/admin_users/:id(.:format) admin/admin_users#show
PATCH /admin/admin_users/:id(.:format) admin/admin_users#update
PUT /admin/admin_users/:id(.:format) admin/admin_users#update
DELETE /admin/admin_users/:id(.:format) admin/admin_users#destroy
admin_dashboard GET /admin/dashboard(.:format) admin/dashboard#index
batch_action_admin_products POST /admin/products/batch_action(.:format) admin/products#batch_action
admin_products GET /admin/products(.:format) admin/products#index
POST /admin/products(.:format) admin/products#create
new_admin_product GET /admin/products/new(.:format) admin/products#new
edit_admin_product GET /admin/products/:id/edit(.:format) admin/products#edit
admin_product GET /admin/products/:id(.:format) admin/products#show
PATCH /admin/products/:id(.:format) admin/products#update
PUT /admin/products/:id(.:format) admin/products#update
DELETE /admin/products/:id(.:format) admin/products#destroy
admin_comments GET /admin/comments(.:format) admin/comments#index
POST /admin/comments(.:format) admin/comments#create
admin_comment GET /admin/comments/:id(.:format) admin/comments#show
admin_dashboard_index GET /admin/dashboard/index(.:format) admin/dashboard#index
new_user_session GET /users/sign_in(.:format) devise/sessions#new
user_session POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
user_password POST /users/password(.:format) devise/passwords#create
new_user_password GET /users/password/new(.:format) devise/passwords#new
edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
PATCH /users/password(.:format) devise/passwords#update
PUT /users/password(.:format) devise/passwords#update
cancel_user_registration GET /users/cancel(.:format) devise/registrations#cancel
user_registration POST /users(.:format) devise/registrations#create
new_user_registration GET /users/sign_up(.:format) devise/registrations#new
edit_user_registration GET /users/edit(.:format) devise/registrations#edit
PATCH /users(.:format) devise/registrations#update
PUT /users(.:format) devise/registrations#update
DELETE /users(.:format) devise/registrations#destroy
product_orders POST /products/:product_id/orders(.:format) orders#create
new_product_order GET /products/:product_id/orders/new(.:format) orders#new
products GET /products(.:format) products#index
POST /products(.:format) products#create
new_product GET /products/new(.:format) products#new
edit_product GET /products/:id/edit(.:format) products#edit
product GET /products/:id(.:format) products#show
PATCH /products/:id(.:format) products#update
PUT /products/:id(.:format) products#update
DELETE /products/:id(.:format) products#destroy
home_about GET /home/about(.:format) home#about
home_contact GET /home/contact(.:format) home#contact
seller GET /seller(.:format) products#seller
sales GET /sales(.:format) orders#sales
static_pages_productlanding GET /static_pages/productlanding(.:format) static_pages#productlanding
GET /content/veg(.:format) content#veg
GET /content/fruit(.:format) content#fruit
GET /content/mix(.:format) content#mix
subscriptions_new GET /subscriptions/new(.:format) subscriptions#new
root GET / static_pages#home
contacts POST /contacts(.:format) contact_us/contacts#create
new_contact GET /contacts/new(.:format) contact_us/contacts#new
contact_us GET /contact-us(.:format) contact_us/contacts#new
Your going to want to generate the devise registration controller. Below is a link with instructions on how to do that.
https://github.com/plataformatec/devise/wiki/Tool:-Generate-and-customize-controllers
You are going to throw this command into your terminal.
bash <(curl -s https://raw.githubusercontent.com/foohey/cdc/master/cdc.sh)
Once you have your registration controller generated you are going to want to put this code in it.
private
def sign_up_params
params.require(:user).permit(:name, :email, :password)
end
def account_update_params
params.require(:user).permit(:name, :line1, :line2, :town, :county, :postcode)
end
Hope this helps.

Restful url not picking up id

I am having troubles with routes. I have defined routes like this:
resource :demo
resource :subjects
resource :pages
resource :sections
when i do
rake routes
it doesn't show right urls. it shows something like
Prefix Verb URI Pattern Controller#Action
root GET / demo#index
demo POST /demo(.:format) demos#create
new_demo GET /demo/new(.:format) demos#new
edit_demo GET /demo/edit(.:format) demos#edit
GET /demo(.:format) demos#show
PATCH /demo(.:format) demos#update
PUT /demo(.:format) demos#update
DELETE /demo(.:format) demos#destroy
subjects POST /subjects(.:format) subjects#create
new_subjects GET /subjects/new(.:format) subjects#new
edit_subjects GET /subjects/edit(.:format) subjects#edit
GET /subjects(.:format) subjects#show
PATCH /subjects(.:format) subjects#update
PUT /subjects(.:format) subjects#update
DELETE /subjects(.:format) subjects#destroy
pages POST /pages(.:format) pages#create
new_pages GET /pages/new(.:format) pages#new
edit_pages GET /pages/edit(.:format) pages#edit
GET /pages(.:format) pages#show
PATCH /pages(.:format) pages#update
PUT /pages(.:format) pages#update
DELETE /pages(.:format) pages#destroy
sections POST /sections(.:format) sections#create
new_sections GET /sections/new(.:format) sections#new
edit_sections GET /sections/edit(.:format) sections#edit
GET /sections(.:format) sections#show
PATCH /sections(.:format) sections#update
PUT /sections(.:format) sections#update
DELETE /sections(.:format) sections#destroy
GET /:controller(/:action(/:id(.:format))) :controller#:action
none of the urls has :id in them. what i might be doing wrong? It still sends id to controller but i am having hard time calling index and show methods as both of them are mapped to -----#show
This is because you are using singular resource, e.g. resource :foo. When you use singular resource, you don't get the :id. In order to get the :id in the parameter, you should change the resources declarations to plural resources:
resources :demoes
resources :subjects
resources :pages
resources :sections

Resources