Router "post" requests in same scope


#1

I currently have a scope in the router.ex that has 2 “post” calls using the same data but to different controllers. When I apply this into my templates (using the create method as the links on are the same page), it appears the router just selects the first in line and doesn’t execute the other router when I click that button: so for example:

===Router.ex===
scope “/post”, ExampleApp do
pipe_through [:browser]

post “/posts/:id” FavouriteController, :create
post “/posts/:id” LikeController, :create
end

===FavouriteController===
create (conn, %{“id” => id} do
user = current_user
post = Repo.get(Post, id)
params = %{user_id: user.id, post_id: post.id}

…standard changeset for create…
end

===LikeController===
create (conn, %{“id” => id} do
user = current_user
post = Repo.get(Post, id)
params = %{user_id: user.id, post_id: post.id}

…standard changeset for create…
end

===postdetail.html.eex===
<%= link “favourites”, to: favourite_path(@conn, :create, @post), method: “create” %>
<%= link “likes”, to: like_path(@conn, :create, @post), method: “create” %>

So because “favourites” is first in the router.ex file, when I click the like like to add it to my likes, it just adds it to my favourites instead and never makes it to the likes. Any thoughts on what I might be doing wrong? Thanks.


#2

Probably because the first match wins…

You could

post "/posts/:id" FavouriteController, :create
patch "/posts/:id" LikeController, :create

# or

post "/posts/:id/favourites" FavouriteController, :create
post "/posts/:id/likes" LikeController, :create

The latest being the most restful, I think…


#3

I tried the latter and it worked like a charm. Thanks!:grinning: