Inheriting and Routing a Custom Action Using ApplicationController in Rails

I am using Rails 2.3.2, but I am sure that this applies to newer versions. I would like to define a custom action in ApplicationController. However, I do not want to add a custom route to each subclass of the controller that uses this action. Is there an easy way to do this?

My first wish was to simply direct directly to the ApplicationController, since this method should not be overridden by subclasses. But I do not think that Rails allows you to switch to ApplicationController more.

Has anyone else suggested something like this:

map.connect ":controller/:action", :controller => my_regex, :action => my_regex 

But I wonder if this could be in conflict with other routes or cancel them? Or if at all the best way? Thanks!

0
source share
4 answers

In the end, I decided to consider my own js_form_builder resource, so I have a controller that will deliver it. The controller accepts additional parameters resource_name and id. If provided, I can create an instance of the object as follows:

 @object = params[:resource_name].classify.constantize.find(params[:id]) 

Then I just post the js.erb template which contains all my js_form_builder support. If @object was created, I can create form_for @object, then skip its attributes and create methods in the javascript form builder object that will return the inputs for each attribute using the Rails FormBuilder tags to create them.

For instance:

 window.FormBuilder = function() { var builder = {}; builder.form = function() { var js = ""; js += '<%= form_for @object do |f| %>'; <% @object.attributes.each do |name, val| %> var methodName = '<%= name.camelize(:lower) %>'; <% if val.class == String %> builder[methodName] = $('<%= f.text_field name.to_sym %>'); <% end %> <% if val.class == TrueClass || val.class == FalseClass %> builder[methodName] = $('<%= f.check_box name.to_sym %> <%= f.label name.to_sym %>'); <% end %> <% end %> js += '<% end %>'; return $(js); }; builder.newForm = function() { var js = ""; js += '<%= form_for @object.class.new do |f| %>'; js += '<% end %>'; } return builder; } 

Currently, I am not sure how useful this input is, since I cannot come up with a script in which I simply would not use html.erb for them. But it was sure that it made it work! :)

0
source

I do not think that this is a modification variant of ApplicationController , but for ActionDispatch::Routing code in ActionDispatch::Routing to include the new actions that you want. This is like the crazy thing you can do in a things scheme, since there is no standard way to increase or extend the usual REST actions. I hope you have every reason to do so.

When viewing the code, you can see what actions are defined by default, and you can enter a new one. Rails 3 has a slightly different structure, but the idea is the same:

  class ActionDispatch::Routing::Mapper::Resources::Resource ENHANCED_DEFAULT_ACTIONS = DEFAULT_ACTIONS + [ :myaction ] def self.default_actions ENHANCED_DEFAULT_ACTIONS end end 

You will have to modify ActionDispatch::Routing::Mapper::Resources#resources to behave differently, but you did not specify if you are talking about a set, new or type of member action, so you just have to copy and change to behave the way you want.

0
source

if you declare this controller as a resource, for example map.resource , then you will have to use the default actions or create your own by adding member or collection to this resource.

  map.resources :post, :member => {:update_live_boolean => :post }, :collection => {:get_all_live_posts => :get} 

Otherwise, if you use the old routing format and do not use REST

  map.connect ':controller/:action/:id' map.connect ':controller/:action/:id.:format' 

Then, all you need to do to bind to the user controller provides the variables :controller' and : action / : id` if necessary

 <%= link_to "New Custom Controller", {:controller => "new_custom_controller", :action => "index"%> 
0
source

This is the part of your post that bothers me:

I do not want to add a custom route to each individual subclass of the controller that uses this action.

The rail community is rightfully becoming more cautious about over-identifying unnecessary routes. It is becoming more common to see things like this:

 map.resources :comments, :only => [:new, :create] 

In the above example, only the new and create routes are generated. This is better security and cleaner routing. Although I am not directly answering your question about how to make the new route available for each resource, I say that the best rail methods will prevent this. Add a custom route only to the resources that will use it.

0
source

All Articles