Here's where I'd like to add a new button:
I'd like to add a new button on the upper right, pointing to a custom URL based on some properties in the user the page is referring to.
I couldn't find the right options here.
They call them action items:
action_item :import_demo, only: :show do
link_to 'Import Demo', '#'
end
Related
The index page for an ActiveAdmin resource by default contains a button to go to the form to create a new instance of that object
I have a view with is a composite list of multiple times of items where a new button does not make sense. Is there a way to remove that button on only that resource?
ActiveAdmin.register Action do
actions :all, except: :new
end
Something like that. Like that you can disable any action you desire
The accepted answer is not the exact solution because it also removes the new action and the new route.
If you are looking just to remove the button (and nothing more) then you should do this :
# item is an ActiveAdmin::ActionItem
config.action_items.delete_if {|item| item.name == :edit && item.display_on?(:show) }
Here i'm not displaying the edit button on the show page of my resource
In my ActiveAdmin dashboard, I have two resources - Posts and ScrapedPosts. For a ScrapedPost, I want to add a Publish Post button, which, when clicked, goes to a page for creating a new Post (new action), where all the fields for Post are pre-filled with values from ScrapedPost (Post and ScrapedPost have identical schema).
Here is my current code inside app/admin/scraped_post.rb
ActiveAdmin.register ScrapedPost do
action_item :view, only: :show do
link_to "Publish Post", new_admin_post_path
end
end
This adds a new button called Publish Post. When I click on it, as expected, it creates a page for adding a new Post. However, I want the fields pre-filled with the values from the current ScrapedPost from where I am creating the post.
I have tried a number of things so far. The documentation for action_item is very basic and it didn't help. It looks like I can't pass parameters to the action identified by new_admin_post_path. How can I do it?
[I started learning RoR just this week, so I'll be grateful if you can explain your solution as well.]
All action_item is doing is injecting a link styled as a button onto the show page. You can pass parameters to the link target by embedding them in the link. However, new does not accept parameters so to copy a Post what you need is a member_action:
member_action :clone do
resource.clone
render :new
end
This action can be invoked so:
action_item :clone, only: :show do
link_to "Publish Post", clone_admin_post_path
end
Make sure first that works for copying a Post. If you want to create a ScrapedPost from a Post then you will need to pass the id of the Post as a parameter:
link_to "Publish Post", clone_admin_scraped_post_path(post_id: resource.id)
then the ScrapedPost member action can find it:
member_action :clone do
#scraped_post = Post.find(params[:post_id]).clone.becomes(ScrapedPost)
render :new
end
As an aside, for better or worse ActiveAdmin is an additional layer of abstraction on top of Ruby on Rails so my recommendation is always to master vanilla Rails first to ease the learning curve.
This is the button that should bring up the show page
edit - changed
<%= button_to 'Show', post_path(p) %>
This does bring up a button but I get an error when I click it:
No route matches [POST] "/posts/3"
I am confused why a POST request is being sent. (Note "post_path" refers to a "Post" controller/model).
In my routes I have
resources :posts
The button is on the index page and I have an iterator to make an edit button for each Post.
Here is my "show" action:
def show
#post = Post.find(params[:id])
end
Thanks for your help.
*edit: the code is here: https://github.com/MaxPleaner/feature_tester*
Rails will construct a form around button elements so that they work properly, and forms by default will use method: :post.
If it's just a link to the show page, it should be a link. You can style the link to look like a button if you really wish.
I need to call a method on Button click from my view. Below is the code in my view:
<button type="submit" id="1"><%link_to "Done", {:controller => :summary, :action => :done_order}, {:method => :put }%></button>
On click of this button, I have to delete a row from table and update the view with updated contents. Below is the done_order method in my controller
def done_order
List.where(:tableno => #orders.first.tableno).delete_all
redirect_to :action => :index
end
When I click on button I need to delete all rows(for a particular table number) from 'lists' table and redirect to index, which will again fetch rows from lists table in #orders and pass it to view to populate.
In routes file I have defined as: put "summary/done_order"
Problem is its not performing anything on click. I have tried numerous suggested ways with button_to & link_to but everytime landing up with some error. If there's no error then nothing is being performed.
I believe there is some shortcoming in my understanding to implement this and I am missing something on trying alternate ways. Please advise. thanks.
You should setup a route for this:
# routes
resources summaries do
put :done_order, on: :member
end
This should allow to write something like this:
= link_to "Done", done_order_summaries_path(#summary), method: :put
I'm not sure what you've named your objects, but it doesn't seem you've followed convention. So that part is up to you, but that's the basic mechanism. Another note is that you've put a link inside a button tag. I'm not sure that's even valid. There's no need for you to do that. Just style your link to look like a button or use a button instead, or even a button with a form.
I would like to dynamically change the menu based on the permissions of the viewing user. I would like the superadmin user to have access to the normal Resource actions (index, show, update, etc). So when an admin clicks on a menu item, it would take them to the index of that resource. I would like to restrict the normal admin user to just viewing a specific show page.
The menu route for the superadmin would be: /admin/resource
The menu route for the normal admin would be: /admin/resource/id
I would also like to restrict normal admin access to the index view, or other resources that they don't have access to. I have been able to achieve both these things, but I have yet to be able to map a menu item to a specific show page. I know I could create a custom page and view, but I really would like to share the custom DSL for the show and edit pages between superadmin and the normal admin.
Anyone know how to make this happen.
Ok, so I figured a way to get what I want. I am not sure if exactly fulfills what I wanted. (meaning, it would be nice to create custom menu items that are mapped to specific resources)
I just overwrote the index controller action to redirect to the specific show page. Because the super admin needs access to the original Store resource, I had to alias it with :as.
ActiveAdmin.register Store, :as => 'My Store' do
menu :if => proc{ !current_user.is_admin? },
:label => 'My Store'
actions :show, :edit, :update
controller do
def index
redirect_to(admin_my_store_url(current_user.store))
end
end
end