Search code examples
ruby-on-railsadministraterails-administrate

Rails Administrate: Cannot customise controller actions


I'm using Rails Administrate and want to override the new action on a controller. The documentation suggests this is straightforward. However, the application seems to ignore my override and goes straight to rendering new.html.erb instead of going through my override of the new method:

Controller:

class MyOrderController < Administrate::ApplicationController

    def valid_action?(name, resource = resource_class)
      %w[edit destroy].exclude?(name.to_s) && super
    end

    def new
      @orders = Order.find_by(property: params[:property])
    end
end

View:

<div>
<%= @orders.each { |order| %>
  <%=  order.id  %>
<% } %>
</div>

In particular, when going to /orders/new, the application doesn't go through my custom new action first, causing an error because @orders is nil.


Solution

  • Nevermind, cause was a silly syntax error: end statement was in the wrong place:

    module MyModule do
      class MyOrderController < Administrate::ApplicationController
    
        def valid_action?(name, resource = resource_class)
          %w[edit destroy].exclude?(name.to_s) && super
        end
      
      end <-- in wrong place
    
        def new <-- outside the scope of Controller 
          @orders = Order.find_by(property: params[:property])
        end
      end