0

I have a 'account roles' controller (for my AccountRole model) in my application using views generated by the standard scaffold generator. This controller is nested into my 'accounts' controller (for my Account model) using the following routing configuration:

resources :accounts do
  resources :people, :controller => :account_roles
end

This results in routing helpers like account_people_path (/accounts/1/people) and account_person_path (/accounts/1/person/1). All well and good.

My problem comes in the default generated _form.html.erb partial. This file looks like this:

<%= form_for([@account,@account_role]) do |f| %>
  <!-- Rest of form -->
<% end %>

I've customised this view to cope with the nested resources, but my use of the :controller option in my routing is causing me problems I think.

My view spec for the 'new' and 'edit' views (both of which use this partial) is resulting in the following error:

ActionView::Template::Error:
   undefined method `account_account_role_path' for #<#<Class:0x105334360>:0x105300c18>

I think what's happening here is that form_for is trying to 'guess' the route helper for these resources. I know I could override the url by using the :url option to form_for, but that feels like a bit of a cop-out.

Am I missing an obvious solution to this that allows me to combine the :controller routing option with form_for's resource oriented style?

2
  • 1
    Is your model AccountRole or is it Person? Commented Mar 29, 2011 at 22:27
  • Hi @Vadim, the model is AccountRole. Will update question to make this clear. Commented Mar 29, 2011 at 22:33

3 Answers 3

1

I believe, you can specify your route like so

resources :people, :controller => :account_roles, :as => "account_roles"

As per http://guides.rubyonrails.org/routing.html#overriding-the-named-helpers

Sign up to request clarification or add additional context in comments.

1 Comment

Was ideally hoping for something that solved this problem from the other end, allowing me to keep my friendly helpers, but I think you're right - this is the best option open to me!
1

In case somebody like me is still looking for an answer to this question, I think I just figured it out. What I did, in your case would be

map.resources :account_roles, :as => "people" map.resources :people, :controller => "account_roles"

It did the trick for me.

Comments

0

While this is a somewhat older post, I was struggling with this as well.

My solution for this was to use the url option to specify the path.

In my case (which is slightly different, but still originates from the controller: "AccountRoles" stuff) I had to do the following:

resources :clients, controller: "AccountClients"


= form_for @client, url: clients_url(@client) do |f|
  = f.error_messages
  %p
    = f.label :name
    = f.text_field :name

  %p= f.submit

So you pass the @client variable to the form, but also set the correct url using url:.

In your case I reckon it would be:

<%= form_for([@account,@account_role], 
      url: account_people_path([@account,@account_role])) do |f| %>
  <!-- Rest of form -->
<% end %>

Hope this helps anyone coming across the same issue.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.