livebud / bud

The Full-Stack Web Framework for Go
MIT License
5.58k stars 179 forks source link

new controller: scaffolding nested resources needs improvement #209

Open matthewmueller opened 2 years ago

matthewmueller commented 2 years ago

The following command doesn't work yet:

bud new controller posts/comments index new

The problems are the following:

  1. Currently the generated view/posts/comments/index.svelte contains a missing comment variable:

    
    <script>
      export let comments = []
    </script>
    
    <h1>Comment Index</h1>
    
    <table border="1" cellpadding="10">
      {#if comments.length > 0}
        <thead>
          {#each Object.keys(comments[0]) as key}
            <th>{key}</th>
          {/each}
        </thead>
      {/if}
      {#each comments as comment}
        <tr>
          {#each Object.keys(comment) as key}
            {#if key.toLowerCase() === "id"}
              <td><a href={`/posts/${comment.post_id || 0}/comments/${comment.id || 0}`}>{comment[key]}</a></td>
            {:else}
              <td>{comment[key]}</td>
            {/if}
          {/each}
        </tr>
      {/each}
    </table>
    
    <br />
    
    <a href={`/posts/${comment.post_id || 0}/comments/new`}>New Comment</a>
    
    <style>
      table {
        border-collapse: collapse;
      }
    </style>

    Right now it's not clear how we should pass this through. Also open to other ideas like using the URL path instead.

  2. posts/comments/new.svelte has a similar problem. It generates a file that is missing comment variable. This variable isn't needed when the scaffolding is unnested.

    <h1>New Comment</h1>
    
    <form method="post" action={`/posts/${comment.post_id || 0}/comments`}>
      <!-- Add input fields here -->
      <input type="submit" value="Create Comment" />
    </form>
    
    <br />
    
    <a href={`/posts/${comment.post_id || 0}/comments`}>Back</a>

    In this case, just a bit more work needs to be done to conditionally introduce a script tag.