3
votes

In my Phoenix 0.10.0 app, I ran

mix phoenix.gen.resource Job jobs job_name:string job_url:string company_url:string location:string notes:string

following up by adding a resources line router.ex and running mix ecto.migrate.

With these changes, creating a new record from the web interface works fine as long as I fill out all the fields. If I leave a single one of them blank there's an error message saying "Oops, something went wrong!" in the browser.

I'd like to allow the notes to be left blank. How can I do that?

1

1 Answers

2
votes

The required fields are listed in the third argument to cast in the definition of changeset so remove the ones that aren't required there and add them to the list of optional arguments at the end:

git diff
diff --git a/web/models/job.ex b/web/models/job.ex
index 2d6303b..323b114 100644
--- a/web/models/job.ex
+++ b/web/models/job.ex
@@ -18,6 +18,6 @@ defmodule JobHunt.Job do
   with no validation performed.
   """
   def changeset(model, params \\ nil) do
-    cast(model, params, ~w(job_url), ~w())
+    cast(model, params, ~w(job_url), ~w(job_name company_url location notes))
   end
 end

(Edited.)