create multiple forms for a resource
I have created a resource called contents_admin I want to add a button next to the "+" / plus button
This new button will be called filter.
when i click on filter - the form will appear similar to add new form.
when user fills the filter form, session will be populated with form fields.
so far i have placed the button to filter in app/views/trestle/resource/index.html.erb as below
<% toolbar(:primary) do |t| %> <%= t.link admin.t("buttons.new", default: "New %{model_name}"), action: :new, icon: "fa fa-plus", class: "btn-new-resource" if admin.actions.include?(:new) %> <%= t.link admin.t("buttons.new", default: "Filter For %{model_name}"), action: :filter, icon: "fa fa-plus", class: "btn-new-resource" if admin.actions.include?(:filter) %> <% toolbar(:primary) do |t| %> <%= t.link admin.t("buttons.new", default: "New %{model_name}"), action: :new, icon: "fa fa-plus", class: "btn-new-resource" if admin.actions.include?(:new) %>
the new button appears fine but when i click on it - I get below error No route matches {:action=>"filter", :controller=>"contents_admin/admin"}
It looks like you might need to add the filter route and controller action to your admin. See this wiki page for a good example: https://github.com/TrestleAdmin/trestle/wiki/How-To:-Add-Custom-Action
how does trestle generate routes like new , edit for all the resources.
i wanted to modify the trestle gem for own use so that i get the feature in all the resources. also - i wanted the form to appear for the filter route similar to the way it comes up for the new route. when save is clicked on the form - i wish my own controller to be called. could you please tell me how trestle or rails engine generate the routes for the app. could you please tell me how trestle or rails engine generate the routes for the app.
when i do ContentsAdmin.path(:new) it works fine.
how do i add a new route to my app through trestle engine. so that below command works: ContentsAdmin.path(:filter)
please guide me a bit.
The standard RESTful routes are defined internally (see https://github.com/TrestleAdmin/trestle/blob/master/lib/trestle/resource/controller.rb). You could potentially monkey-patch this but it's not the safest route.
The method I would currently recommend to re-use your custom routes and actions among multiple admins is to define a module:
module Filterable
def self.extended(builder)
builder.instance_eval do
controller do
def filter
# Action goes here
end
end
routes do
post :filter, on: :member
end
end
end
end
and then extend it within your admin:
Trestle.admin(...) do
extend Filterable
end
in /app/views/trestle/resource/index.html.erb :
<%= t.link admin.t("buttons.new", default: "Filter For %{model_name}"), action: :myfilter, icon: "fa fa-plus", class: "btn-new-resource" %>
in /lib/trestle/resource/controller.rb def myfilter puts "inside filter --------------------------- " end
in my app contents_admin.rb
routes do get :myfilter, on: :member end in my app contents_admin.rb def myfilter end
error: No route matches {:action=>"myfilter", :controller=>"contents_admin/admin"}
If I do rake routes I see myfilter_contents_admin GET /contents/:id/myfilter(.:format) contents_admin/admin#myfilter
In your case you might need to specify the route as get :myfilter, on: :collection, since it appears not to operate on an instance but on the collection (i.e. similar to the index action).
Check out the Rails routing guide at https://guides.rubyonrails.org/routing.html#adding-more-restful-actions. Trestle's routes block is called inside of a Rails resources routing block so should support most of the options and methods you can use there.
yes sir. The route error has gone now. sorry about the on: :member mistake.
However - I will now need to write my own code to generate the form. I need the form to be similar to the "new" route
So i loose out on existing functionality provided by trestle . also DRY principle will not be followed.
can i generate the "myfilter" route through trestle and not specify it in contents_admin
If i do not add custom action route and write in lib/trestle/resource/controller.rb : def myfilter puts "inside filter --------------------------- " end
why do i still get the missing route error?
why do i need to specify the route in contents_admin.rb even though i have specified the def myfilter in resource/controller.rb ???
is there a way to add the myfilter route through trestle engine?
why do i still get the missing route error? why do i need to specify the route in contents_admin.rb even though i have specified the def myfilter in resource/controller.rb ???
This is a Rails requirement -- hooking up a controller action requires the route to be defined, so that links/buttons/requests no how the URL should be generated and what code that URL should be dispatched to.
is there a way to add the myfilter route through trestle engine?
If you have a route that you want to add globally within your admin, you can inject routes directly into Trestle from your routes.rb file:
Trestle::Engine.routes.draw do
# Add your routes here (will be scoped to /admin)
end
However in your case it seems like you will want the route to apply for each admin.
Thank you for your answer.
Let me rephrase my question a bit please.
if I do rake routes , then i see the "new" route for all my resources/admins. example : new_contents_admin GET /contents/new(.:format) contents_admin/admin#new new_movies_admin GET /movies/new(.:format) movies_admin/admin#new
so how is the "new" route added to all the resources / admins .
I have not added anything in my app's routes.rb I have not added the new action in my app's controller either.
It is being added to each resource by trestle engine.
So how can i add a similar route called "myfilter" to all my resources by modifying the trestle gem on my local machine.
Thanks a bunch :)
Hello sir,
I read your answers again.
I will try this again and report back.
In the interim, I have created a new resource/admin for the filter to work.
So I have one resource called contents and one resource called filter.
Both resources point to Content model.
The contents resource handles creating, editing. The filter resource handles filtering.
So Content model has creation, editing, filtering features now :)
I will post the code in a blog post for review by community.
I will also try adding a new route to trestle from routs. Rb of my app. This could probably work as well. I will try it out.
I think the new, edit routes are being created by rails by default. Will verify this.
Thanks a million.
Best regards, Karan
Hello,
Thanks for responses.
I figured out that new, edit routes are added by default by rails. As you mentioned as well.
To create multiple forms on a resource, I created a new resource.
The contents resource handles creation of content with route contents/new
The filter resource handles filtering of contents with route filter/new
It works ok.
I still wish to add a filter action to the contents resource with the route contents/filter
I can add a custom action but I want trestle to show the exact same form that appears on the "new" Action
So contents/new And contents/filter should display the same form.
I shall try to do this and report back.