Passing action along in form with multiple pages
- Dominant language
- No language data
- Stars
- 664
- Forks
- 41
- PR merge metrics
- No merged PRs in 30d
Description
I have a form with name and email (form1) on one page, which is inserted into the db, then goes to a second form (`/signup2`) to fill in password and security Q&A. The first page was loading as expected and the name & email was being properly inserted into the database upon clicking "Next".
However, when it was redirected to `/signup2` there would be an error:
```
assign @action not available in eex template.
Please make sure all proper assigns have been set. If this
is a child template, ensure assigns are given explicitly by
the parent template as they are not automatically forwarded.
Available assigns: [:changeset, :conn, :current_user, :view_module, :view_template]
```
So the trouble seemed to be that in `/signup2` there was no action. The create controller that inserted the input from form1 and redirected to `/signup2` was as follows when the error was occurring:
```
def create(conn, %{"user" => user_params}) do
changeset = User.changeset(%User{}, user_params)
case Repo.insert(changeset) do
{:ok, _post} ->
conn
|> redirect(to: user_path(conn, :signup2, changeset: changeset))
{:error, changeset} ->
render(conn, "new.html", changeset: changeset)
end
end
```
When the `changeset` was inspected, the `action` was set to `nil`.
We tried setting this by adding `assign` to the pipe, but assign kept coming back as an undefined function:
```
{:ok, _post} ->
conn
|> assign(action: :some_action)
|> redirect(to: user_path(conn, :signup2, changeset: changeset))
```
The solution was to render the `signup2.html` and pass in the `action` directly.
```
{:ok, _user} ->
render(conn, "signup2.html", changeset: changeset, action: :some_action)
```
Contributor guide
Research direction
Start with the create controller action shown in the issue and the /signup2 route and signup2.html template. Reproduce the redirect path and compare it with the direct render example, then document the intended way to pass the changeset and action so the template has the required assigns.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- elixir
- Domain
- backend, documentation
- Issue type
- Documentation
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 30/100