greetings Phoenix community,
I am creating a new project with classic nested resources
Router
resources "/channels", ChannelController do
resources "/posts", PostController
end
I am now modifying post_controller.ex
and am having issues with new
and create
, index
is OK
def index(conn, %{"channel_id" => channel_id}) do
channel = Channels.get_channel!(channel_id)
render(conn, "index.html", channel: channel)
end
not sure I have new
correct
def new(conn, %{"channel_id" => id}) do
channel = Channels.get_channel!(id)
changeset = Channels.change_post(%Post{})
render(conn, "new.html", changeset: changeset, channel: channel)
end
The new form renders and when I post I get the create
error
new.html.heex
<h1>New Post</h1>
<%= render "form.html", changeset: @changeset,
action: Routes.channel_post_path(@conn, :create, @channel) %>
<span><%= link "Back", to: Routes.channel_path(@conn, :index) %></span>
and create
I think I get an error
def create(conn, params = %{"channel_id" => id, "post" => post_params}) do
case Channels.create_post(params) do
{:ok, post} ->
conn
|> put_flash(:info, "Post created successfully.")
|> redirect(to: Routes.post_path(conn, :show, post))
{:error, %Ecto.Changeset{} = changeset} ->
render(conn, "new.html", changeset: changeset)
end
end
get error
ArgumentError at POST /channels/1/posts
assign @channel not available in template
and iex
Request: POST /channels/1/posts
** (exit) an exception was raised:
** (ArgumentError) assign @channel not available in template.
Please make sure all proper assigns have been set. If you are
calling a component, make sure you are passing all required
assigns as arguments.
Available assigns: [:changeset, :conn, :current_user]
I couldn’t see any documentation on modifying the new
and create
for nested resources, which seems like it should be a foundation use case.
https://hexdocs.pm/phoenix/routing.html#nested-resources
apologies if I have missed something obvious
thankyou