Checking if an element is in database and rendering it in template

Hi,

I am calculating users score dynamically. The result i am rendering on the main page. However, the moment I create a new user I get an error - expected at least one result but got none in query. Therefor I guess I should use an if statement, however I am not sure what I am doing wrong. Any suggestions?

controller

def index(conn, _params, current_user) do
  ...
  user_score = Float.floor(Exercise.score_for_user(user_id, user_exercise)
  render(conn, "index.html", user_score: user_score)

template

<%= if @current_user do %>
  <%= if @user_score do %>
  <h2>User Score: <br> <%= @user_score %></h2>
  <% end %>
...
<% end %>

The problem is not here, but in the show action.

After create, it should redirect, but You need to show more of the controller actions

Thanks for the reply! :slight_smile:

The only action I have in this controller is this index action.

How do You create a user if You don’t have a create action?

Maybe You can show this…

I create him under different route in different controller.

Usually the error specify the file, and the line number.

You should show the full error stack.

expected at least one result but got none in query:

from p0 in Calorie.StudentProfile,
  where: p0.user_id == ^1,
  where: p0.id == ^1