In a Rails app, I have the following route.
namespace :masterdata do
resources :projects do
resource :approval, only: [:new], controller: "project_approvals"
end
end
When submitting the masterdata_parameter_approval#new
form, a Signature
is created by calling the signatures#create
action is called.
Since signatures are associated with multiple models, the signatures#create
action redirects to the show
action of the signed model.
redirect_to params[:signature].signable
Since the projects
routes are namespaced, I get an error telling me that project_url
doesn’t exist.
To solve the issue, I have added the following route:
get "/projects/:id", to: redirect("/masterdata/projects/%{id}"), as: "project"
Which works, but I’m not too fond of it.
Is there any other way to make the redirect (in the signatures controller) work without adding a dedicated route?
Sice it’s namespaces it should be like this
redirect_to masterdata_project_path(@project)
i assume you have an instance of project in your create action called @project
1
You can either call the routing helper explicitly:
redirect_to masterdata_project_path(project)
Use rails routes -c project
to get a list of the routes. The name of the routes are on the first column. Add _path
or _url
to get the actual helper method.
Or you can pass an array to the polymorphic routing helpers to resolve the route helper method name dynamically:
redirect_to [:masterdata, project]
Both have the exact same end result but the later is useful when you want to generalize the code for resuse.