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

sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> somethi...
New
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
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
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
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
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
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
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
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New

We're in Beta

About us Mission Statement