ex_admin icon indicating copy to clipboard operation
ex_admin copied to clipboard

Support collection tuples in form

Open smpallen99 opened this issue 8 years ago • 7 comments

We currently support

input address, :type, collection: ~w(Home Work Other)

But we would like to support:

input address, :type, collection: [Home: "home", Work: "work", Other: "other"]

This request came from issue #89

smpallen99 avatar May 05 '16 14:05 smpallen99

Hi @smpallen99

I was planning to give this issue a try and solve it, but I keep running into an error (that is probably my fault) when trying to insert a has_many association. Here is the example I made. If you look at mix.exs you will see that the path for the ex_admin dep is being loaded from my machine (the same as master for now). Could you take a look?

The stacktrace:

[error] #PID<0.634.0> running ExAdminDemo.Endpoint terminated
Server: localhost:4000 (http)
Request: POST /admin/users/2
** (exit) an exception was raised:
    ** (UndefinedFunctionError) undefined function :error.__struct__/0 (module :error is not available)
        :error.__struct__()
        ExAdmin.Repo.do_attributes_for/5
        lib/ex_admin/repo.ex:202: anonymous fn/4 in ExAdmin.Repo.insert_or_update_attributes_for/2
        (elixir) lib/enum.ex:1473: Enum."-reduce/3-lists^foldl/2-0-"/3
        lib/ex_admin/repo.ex:29: ExAdmin.Repo.changeset_attributes_for/3
        lib/ex_admin/repo.ex:16: ExAdmin.Repo.changeset/3
        web/controllers/admin_controller.ex:268: ExAdmin.AdminController.update/2
        web/controllers/admin_controller.ex:1: ExAdmin.AdminController.action/2
        web/controllers/admin_controller.ex:1: ExAdmin.AdminController.phoenix_controller_pipeline/2
        (ex_admin_demo) lib/phoenix/router.ex:261: ExAdminDemo.Router.dispatch/2
        (ex_admin_demo) web/router.ex:1: ExAdminDemo.Router.do_call/2
        (ex_admin_demo) lib/ex_admin_demo/endpoint.ex:1: ExAdminDemo.Endpoint.phoenix_pipeline/1
        (ex_admin_demo) lib/plug/debugger.ex:93: ExAdminDemo.Endpoint."call (overridable 3)"/2
        (ex_admin_demo) lib/phoenix/endpoint/render_errors.ex:34: ExAdminDemo.Endpoint.call/2
        (plug) lib/plug/adapters/cowboy/handler.ex:15: Plug.Adapters.Cowboy.Handler.upgrade/4
        (cowboy) src/cowboy_protocol.erl:442: :cowboy_protocol.execute/4

The params being passed:

%{addresses_attributes: %{"1462724936019": %{_destroy: "0",
      phone_number: "1234", street: "xyz", type: "Work"}}, age: "29",
  name: "Bernardo"}

After solving this problem I will start probably here, am I on the right path?

Thanks for the hard work!

bernardoamc avatar May 08 '16 17:05 bernardoamc

@bernardoamc The issue you are facing is likely because you are using the has_many macro on simple one-to-many relationship. Unfortunately, this macro is a little misleading since It only supports many-to-many relationships through a join table. I'm working on issue #89 to support this, but its not done yet (very difficult feature).

The idea of this issue is to support the collection keyword list on a standard field, not an association. For example, if we have an address model/schema that has a type string field. An we want to allow a select box for the values in that field. Make sense.

So, to fix your problem. Just create a simple Address model with the string type field and try to get the above syntax working. Make sense?

Steve

smpallen99 avatar May 08 '16 19:05 smpallen99

@smpallen99 I see! That is indeed what I am doing, sorry to bother you with this problem. :)

Totally makes sense, I will change the app and try to solve this issue in the next few days. 👍

bernardoamc avatar May 08 '16 19:05 bernardoamc

Maybe I'm misunderstanding something, but the example below works correctly:

defmodule ExAdminDemo.ExAdmin.Address do
  use ExAdmin.Register

  register_resource ExAdminDemo.Address do
    form address do
      inputs do
        input address, :street
        input address, :phone_number
        input address, :type, collection: ["my house": "home", "my job": "work", "other type": "other"]
      end
    end
  end
end

With Schema:

  schema "addresses" do
    field :street, :string
    field :phone_number, :string
    field :type, :string

    timestamps
  end

bernardoamc avatar May 09 '16 02:05 bernardoamc

@smpallen99 If I'm right, the following syntax is already supported:

input address, :type, collection: [{"home", "Home"}, {"work", "Work"}, {"other", "Other"}]

isn't it?

romul avatar May 09 '16 11:05 romul

I thought I tried it the other day without success. but if your saying you tested it an it works, then perhaps I did something wrong. I guess thats a issue with not having tests. Perhaps you could add a test for this scenario.

Thanks, Steve

On May 9, 2016, at 7:29 AM, Roman Smirnov [email protected] wrote:

@smpallen99 https://github.com/smpallen99 If I'm right, the following syntax is already supported:

input address, :type, collection: [{"home", "Home"}, {"work", "Work"}, {"other", "Other"}] isn't it?

— You are receiving this because you were mentioned. Reply to this email directly or view it on GitHub https://github.com/smpallen99/ex_admin/issues/91#issuecomment-217840395

smpallen99 avatar May 09 '16 11:05 smpallen99

How can i use except in form. e.g. if i have 100 fields and i want to use 95, except 5 of the fields. how can i use except or escape few from the input fields?

zubairshokh avatar Apr 16 '18 18:04 zubairshokh