Active Admin Rename the resource in different places - ruby-on-rails-3

The documentation allows you to rename a page pretty easily, but not exactly in the way I'd like.
Given this code: ActiveAdmin.register User, :as => "Static" gives a menu name of statics
I was wondering if there was a way to customize it so the name in the menu is not pluralized => static

Rename the menu item: menu :label => "Static"
Rename the title in the top left corner: index :title => "Static"
Didn't really see them when looking through the documentation, but saw them being used in other questions/code

Related

passing params through a link_to rails 3

Im just looking for some clarification on the following piece of code, well part of it.To give some background i have an app where you can upload recipes, search recipes and save them as favourites, this piece of code is in a controller "recipes", action is "my_recipes"
<%= link_to "Add to favorites", {:controller => 'favourites', :action => 'create', :recipe_id => recipe.id}, {:method => :post } %>
My understanding is that this creates a link_to (anchor tag if you will) that makes a post request through the create method within the favourites controller. This part I think i underdstand (corrections welcome), the part i am unsure of is
:recipe_id => recipe.id}
I know this is passing the recipe_id for example but I would like to know why we do this? and what relevance of the : before the first recipe_id.May seem obvious to some but you dont know until you learn.
Any help appreciated
Is this code in a partial? Is recipe being passed along? You should rewrite as so:
link_to "Add to favorites", new_favourite_path(recipe), method: :post
Do rake routes in your console and find out what the path is for creating favourites, then replace 'new_favourite' with that above. Note, the route might be identified with something more explicit like new_favourite_recipe.
To answer you question, you must pass recipe, or recipe.id because otherwise the controller wouldn't know which recipe to add to the favourites. You don't need to specify the user as that should be accessed directly from within the controller action using something like current_user.

Conditionally change resource name Activeadmin

I'd like to conditionally change the label of a resource in ActiveAdmin based on the user role. I'd like to keep the default pluralization of the name for admins, but for regular users, I need to change the name.
I.e. I have a resource Users, which I would like to change to My Account for the regular user (since they won't see #index).
I'm trying something like
ActiveAdmin.register User do
menu :if => proc {if !current_user.admin?
menu :label => "My Account"
else
menu :label => "I hate Users"
end}
Anyone know how to conditionally name the resource?
Thanks!
This works:
menu :label => proc { true ? "I Hate Users" : "My Account" }
But then to access the
current_admin_user
or the
current_user
object from within the proc won't work. I haven't found a way to get the logged in user object from within the ActiveAdmin::MenuBuilder scope. There are suggestions, see e.g. https://stackoverflow.com/a/2513456/790737 where you set a variable in
Thread.current
after succesfull login. I guess you will have to hook in to the post-authentication work of devise. Good luck.

Questions about rails3 routes

I'm upgrading my app to rails 3, and I am a bit confused about some of the routes. The resourceful ones are easy enough, but how can I set a generic rule for all actions in a specific controller. I tried something like this:
get 'custom/:action/' => {:controller => :custom}
But that didn't work. It seems the new format is "controller#action", but how can I specify the action to be variable?
Also, other than using named routes or resources, is it possible to do shorthand notation to name routes in a specific controller?
i.e. rather than:
get '/tasks', :controller => :home, :action => :tasks, :as => 'tasks_home'
get '/accounts', :controller => :home, :action => :accounts, :as => 'accounts_home'
is it possible to do something a little cleaner, like:
controller => :home do
get :tasks
get :accounts
end
And that would automatically created the named routes?
You can use action as a variable like this:
resource :custom do
match ':action'
end
This will generate
/custom/:action(.:format) customs#:action
custom POST /custom(.:format) customs#create
new_custom GET /custom/new(.:format) customs#new
edit_custom GET /custom/edit(.:format) customs#edit
GET /custom(.:format) customs#show
PUT /custom(.:format) customs#update
DELETE /custom(.:format) customs#destroy
So it will handle your action as a variable URL-s and will add some default CRUD actions as well.
Note that the controller name here is in plural. If you would like to use a route for a controller which name is in singular, use resources instead of resource.
The answer to the second question is almost identical to the first one, use resource:
resource :home do
get :tasks
get :accounts
end
generates:
tasks_home GET /home/tasks(.:format) homes#tasks
accounts_home GET /home/accounts(.:format) homes#accounts
home POST /home(.:format) homes#create
new_home GET /home/new(.:format) homes#new
edit_home GET /home/edit(.:format) homes#edit
GET /home(.:format) homes#show
PUT /home(.:format) homes#update
DELETE /home(.:format) homes#destroy
Note that the matched controller names are in plural again, because of the convention.
Looks like this is related to the persisted field being set to false on nested ActiveResource objects: https://github.com/rails/rails/pull/3107

Activeadmin disabling the "new resource" method

I'm using Activeadmin for the admin interface on an app I'm working on (loving it) and I am curious if there is a way to disable the "New Resource" link in the upper-right corner of the resource show page?
The particular resource I'm using is nested inside another resource and I have a partial that allows it to be created from the show page on that parent resource.
I have disabled the resource in the menu, but I'd rather leave the resource in the menu so I can see/edit/delete those resources without having to find it by looking through its parent resource.
Previous solution didn`t work for me, so here is general solutions, that works always:
ActiveAdmin.register Book do
actions :index
#or like that
#actions :all, :except => [:destroy]
index do
column :title
column :author
end
end
Try config.clear_action_items! to remove the link to New and other links on top of the table
This removed the "New Resource" button from the top-right:
config.clear_action_items!
This removed both the "New Resource" button as well as the box "There are no resources yet - create one".
actions :all, :except => [:new]
Thank you, Irio
config.clear_action_items!
Will remove all the actions.
If you only want to remove the new action link you can also use:
config.remove_action_item(:new)
I know this is an old question, but I just came up to it (had the same problem), and realized that config.clear_action_items! and actions :all, :except => [:new] are fundamentally different.
config.clear_action_items! will remove the New button from the index page, while actions :all, :except => [:new] will remove both the button, AND the route, meaning you can't call it from another place (which, in my case, is needed).
I did this:
controller do
def action_methods
if some_condition
super
else
super - ['new', 'create', 'destroy']
end
end
end
To disable some of the possible actions. action_methods returns an array of the 7 standard CRUD actions, so you can subtract those you don’t want
Or even:
ActiveAdmin.register Purchase do
config.clear_action_items!
actions :index
end
Worked for me too ! :-)
ActiveAdmin.register AssetSumView do
menu :label => "Asset Summary View", :parent => "Things"
# no button for NEW (since this is a db view)
#---------------------------------------------------------------------------------------------
config.clear_action_items!
enter code here
action_item do
link_to "Assets" , "/admin/assets"
end
action_item do
link_to "AssetCatgCodes", "/admin/asset_catg_codes"
end
#---------------------------------------------------------------------------------------------
config.clear_action_items! does only half of the job. There is one issue though.
In case of empty index table, active admin show this message
There are no [Resources] yet. Create one
which doesn't get hidden by the above command and I don't want to entirely disable the action. So, I kept the link and edited the new action to redirect to the parent resource index with a message.
controller do
def new
if params[:parent_id].present?
super
else
redirect_to parent_resources_path, notice: "Create Resource through ParentResource"
end
end
end

Beginner struggling with update_attribute command

I am in the process of trying to use the update_attribute command, but struggling to get it working (at all) and hoped someone could point me in the right direction?
I have previously posted a question about this issue, it was very useful in terms of giving a feel for the mechanics of what is going on, but unfortunately it didn't actually get it working.
I have a database of items (Items), which among other things contains ':item_name', ':click_count' and ':external_url'.
Currently I have a view (Showselecteditems) in which there is a list of all the items, when a user clicks on an item name, they are directed to the appropriate external url. This works using the code:
<%= link_to selecteditem.item_name.to_s, selecteditem.external_url %>
I would now like to add the ability to count the number of times a particular item name has been clicked on (i.e. in total for all users, not individual users) and therefore the number of times each external url has been visited in order to work out which is most popular.
Reading around, I believe i need to modify the code above to something of the form:
<%= link_to selecteditem.item_name.to_s, selecteditem.external_url, {:controller => params[:controller], :action => clickcountplusone, :identifier => selecteditem.item_name} %>
And need to define this function somewhere - it seems to only be found if located in 'application_helper'?
def clickcountplusone
clickeditem = Items.find(params[:identifier])
clickeditem.update_attribute(:click_count, clickeditem.click_count + 1)
rescue ActiveRecord::RecordNotFound # to avoid error if no identifier value
end
Needless to say, I cannot get this to work... My question is therefore, how can I set things up correctly so that when the link is clicked on the count is incremented? The other common problem people seem to report is that the number will be incremented each time the page is refreshed, which I would like to avod if possible.
Previously people have suggested adding to the 'show' section of the 'Items' controller, however, i don't know how this would work as the links are being clicked on the Showselecteditems view page, not the database itself where you get the show, edit, destroy commands. Any advice greatly appreciated.
This
<%= link_to selecteditem.item_name.to_s, selecteditem.external_url, {:controller => params[:controller], :action => clickcountplusone, :identifier => selecteditem.item_name} %>
will not point user to the some_controller#clickcountplusone, because you already specified an external link.
The easiest way to do this job is to modify your link_to like:
<%= link_to selecteditem.item_name.to_s, {:controller => params[:controller], :action => clickcountplusone, :identifier => selecteditem.item_name} %>
And then to modify your actions source:
def clickcountplusone
clickeditem = Items.find(params[:identifier])
redirect_to clickeditem.external_url if clickeditem.update_attribute(:click_count, clickeditem.click_count + 1)
rescue ActiveRecord::RecordNotFound # to avoid error if no identifier value
end