mardi 17 novembre 2015

How to link to an action without a route helper

How do I link to an action without a route helper?

I have a route

get '/batches/:id/creation_wizard/add_funds' => 'batch::creation_wizard#add_funds'

If I do in the Rails console

include Rails.application.routes.url_helpers
default_url_options[:host] = "localhost"
url_for(controller: 'batch::creation_wizard', action: 'add_funds', id: 1)

I get "http://localhost/batches/1/creation_wizard/add_funds"

But if I have in

class Batch::CreationWizardController < ApplicationController
  def my_method
    redirect_to controller: 'batch/creation_wizard', action: 'add_funds', id: 1
  end
end

I get

No route matches {:controller=>"batch/batch::creation_wizard", :action=>"add_funds", :id=>1}

and if I try

redirect_to controller: 'creation_wizard', action: 'add_funds', id: 1

I get

No route matches {:controller=>"batch/creation_wizard", :action=>"add_funds", :id=>1}

and if I try

redirect_to action: 'add_funds', id: 1

I get

No route matches {:action=>"add_funds", :id=>1, :controller=>"batch/creation_wizard"}

I tried reading the Rails guide "Rails Routing from the Outside In", and "Getting Started with Rails", and I didn't notice anything that helped.

I could change the routing to

get '/batches/:id/creation_wizard/add_funds' => 'batch::creation_wizard#add_funds', as: :creation_wizard_add_funds

and rely upon the route helper, but that feels hacky.

I'm using Rails 3.2.22.

What am I doing wrong?

Aucun commentaire:

Enregistrer un commentaire