PatrykosA

PatrykosA

API with update using jQuery.ajax

Hello,

I have a page with a button “Save” (#room-socket-save-changes) and a select (#room-socket-select) which contains id.
There’s a content of some files below.
router.ex

  pipeline :api do
    plug :accepts, ["json"]
  end

  scope "/api", MdbmsWeb, as: :api do
    pipe_through :api

    scope "/v1", Api.V1, as: :v1 do
      resources "/room_sockets", Room_socketController, only: [:index, :show, :update]
    end
  end

room_socket

  schema "room_sockets" do
    field :name, :string
    belongs_to :room, Mdbms.Room, foreign_key: :room_id
    has_one :connection, Mdbms.Connection

    timestamps()
  end

room_socket_controller.ex

  def update(conn, %{"id" => id, "name" => name, "room_id" => room_id}) do
    room_socket_params = %{"name" => name, "room_id" => room_id}
    room_socket = Repo.get!(Room_socket, id)
    changeset = Room_socket.changeset(room_socket, room_socket_params)

    case Repo.update(changeset) do
      {:ok, post} ->
        conn
        |> put_flash(:info, "Post updated successfully.")
      {:error, changeset} ->
        # render(conn, "edit.html", post: post, changeset: changeset)
    end
  end

app.js

  $('#room-socket-save-changes').on('click', function() {
    var room_socket_id = $('#room-socket-select').val();
    var name = $('#input-rs-name').val();
    var room_id = $('#select-rs-locations').val()

    $.ajax({
      type: 'PUT',
      url: '/api/v1/room_sockets/' + room_socket_id,
      data: {
        name: name,
        room_id: room_id
      },
      dataType: "json",
      success: function() {
        alert("success!");
      },
      error: function() {
        alert('error!');
      }
    });
  });

When I click button “Save” I get alert “error!” and an error shows in my terminal:

[error] #PID<0.463.0> running MdbmsWeb.Endpoint (cowboy_protocol) terminated
Server: localhost:4000 (http)
Request: PUT /api/v1/room_sockets/1
** (exit) an exception was raised:
    ** (ArgumentError) flash not fetched, call fetch_flash/2
        (phoenix) lib/phoenix/controller.ex:1236: Phoenix.Controller.get_flash/1
        (phoenix) lib/phoenix/controller.ex:1218: Phoenix.Controller.put_flash/3
        (mdbms) lib/mdbms_web/controllers/api/v1/room_socket_controller.ex:1: MdbmsWeb.Api.V1.Room_socketController.action/2
        (mdbms) lib/mdbms_web/controllers/api/v1/room_socket_controller.ex:1: MdbmsWeb.Api.V1.Room_socketController.phoenix_controller_pipeline/2
        (mdbms) lib/mdbms_web/endpoint.ex:1: MdbmsWeb.Endpoint.instrument/4
        (phoenix) lib/phoenix/router.ex:278: Phoenix.Router.__call__/1
        (mdbms) lib/mdbms_web/endpoint.ex:1: MdbmsWeb.Endpoint.plug_builder_call/2
        (mdbms) lib/plug/debugger.ex:122: MdbmsWeb.Endpoint."call (overridable 3)"/2
        (mdbms) lib/mdbms_web/endpoint.ex:1: MdbmsWeb.Endpoint.call/2
        (plug) lib/plug/adapters/cowboy/handler.ex:16: Plug.Adapters.Cowboy.Handler.upgrade/4
        (cowboy) /osA/Elixir/mdbms/deps/cowboy/src/cowboy_protocol.erl:442: :cowboy_protocol.execute/4

I’ve tried some solutions but the error still shows up. Do you have any idea where I make a mistake?

Marked As Solved

sanswork

sanswork

Try replacing

conn
|> put_flash(:info, “Post updated successfully.”)

in your controller

with

text conn, “OK”

Also Liked

sanswork

sanswork

Check out the network tab in your developer console to see whats happening with the request. It might just be that you need to change the text reply type to a json one as shown below. It’s probably on the jquery side though at that point.

text conn, “OK”
json conn, %{}

PatrykosA

PatrykosA

You’re right. I’ve changed that and it works fine now. Thanks again!

Last Post!

PatrykosA

PatrykosA

You’re right. I’ve changed that and it works fine now. Thanks again!

Where Next?

Popular in Questions Top

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
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
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
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
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New

Other popular topics Top

New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
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
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
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New

We're in Beta

About us Mission Statement