Sending raw JSON data to a member action in a controller spec
This is what worked for me in a Rails 4:
# JSON data as first argument, then parameters
patch :update, { some: 'data' }.to_json, id: id, format: :json
Related cards:
Consul 0.3.0 has a shortcut to quickly create an action map for CRUD controllers
In moderately complex authorization scenarios you will often find yourself writing a map like this:
class NotesController < ApplicationController
power :notes, :map => {
[:edit, :update] => :updatable_notes
[:new, :creat...
How to mount a legacy database to migrate data
There are many approaches out there how you can import data from a legacy application to a new application. Here is an approach which opens two database connections and uses active record for the legacy system, too:
1. Add you database informati...
RSpec: How to test the content of a flash message in a request spec
The ActionDispatch module of Rails gives you the helper method flash
to access the flash messages in a response.
describe PostsController, type: :request do
descr...
Beware: Don't name a controller action "cookies"
The method cookies
is defined in the ActionController and should never be overwritten.
Bad example
class StaticPagesController < ApplicationController
def imprint
end
def cookies
redirect_to '/'
end
end
If you cr...
How to make changes to a Ruby gem (as a Rails developer)
At makandra, we've built a few gems over the years. Some of these are quite popular: spreewald (> 1M downloads), active_type (> 1M downloads), and geordi (> 200k downloads)
Developing a Ruby gem is different from developing Rails applications, w...
Best practice: How to manage versions in a package.json
It most cases it's not necessary to add a version constraint next to your packages in the package.json
. Since all versions are saved in a lockfile, everyone running yarn install
will get exactly the same versions. Yarn saves this lockfile as `...
Don't define a controller action called #process
Remember that your controller actions share the same method space with private methods defined in ActionController::Base
. If your controller behaves in super-weird ways, check that you don't overwrite some internal method with a controller action.
How to use Parallel to speed up building the same html partial multiple times (for different data)
The parallel-gem is quite easy to use and can speed up rendering time if you want to render the same partial multiple times (e.g. for rendering long lists of things).
If your parallelized code talks to the da...
How to use helper methods in a controller
Rails 3+
view_context.helper_method('args')
Rails 2
ApplicationController.helpers.helper_method('args')
Also see [How to use helper methods inside a model](https://makandracards.com/makandra/1307-how-to-use-hel...
Howto respond html or json in the same controller action with Rails 2
Code snippet tested with Rails 2.3
def index
# ...
if request.xhr?
html = render_to_string(:partial => "list", :layout => false)
respond_to do |format|
format.html { render :text => html }
format.json {...