Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Route errors when using namespace routes

How do you deal with form_for's when the routes are namespaced? I am getting some weird route errors that I really expect to get.

For example, let's say you have a controller called Admin::CompaniesController in your :admin namespace in your routes.rb:

namespace :admin do
  resources :companies
end

Most things work just fine, but I get an error when I render a new form. Here's the code:

<%= simple_form_for(@company, :url => admin_company_path(@company)) do |f| %>

And here's the error message:

ActionView::Template::Error: No route matches {:action=>"show", :controller=>"admin/companies", :id=>#<Company id: nil, name: nil, phone_number: nil, address: nil, postal_code: nil, is_enabled: true, courses_created: 0, province_id: nil, theme_id: nil, payment_plan_id: nil, created_at: nil, updated_at: nil>}

How can I get rails to play nice here? I obviously want one url for edits, and another for new forms. Usually, I'd never even have to put :url in my form_for statements, but because of the nesting, I am forced to.

I have no idea what to do here now, at least not elegantly.

like image 357
Fire Emblem Avatar asked May 28 '11 02:05

Fire Emblem


1 Answers

Try using simple_form_for([:admin, @company]) do |f|

like image 196
Devin M Avatar answered Oct 23 '22 09:10

Devin M