Remember that your controller actions share the same method space with private methods defined in ActionController::Base. If your controller...
Rails has always included a scaffold script that generates a default controller implementation for you. Unfortunately that generated controller is...
Views are normally (for good reason) not rendered in controller specs. If you need it to happen, use:
Code snippet tested with Rails 2.3 def index # ... if request.xhr? html = render_to_string(:partial => "list", :layout => false) respond_to...
In Rails 5+ you can access a helper from a controller using the helpers method: # Inside a controller action
When you use the :as option to map a power to a controller method you can now override the generated...
...the first example rails will expect to find a Users::SignUpsController in the subdirectory controllers/users/sign_ups_controller.rb, but there has to be a SignUpsController in controllers/sign_ups_controller.rb in order to make the second...
Rails supports alert and notice as default flash types. This allows you to use these keys as options in e.g...
end define something like this in your spec_helper.rb: def sign_in(user = User.new) controller.instance_variable_set('@current_user', user)
...by stubbing the controller's session and querying that stub: persisted_session = ActionController::TestSession.new controller.stub :session => persisted_session # do things persisted_session[:something].should ... Apply this workaround only when you...
...need it. Stubbing controller.session is not entirely the same, but good enough in most cases. Example If you, for example, had code that increments a "page_views" entry for every...
In order to keep the controllers directory tidy, we recently started to namespace controllers. With the :controller option you can...
You know that you can force absolute URLs throughout a response. Now you want to modify URLs similarly, but only...
You can quickly access views that belong to a controller by using the tiny "page with arrow" icon in the...
...the filter chain in specs. You can do it like that on Rails 2: controller.class.filter_chain.map(&:method) Note that we need to look at the controller's class since before_filter...
...and after? on the filter objects to scope down to only some of them: controller.class.filter_chain.select(&:before?).map(&:method) For Rails 3, do it like this (warning, ugly!): controller._process_action_callbacks.select { |c| c.kind...
If a controller action responds to other formats than HTML (XML, PDF, Excel, JSON, ...), you can reach that code in...
In the Controller: // Instead of doing this: app.controller('TodoCtrl', function ($scope) { $scope.input = 'ex. buy milk'; }); // Do this: app.controller('TodoCtrl', function...
The method cookies is defined in the ActionController and should never be overwritten. Bad example class StaticPagesController < ApplicationController
view_context.helper_method('args') Rails 2 ApplicationController.helpers.helper_method('args') Also see How to use helper methods inside a model...
In moderately complex authorization scenarios you will often find yourself writing a map like this: class NotesController < ApplicationController power :notes...
If you have a FooController and also have a layout app/views/layouts/foo.html, Rails will use this without being told so.
This is what worked for me in a Rails 4: # JSON data as first argument, then parameters patch :update, { some...
Do that for noble reasons only.
The Rails router has been written and rewritten at least four times2, including a recent rewrite for the upcoming Rails...