Ratatouille
Ratatouille

Reputation: 1492

Define Controller for the custom action doesnot seem to work Rails Admin

HI Everyone ,

I have rails admin implemented in my project Now there are couple of thing that I currently stuck at

  1. I want a link (Mark as Publisher) In the list View of my user Controller in the rails admin as ajax link something that is done using remote => true in rails after that where the write the associated jscode and html code for it

  2. for the above custom action "mark_as_publisher" I define the configuration setting like this

    Inside config/rails_admin.rb

     config.actions do 
    
            # root actions
          dashboard                     # mandatory
          # collection actions 
          index                         # mandatory
          new
          export
          history_index
          bulk_delete
          # member actions
          show
          edit
          delete
          history_show
          show_in_app
          member :mark_as_publisher
     end
    

    Now The Definition of the custom action look like this

    require "rails_admin_mark_as_publisher/engine"
    
    module RailsAdminMarkAsPublisher
    end
    
    require 'rails_admin/config/actions'
    
    module RailsAdmin
      module Config
        module Actions
          class MarkAsPublihser < Base
            RailsAdmin::Config::Actions.register(self)
    
            register_instance_option :collection do
              true
            end
    
            register_instance_option :http_methods do
              [:get,:post]
            end
    
            register_instance_option :route_fragment do
              'mark_as_publisher'
            end 
    
            register_instance_option :controller do
              Proc.new do
                binding.pry
                if request.get?
                  respond_to do |format|
                    format.html { render @action.template_name} 
                  end
                elsif request.post?
                  redirect_path = nil
                  if @object.update_attributes(:manager => true)
                    flash[:success] = t("admin.flash.successful", :name => @model_config.label, :action => t("admin.actions.mark_as_publisher.done"))
                    redirect_path = index_path
                  else
                    flash[:error] = t("admin.flash.error", :name => @model_config.label, :action => t("admin.actions.mark_as_publisher.done"))
                    redirect_path = back_or_index
                  end
                end  
              end
            end  
          end
        end
      end
    end
    

Now the View for the same define in app/view/rails_admin/main/mark_as_publisher.erb look like this

<%=  rails_admin_form_for @object, :url => mark_as_publisher_path(:model_name => @abstract_model.to_param, :id => @object.id), :as => @abstract_model.param_key,:method => :post ,:html => { :class => "form-horizontal denser", :data => { :title => "Mark" } } do |form| %>
  <%= form.submit "save" %>
<%end%>

The get and post url for mark_as_publisher does come under by controller define above and saving the above form result in error called

could not find routes for '/user/5/mark_as_publisher' :method => "post"

Does Any body has an idea of what I'm missing

Upvotes: 2

Views: 2857

Answers (1)

drubin
drubin

Reputation: 1012

Sorry for the delayed reply, but I also came into the exact same issue.

EDIT: I notice you already have this, have you tried restarting your server?

if you add the following it will fix it.

  register_instance_option :http_methods do
      [:get,:post]
  end

The problem is by default Actions only respond to the :get requests.

If you run

 rake routes 

You will see something along the lines of

  mark_as_publisher_path     GET     /:model_name/:id/mark_as_publisher(.:format)    rails_admin/main#mark_as_publisher

https://github.com/sferik/rails_admin/blob/master/lib/rails_admin/config/actions/base.rb#L89

Upvotes: 2

Related Questions