Rails active_admin controller - ruby-on-rails

How can i set routes for controller defined in active_admin gem.
In active_admin, links.rb i have:
controller do
def admin_links
//some code
end
end
and then i try to do routes for it:
scope '/admin_links' do
put...??
end
But i just dont know what controller should i call. Can anybody help?

controller do is meant to overwrite existing actions.
If you want to add actions, use either member_action or collection_action.
Doc is here.
since you use PUT verb, you'll have to do:
member_action :admin_links, method: 'put' do
# your action code here
end

Related

Ruby on Rails - custom REST action and routing

I made custom REST action called makedefault:
class PicturesController < ApplicationController
def makedefault
...
end
end
But I get No route matches [POST] "/pictures/12" error.
In routes I've tried:
get "/pictures/:id" => "pictures#show"
But it didn't help. How can I make my own REST action and route it properly?
Since you want to create a POST route (this is what I understand from the question), you should try this:
post "/pictures/:id" => "pictures#makedefault"
The second argument after # must be pointed to your custom action.

Skip authentication for specific member action in ActiveAdmin

I'm trying to skip the authentication for a custom ActiveAdmin member action that I've created. Here's what I've been trying, but it still brings me to the login page.
ActiveAdmin.register Foo, as: "Foos" do
controller do
skip_before_action :authenticate_admin_user!, only: :bar
end
member_action :bar, method: :get do
# render something
end
end
Versions:
Rails: 4.1.1
ActiveAdmin: 1.0.0.pre1
Have a look at this gist.
Put the self.filters and self.before_filters methods into controller do block.
Add binding.pry after those methods and restart application server - this should stops on binding.
Type filters(:before) and you will see a list of callbacks.
Find the callback responsible for authentication, e.g. authenticate_active_admin_user.
Add skip_before_filter :authenticate_active_admin_user to controller do block.
Have a break :)

How to add a new method to ProductsController in spree

I want to add a method called quick_view, which is basically a show method but much simpler and meant to be served with Ajax on hover on a product.
How can i achieve this ? How can i open the ProductsController of spree and add before_filters and also specify the appropriate routes..
Thanks !
Doco for that is at https://guides.spreecommerce.com/developer/logic.html
Create a class called app/controllers/products_controller_decorator.rb, and put something like this in it:
Spree::ProductsController.class_eval do
before_filter :my_filter, :only => :quick_view
def quick_view
# your code goes here
end
private
def my_filter
# code for your before filter goes here
end
end
As for the routes, you'll be able to add it in your routes.rb file just like any other route, but you'll need to specify that it's a spree route:
Spree::Core::Engine.routes.draw do
# Your route goes inside this block
end

How to DRY up controller code which involves views code as well?

In my Rails 3 app, I have a number of models with a boolean field disabled. In controllers for these models, I use a custom action disable to toggle the disabled field using Ajax.
Example (For client),
# routes.rb
resources :clients do
member do
get :toggle_disable, to: 'clients#disable', as: :disable
end
end
# clients_controller.rb
def disable
#client = Client.find(params[:id])
#client.update_attribute :disabled, !#client.disabled
render 'clients/update_client', format: :js
end
# update_client.js.erb
$('#client-<%= #client.id %>-details').html("<%= escape_javascript(render 'clients/client', client: #client) %>");
I have this code for at least ten resources in my application.
QUESTION
How do I go about DRYing up this code and add actions for these boolean fields dynamically? I could have gone with creating a parent controller or module but I am not sure how will I take care of the views code.
I should be able to do something like this
#clients_controller.rb
class ClientsController < ApplicationController
add_toggle_action :disable
end
Two main ways to share methods:
inheritance: define your action in ApplicationController
mixins: add your method in a module and include the module in the appropriate controllers
Since you want only some controllers to get the method, I'll head towards mixin.
Your controller action must use a view with a full path, not relative, something like:
render '/shared/clien/update', format: :js
Lastly, you'll have to define all routes.

undefined method current_user: Cancan and Active Scaffold

I'm trying to add an action link to an active scaffold controller using
if current_user.can? :update, Post
config.action_links.add 'export', :label => 'Export', :page => true
end
but whatever controller I try to use, I always get undefined method current_user
So, how can I check if the logged user can/cannot do something?
I've even tried with
def ability
#ability ||= Ability.new(self)
end
delegate :can?, :cannot?, :to => :ability
as suggested here, but without success.
What's wrong with this?
Note that in the normal case ActiveScaffold config is done in the class not the instance so there is no current_user set because there is no request yet.
current_user is a typically defined method, but it is one that you must add. it is not supplied by rails. So, you need a current_user method in your ApplicationController. There are tons of examples but I would look at the authlogic example app on github

Resources