sangeethailango

sangeethailango

How to set value from url params without specifyng it in the request body?

I’m working with Ash and AshJsonApi and encountering an issue with how article_id is being handled in the URL parameters for my comment resource.

  actions do
    create :add_comment do
      accept [:comment, :article_id]

      change set_attribute(:article_id, arg(:article_id))
  end

My domain:

  json_api do
    routes do
      base_route "/articles", Article do
          base_route "/:article_id/add_comment", Comment do
            post :add_comment
        end
      end
    end
  end
end

I intended to use change set_attribute(:article_id, arg(:article_id)) in the add_comment action of the comment resource to automatically set the article_id from the URL parameters, without having to include it in the request body. However, I’m encountering an error where it says invalid_body and required properties are missing: channel_id

How can I configure Ash to use the article_id from the URL parameters for the add_comment action?

Most Liked

shankardevy

shankardevy

Assuming you want to create your comments by submitting POST request to /articles/:article_id/comments

Try changing this

actions do
    create :add_comment do
      accept [:comment, :article_id]

      change set_attribute(:article_id, arg(:article_id))
  end

to

actions do
    create :add_comment do
      argument :article_id, :uuid
      accept [:comment]

      change set_attribute(:article_id, arg(:article_id))
  end

Basically you set the arguments that you are accepting via URL placeholders via argument/2 and you set required body fields expected in your POST request via accept/1. Not sure if my interpretation is correct but that has worked for me.

shankardevy

shankardevy

on a different note, you might want to change the following

      base_route "/articles", Article do
          base_route "/:article_id/add_comment", Comment do
            post :add_comment
        end
      end

to

      base_route "/articles", Article do
          base_route "/:article_id/comments", Comment do
            post :add_comment
        end
      end

this would result in an url POST /api/articles/:id/comments for creating comments instead of POST /api/articles/:id/add_comments.

More than following some established conventions for the URLs, having the base_route as /:article_id/comments allows you to nest other actions like read :get_comment_by_id and get meaningful URLs. Otherwise as per your existing setup, a nested url to read comment would be GET api/articles/:article_id/add_comments/:id which is not meaningful.

Where Next?

Popular in Questions Top

nobody
How to bind a phoenix app to a specific ip address? could not find anything about that, nowhere, unfortunately, but for me this is quite...
New
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call t...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
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
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

Other popular topics Top

Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
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
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
klo
Got a question about when to concat vs. prepending items to list then reversing to achieve appending. So i know lists boil down to [1 | ...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 54250 245
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New

We're in Beta

About us Mission Statement