grych / drab

Remote controlled frontend framework for Phoenix.

Home Page:https://tg.pl/drab

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

(ArgumentError) assign @xxx not available in eex template.

joaquinalcerro opened this issue · comments

I am currently getting this alert error in the browser when I click the input checkbox:

"An error occured. Please contact the System Administrator"

20:12:06.347 [error] Drab Handler failed with the following exception:
** (ArgumentError) assign @changeset 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: [:action, :conn, :parent, :po_grantotal, :type, :view_module, :view_template]

    (phoenix_html) lib/phoenix_html/engine.ex:116: Phoenix.HTML.Engine.fetch_assign/2

    (epr) lib/epr_web/templates/payment_order/form.html.drab:1: EprWeb.PaymentOrderView."form.html"/1
    (epr) lib/epr_web/templates/payment_order/new.html.drab:6: EprWeb.PaymentOrderView."new.html"/1
    (phoenix) lib/phoenix/view.ex:332: Phoenix.View.render_to_iodata/3
    (phoenix) lib/phoenix/view.ex:339: Phoenix.View.render_to_string/3
    (drab) lib/drab/live.ex:674: Drab.Live.rerender_template/4
    (drab) lib/drab/live.ex:629: Drab.Live.process_poke/9
    (drab) lib/drab.ex:360: anonymous fn/7 in Drab.handle_event/6

Mi current configuration:

Drab 0.10.0
Phoenix 1.3.4
Phoenix_html 2.12.0 (currently locked due to fetch_assign/2 issue)

This is the code:

The commander (payment_order_commander.ex):

defmodule EprWeb.PaymentOrderCommander do
  use Drab.Commander

  defhandler invoice_selected(socket, _sender) do
    poke socket, po_grantotal: 1000 * 1
  end
end

The controller (payment_order_controller.ex):

defmodule EprWeb.PaymentOrderController do
  ........

  def new(conn, _params, %Epr.Debts.Contract{} = document) do
    invoices = Epr.Debts.list_unpaid_invoices(document)
    changeset =
        %PaymentOrder{}
        |> Payments.new_payment_order()

      render(
        conn,
        "new.html",
        changeset: changeset,
        parent: document,
        invoices: invoices,
        bank_accounts: Epr.Payments.list_all_bank_accounts(),
        currency_us: document.currency_us,
        po_grantotal: 0,
        type: "contract"
      )
  end

end

The main template (new.html.drab):

This template is the one the controller renders. I tried using the eex extension instead of drab and experience the same error.

<p class="title1">Nueva orden de pago</p>

render "form.html", Map.put(assigns, :action, contract_payment_order_path(@conn, :create, @parent))

The form (form.html.drab):

<%= form_for @changeset, @action, fn f -> %>
  <div class="row">
    <div class="col-md-12">
      <%= render "_invoices.html", conn: @conn, invoices: @invoices, parent: @parent %>
    </div>
.......
<% end %>

The first partial (_invoices.html.drab)

...........
  <tbody>
  <%= 
    case @parent do
      %Epr.Partners.Provider{} ->
        for i <- @invoices do
          render "_invoice_details.html",  invoice: i, currency: i.currency_us
        end

      _ ->
        for i <- @invoices do
          render "_invoice_details.html",  invoice: i, currency: @parent.currency_us
        end
    end
  %>
  </tbody>
</table>
.........

The second partial (_invoice_details.html.drab):

This partial has the input checkbox and drab-change event handeler.

<tr name="invoice">
  <td>
        <span class="text-right">
          <input type="hidden" name="invoices[<%= @invoice.id %>][paid]" value="false"> 
          <input type="checkbox" id="<%= @invoice.id %>" name="invoices[<%= @invoice.id %>][paid]" value="true" drab-change="invoice_selected" checked> 
        </span>
  </td>
  <td><%= @invoice.proforma_invoice_number %></td>
  <td><%= @invoice.exempt_order_number %></td>
  <td><%= @invoice.invoice_number %></td>
  <td><%= @invoice.date %></td>
  <td class="<%= active_icon(@invoice.exempt) %>"></td>
........

Thanks in advance for the help.

Best regards.

render "form.html", Map.put(assigns, :action, contract_payment_order_path(@conn, :create, @parent))

Is there the changeset already in the assigns variable? Could you share IO.inspect(assigns) just before render?

Here is the extract of the IO.inspect(assigns). It does include the changeset assign.

    adapter: {Plug.Adapters.Cowboy.Conn, :...},
    assigns: %{
      bank_accounts: [
        %Epr.Payments.BankAccount{
          __meta__: #Ecto.Schema.Metadata<:loaded, "bank_accounts">,
          active: true,
          bank: %Epr.Payments.Bank{
            __meta__: #Ecto.Schema.Metadata<:loaded, "banks">,
            active: true,
            bank_accounts: #Ecto.Association.NotLoaded<association :bank_accounts is not loaded>,
            id: 1,
            inserted_at: ~N[2018-11-09 01:03:24.408027],
            name: "Banco Atlantida",
            updated_at: ~N[2018-11-09 01:03:24.408060]
          },
          bank_id: 1,
          description: "Cuenta de Lemprias Principal",
          id: 1,
          inserted_at: ~N[2018-11-09 01:23:40.907474],
          num: "765239001",
          updated_at: ~N[2018-11-09 01:23:40.907495]
        }
      ],
      changeset: #Ecto.Changeset<
        action: nil,
        changes: %{},
        errors: [
          date: {"can't be blank", [validation: :required]},
          concept: {"can't be blank", [validation: :required]},
          check_number: {"can't be blank", [validation: :required]},
          bank_account_id: {"can't be blank", [validation: :required]}
        ],
        data: #Epr.Payments.PaymentOrder<>,
        valid?: false
      >,
      currency_us: false,
      current_user: %Epr.Accounts.User{
        __meta__: #Ecto.Schema.Metadata<:loaded, "users">,
        abr: nil,
        active: true,


Thanks for this. I still do not understand anyway.
Could you also past the whole new.html.drab as it is?

Thanks,

This is the complete code for the new.html.drab file:


<p class="title1">Nueva orden de pago</p>

<%= 
  case @type do
    "contract" ->
      IO.inspect(assigns)
      render "form.html", Map.put(assigns, :action, contract_payment_order_path(@conn, :create, @parent))
    "order" ->
      render "form.html", Map.put(assigns, :action, order_payment_order_path(@conn, :create, @parent))
    "provider" ->
      render "form.html", Map.put(assigns, :action, provider_payment_order_path(@conn, :create, @parent))
  end 
%>


I must say I am quite confused. It should not work, as you try to poke the assign po_grantotal to new.html. There is no such assign obviously, but the error is very confusing.

In Drab, if you want to poke an assign to the partial, you need to specify the partial and/or the view name.

poke socket, "partial.html", assign: 42

See https://hexdocs.pm/drab/Drab.Live.html#module-partials

Thanks for your support.

I checked the documenation and change my code. It is working.

Best regards

Reopening as the error message is misleading.