rails: link_to / form_for doesn't work for singular resource
In my routes I have
resource :company
and in my views
<% form_for(@store) do |f| %>
…
<% end %>
When
new action gets an error when rendering
undefined method
hash_for_companies_path’ for #Module:0x00000102bfd3f0`
The stack trace reviels that the problem occurs in
lib/action_dispatch/routing/polymorphic_routes.rb:133:in
polymorphic_url’`
edit action can render without errors but the forms url is /company.4
where 4 is the id of the company.
This seems to be a bug that has been around for a very long time so I think it’s time to fix it.
About this issue
- Original URL
- State: closed
- Created 13 years ago
- Reactions: 3
- Comments: 67 (40 by maintainers)
Links to this issue
Commits related to this issue
- Added a warning about #1769 to the routing guide. — committed to rails/rails by codyrobbins 11 years ago
- Merge pull request #142 from codyrobbins/master Added a warning about #1769 to the routing guide — committed to rails/rails by vijaydev 11 years ago
- Fixing singular Rails resource behavior Since we have a singular resource for ProfileRequest, the `Controller#respond_with` was treating the profile_request parameter as the :format option (i.e. form... — committed to samvera-deprecated/orcid by jeremyf 10 years ago
- Fixing singular Rails resource behavior Since we have a singular resource for ProfileRequest, the `Controller#respond_with` was treating the profile_request parameter as the :format option (i.e. form... — committed to samvera-deprecated/orcid by jeremyf 10 years ago
- Implement home/index view References about form_for URL direct specification http://guides.rubyonrails.org/routing.html#singular-resources https://github.com/rails/rails/issues/1769 — committed to kaosf/cure-general-election by kaosf 10 years ago
- Make form_for correctly handle singular resources. Fixes #1769. Given the following route: ``` resource :account ``` This would generate an error: ```ruby <%= form_for(Account.new) do |f| %> ... <%... — committed to eval/rails by eval 10 years ago
- bugfix: avatars always use a single resource So we set this in the model to work around the plural default. This is because of a loong pending rails bug: https://github.com/rails/rails/issues/1769 — committed to riseuplabs/crabgrass-core by azul 9 years ago
- Add singular resource support for `form_for` Fixes #1769 — committed to maclover7/rails by maclover7 8 years ago
- Add singular resource support to `polymorphic_path` A somewhat less invasive redo to #23535. The problem that makes #1769 so hard to solve, is that `polymorphic_path` is only provided with an databas... — committed to maclover7/rails by maclover7 8 years ago
- Add custom polymorphic mapping Allow the use of `direct` to specify custom mappings for polymorphic_url, e.g: resource :basket direct(class: "Basket") { [:basket] } This will then generate the ... — committed to rails/rails by pixeltrix 7 years ago
- Add custom polymorphic mapping Allow the use of `direct` to specify custom mappings for polymorphic_url, e.g: resource :basket direct(class: "Basket") { [:basket] } This will then generate the ... — committed to rails/rails by pixeltrix 7 years ago
For anyone landing here via google, this is what got implemented: http://edgeguides.rubyonrails.org/routing.html#singular-resources
In short, add something like
resolve('User') { [:user] }
after you define the singular resource in routes.rb, where ‘User’ is the class name and :user is the resource name.To anyone stumbling upon this issue, the workaround is quite simple. Given:
You generate the
form_for
call like this:Not sure if it can help to fix the overall problem but I helped myself the following way, since https://github.com/rails/rails/issues/1769#issuecomment-9556381 did not work properly for my new action
EDIT: slightly modified to make it work also when validation errors come up.
Couldn’t this be fixed in the latest rails implementation by changing the behavior of
form_with
?In case anybody is looking for a workaround:
http://stackoverflow.com/a/16229154/1149074
Yeah, it does handle both
:create
and:update
actions.Yep, here too, my first time with singular resource, lost 3h coz i was blaming myself like a first
i think this bug should be written in rails guides somewhere.
for anyone using a singular resource, they will eventually find themselves on this bug page. better to announce first