augnustin

augnustin

Phoenix.HTML.Link.button with params

Hello,

I’d like my button("content", to: url) to have extra hidden fields in it, like button("content", to: url, params: %{field: "value"}) is this possible?

The same question went in Rails: https://stackoverflow.com/questions/4886963/how-to-add-additional-params-to-a-button-to-form and it is now possible. :smile:

Cheers

First Post!

albydarned

albydarned

From the docs

button("hello", to: "/world", method: "get", class: "btn")
#=> <button class="btn" data-method="get" data-to="/world">hello</button>

Try

button("hello", to: "/world", method: "get", class: "btn", custom: "property")
#=> <button class="btn" data-method="get" data-to="/world" custom="property">hello</button>

Image from my test:
image

Most Liked

albydarned

albydarned

I see, so this button is in a form?

Have you considered adding properties as hidden inputs?

LostKobrakai

LostKobrakai

I’m not sure why this was added to rails, but conceptionally it seems quite strange to exist. GET parameters are part of the url, so I’m not sure why they should be added via options on the button. Phoenixs’ route helpers allow you to add GET parameters to urls as well as the URI module of the elixir core. If you’re looking for additional body params you’d need to wrap the button in a form to submit, which has some hidden inputs.

LostKobrakai

LostKobrakai

Phoenix’s button handling with method is mostly a convenience to not need to wrap just a button with a form, to defines nothing more than a url/method. If you need more data as part of the request use a form with hidden fields. You’re imho past the point there the convenience does make much sense.
For get requests you could alter the url, but a form will also just use get params if you submit it with the GET method. I guess both ways are viable for get parameters.

If you’d use form_for instead of content_tag(:form, …) you’d get the csrf token and the method override automatically and it would therefore work for any http method.

Last Post!

alexsysm

alexsysm

2 → link(“POST”, to: Routes.admin_user_path(@conn, :create, %{user: %{name: “alex”, uid: “alex@gmail”}}), method: :post, data: [confirm: “Are you sure?”], class: “btn btn-danger btn-xs”)

Where Next?

Popular in Questions Top

hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" =&gt; #BSON.ObjectId&lt;58eb1a7a9ad169198c3dXXXX&gt;, "email" =&gt; ...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

Other popular topics Top

baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 49266 226
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 40165 209
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New

We're in Beta

About us Mission Statement