Created
August 29, 2019 17:44
-
-
Save fabioaraujo121/c6ae4869f47e290f5df86cfea7ad2cbf to your computer and use it in GitHub Desktop.
[rails_admin] How to create a costum action with form? GET and POST
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- <root>/app/views/rails_admin/main/next_step.html.erb --> | |
<div class="container-fluid"> | |
<div class="row"> | |
<div class="col-md-6 col-md-offset-3"> | |
<%= form_tag(next_step_path, {method: :post}) do %> | |
<div class="form-group"> | |
<label for="step">Próximo Passo:</label> | |
<%= select_tag "step", options_from_collection_for_select(@kind_steps, "id", "name"), prompt: "Selecione o Passo", class: "form-control", onchange: "this.form.submit();" %> | |
</div> | |
<% end %> | |
</div> | |
</div> | |
<div class="row"> | |
<div class="col-md-8 col-md-offset-2"> | |
<table class="table"> | |
<thead> | |
<tr> | |
<th>Passo</th> | |
<th>Responsável</th> | |
<th>Em</th> | |
</tr> | |
</thead> | |
<tbody> | |
<% @object.steps.order(created_at: :desc).each do |step|%> | |
<tr> | |
<td><%= step.kind_step.name %></td> | |
<td><%= step.user.name %></td> | |
<td><%= l step.created_at %></td> | |
</tr> | |
<% end %> | |
</tbody> | |
</table> | |
</div> | |
</div> | |
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# <root>/lib/rails_admin/next_step.rb | |
class RailsAdmin::Config::Actions::NextStep < RailsAdmin::Config::Actions::Base | |
RailsAdmin::Config::Actions.register(self) | |
register_instance_option :http_methods do | |
[:get, :post] # NEW / CREATE | |
end | |
register_instance_option :member do | |
true | |
end | |
register_instance_option :breadcrumb_parent do | |
[:show, bindings[:abstract_model], bindings[:object]] | |
end | |
register_instance_option :controller do | |
proc do | |
if request.get? | |
# Rendering the page | |
@kind_steps = @object.kind.kind_steps | |
render @action.template_name, status: 200 | |
elsif request.post? | |
@step = Step.new(demand: @object, kind_step_id: params[:step].to_i, user: current_user) | |
if @step.save! | |
@auditing_adapter && @auditing_adapter.create_object(@object, @abstract_model, _current_user) | |
respond_to do |format| | |
format.html { redirect_to_on_success } | |
format.js { render json: {id: @object.id.to_s, label: @model_config.with(object: @object).object_label} } | |
end | |
else | |
handle_save_error | |
end | |
end | |
end | |
end | |
register_instance_option :route_fragment do | |
'next-step' | |
end | |
register_instance_option :link_icon do | |
'icon-arrow-right' | |
end | |
register_instance_option :visible? do | |
subject = bindings[:object] | |
subject.is_a? Demand | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# <root>/config/initializers/rails_admin.rb | |
RailsAdmin.config do |config| | |
require Rails.root.join('lib', 'rails_admin', 'next_step.rb') | |
### Popular gems integration | |
## == Devise == | |
config.authenticate_with do | |
warden.authenticate! scope: :user | |
end | |
config.current_user_method(&:current_user) | |
## == CancanCan == | |
# config.authorize_with :cancancan | |
## == Pundit == | |
# config.authorize_with :pundit | |
## == PaperTrail == | |
# config.audit_with :paper_trail, 'User', 'PaperTrail::Version' # PaperTrail >= 3.0.0 | |
### More at https://github.com/sferik/rails_admin/wiki/Base-configuration | |
## == Gravatar integration == | |
## To disable Gravatar integration in Navigation Bar set to false | |
# config.show_gravatar = true | |
config.actions do | |
dashboard # mandatory | |
index # mandatory | |
new | |
export | |
bulk_delete | |
show | |
edit | |
delete | |
show_in_app | |
next_step | |
## With an audit adapter, you can add: | |
# history_index | |
# history_show | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment