HTML Helper Not Working

I borrowed this code from this web page.

  def pagination_text(list) do
    ~e"""
    Page: <%= comma_number(list.page) %> of <%= comma_number(calculate_pages(list.count, list.per_page)) %> - Records <%= list.first %> to <%= list.last %> of <%= comma_number(list.count) %>
    """
  end

  def pagination_links(conn, list, route) do
    content_tag :div, class: "pagination" do
      children = []
      if list.has_prev do
        children = children ++ link "Previous", to: route.(conn, :index, page: list.prev_page), class: "btn btn-secondary"
      end
      if list.has_next do
        children = children ++ link "Next", to: route.(conn, :index, page: list.next_page), class: "btn btn-secondary"
      end
      children
    end
  end

When I invoke the pagination_text function, it works :slight_smile:

<%= Ptrack.PaginationHelpers.pagination_text(@tickets) %>

When I invoke the pagination_links function, it generates an empty div :disappointed:

<%= Ptrack.PaginationHelpers.pagination_links(@conn, @tickets, &ticket_path/3) %>

If I ‘hard-code’ the guts of the pagination_links function in my view, it works :thinking:

<div class="padded-top padded-bottom">
  <%= if @tickets.has_prev do %>
    <span><%= link "⫷ Previous", to: ticket_path(@conn, :index, page: @tickets.prev_page), class: "btn btn-primary" %></span>
  <% end %>
  <%= if @tickets.has_next do %>
    <span><%= link "Next ⫸", to: ticket_path(@conn, :index, page: @tickets.next_page), class: "btn btn-primary" %></span>
  <% end %>
</div>

Can anyone spot what I am doing wrong?

You’re using if in an incorrect way for elixir: you cannot change variables of the scope outside of the if expression like that. You’d need to return something from the if expression.

I’ll put an example of a warning you should get when compiling this code:

Note variables defined inside case, cond, fn, if and similar do not leak. If you want to conditionally override an existing variable "params", you will have to explicitly return the variable. For example:

    if some_condition? do
      atom = :one
    else
      atom = :two
    end

should be written as

    atom =
      if some_condition? do
        :one
      else
        :two
      end
6 Likes

You may also want to consider using these packages… https://github.com/drewolson/scrivener_ecto and https://github.com/mgwidmann/scrivener_html

1 Like

I tried using the scrivener_ecto package the other day and went through a dependency hell which I eventually had to back out of to get my web application to compile again.

I’ll have a look at scrivener_html.

1 Like

That code came from someone else. I tried modifying the code as the compiler suggested. I got rid of the error but got a new one - something about ‘no file’ (I don’t remember the exact error that was displayed on the web page).

I scrolled through my terminal’s history and found this output. You can see that it was trying to generate the HTML elements but failed.

[error] #PID<0.5436.0> running PtrackWeb.Endpoint (cowboy_protocol) terminated
Server: localhost:4000 (http)
Request: GET /tickets
** (exit) an exception was raised:
    ** (ArgumentError) argument error
        :erlang.++(nil, {:safe, [60, "a", [[32, "class", 61, 34, "btn btn-secondary", 34], [32, "href", 61, 34, "/tickets?page=2", 34]], 62, "Next", 60, 47, "a", 62]})
        (ptrack) lib/ptrack/pagination_helpers.ex:32: Ptrack.PaginationHelpers.pagination_links/3
        (ptrack) lib/ptrack_web/templates/ticket/index.html.eex:47: PtrackWeb.TicketView."index.html"/1
        (ptrack) lib/ptrack_web/templates/layout/app.html.eex:40: PtrackWeb.LayoutView."app.html"/1
        (phoenix) lib/phoenix/view.ex:332: Phoenix.View.render_to_iodata/3
        (phoenix) lib/phoenix/controller.ex:740: Phoenix.Controller.do_render/4
        (ptrack) lib/ptrack_web/controllers/ticket_controller.ex:1: PtrackWeb.TicketController.action/2
        (ptrack) lib/ptrack_web/controllers/ticket_controller.ex:1: PtrackWeb.TicketController.phoenix_controller_pipeline/2
        (ptrack) lib/ptrack_web/endpoint.ex:1: PtrackWeb.Endpoint.instrument/4
        (phoenix) lib/phoenix/router.ex:278: Phoenix.Router.__call__/1
        (ptrack) lib/ptrack_web/endpoint.ex:1: PtrackWeb.Endpoint.plug_builder_call/2
        (ptrack) lib/plug/debugger.ex:122: PtrackWeb.Endpoint."call (overridable 3)"/2
        (ptrack) lib/ptrack_web/endpoint.ex:1: PtrackWeb.Endpoint.call/2
        (plug_cowboy) lib/plug/cowboy/handler.ex:18: Plug.Adapters.Cowboy.Handler.upgrade/4
        (cowboy) /Users/steve/Sites/ptrack/deps/cowboy/src/cowboy_protocol.erl:442: :cowboy_protocol.execute/4