This project is archived and is in readonly mode.
Problem with scoped routes and form_for helper
-
Andrew White
- State changed from new to invalid
- Importance changed from to Low
The error you're getting is because the @submission variable is a new record and the admin_submission_path url helper is expecting a value for :id. You're also using form_for in the wrong way - even if the url helper worked, the form_for helper would fail as you're not passing the correct arguments.
Reading the form helper guide should help make things clearer.
-
Jack Chu
Andrew, thanks for your reply.
I looked over the guide and that helped me with the proper arguments for form_for, however I can't find a good example of how to use form_for and scoped routes anywhere. I also wanted it to handle both create/update at the same time (so it can be used in a _form partial for example). Maybe you should add a section with dealing with scopes after the "Dealing with Namespaces" section.
For anyone with the same issue, from looking at the form_for api and guide, this seems to the way to handle it:
= form_for @submission, :url => @submission.new_record? ? submissions_path(@project.name) : submission_path(@project.name, @submission), :html => { :multipart => true } do |f|Ternary operator in the url value checks to see if @submission is a new record and returns a create or edit path accordingly.
-
Jack Chu
Oops, I just noticed the API seems to prefer #persisted? to #new_record?, so I guess this works better:
= form_for @submission, :url => @submission.persisted? ? submission_path(@project.name, @submission) : submissions_path(@project.name), :html => { :multipart => true } do |f|