0

Defined an action transfer in transactions controller (Rails 4.2). Action transfer is similar to new by bringing up a form transfer.html.erb for user to fill and save:

def transfer
  @title = t('Account Transfer')
  @transaction = BankAccountx::Transaction.new()
end

In routes.rb, define the route:

  resources :transactions do
    member do
      get :transfer
      patch :transfer_result
    end 
  end  

Here is the spec:

it "should render transfer" do
    session[:user_id] = @u.id
    get 'transfer'
    expect(response).to be_success
end

In rspec, there is error:

ActionController::UrlGenerationError:
       No route matches {:action=>"transfer", :controller=>"bank_accountx/transactions"} 

The following is in the spec:

routes {BankAccountx::Engine.routes}

If we use collection instead of member in routes.rb, then the above rspec passes. The following route works:

 resources :transactions do
    collection do
      get :transfer
      patch :transfer_result
    end 
 end  

Why this member action transfer requires collection route in routes.rb?

user938363
  • 9,990
  • 38
  • 137
  • 303

1 Answers1

2

The way you are calling transfer action it looks like you are not passing any ID. Member route always work on ID (i.e. on specific member) and collection works on multiple objects.

e.g. The methods like edit, show are examples of member methods because they accept ID of object and works on specific object. Whereas the method like index is example of collection method

  • `transfer` is like `new` by bringing up a form `transfer.html.erb` for user to fill and save. So there is no `id` to pass in and that's probably the cause of the error. Is default `new` treated by Rails as collection route? – user938363 Dec 10 '15 at 14:10
  • Here is an interesting post about member and collection. new/create seem to be a collection action: http://stackoverflow.com/questions/3028653/difference-between-collection-route-and-member-route-in-ruby-on-rails – user938363 Dec 10 '15 at 14:38