Hi ! ^^
Following your example I would say that that the request to “/” url that just list all post is a get
request.
get “/”, PostController, :index
If you want your action to be still requested at “/”, you will need to use another method than get
for the request, since the index
action of the PostController
will always match a get
request at “/”.
In fact Even though there was no route defined as a get
request at that path, I don’t find it really convenient to use the get
method for that. For example to delete a resource or upvote a post we don’t want to use just a get
request. Copy pasting the path in the browser adress bar should not be sufficient to trigger this kind of action. We want a button in a specific context to send that request.
So if we eliminate the get
method, we are left with 4 others: post
, put
, patch
, delete
.
If you opt for the post
:
post “/”, SomeController, :some_action
You will just put your button inside a form that will have “/” as action and post
as method.
For put
, patch
, delete
a simple link in your template with the right method will be sufficient. According to what you want you will chose on of them. For your specific example I would take either post
or patch
.
patch “/”, SomeController, :some_action
And in the template:
link(“Do some action”, to: “/”, method: :patch)
Since I started to learn some LiveView recently, I can add that if your button is in a live view a lot of all these considerations will be simplified. You could just pass an event through the socket and do whatever you want without the hassle of all those protocols. 