Reshuffle6795
Hello!
I’m trying to make sure a parameter in my Phoenix changeset can only be set by the server during a POST request.
I have a blogs table with a user_id foreign key referencing the user who created each blog post. I’m using the standard Phoenix auth generator. My goal is to set the user_id in the changeset based on the current_user (retrieved from the fetch_current_user plug), but I want to make sure users can’t manually set this user_id by tampering with the request.
Is the controller the correct place to set user_id in the changeset, and how can this be done securely and while ideally adhering to DRY?
I hope my question is understandable.
Thanks in advance for any help!
Trending in Questions
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
Hello,
I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
Other Trending Topics
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself.
My main conc...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #ai
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 8- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
kokolegorille
Hello and welcome,
You might use Ecto.build_assoc instead…
This change my create signature for blog, but I am sure user_id cannot be set via attrs
al2o3cr
A simple way is to build the struct with
user_idexplicitly, and not include it in thecastin the changeset:sodapopcan
Oooo! Ok, so I have always wondered this.
Is there any difference between
|> put_assoc(:foo, foo)and%__MODULE__{foo_id: foo.id}? There certainly is if you are doing an update since you then need%__MODULE__{foo | id: foo.id}but it’s still kind of the same thing.This is just one of those things that I’ve always wondered because I used to always not use
put_assocbut then eventually just started using it always. I have never encountered discrepancies between the two versions so I have never been able to confidently say that I know why I’m using one over the other.Nicodemus
You can also manually put changes on a changeset with
put_change/3.Reshuffle6795
Wow, I don’t know why I forgot about that, but you’re right, that seems to be by far the easiest way. Thank you!
Reshuffle6795
That also seems like a nice option, thanks!
Reshuffle6795
Alright, that also seems good, thanks!
al2o3cr
put_associs going to put the newfoo_idintochangeson the changeset, so that validations etc can see it - the bare struct update won’t do that.