NoRedInk/elm-rails/9.0.0

NOTE: the latest version is 9.0.1

Rails

Requests

get :
{ url : String
, expect : Expect msg
}
-> Cmd msg

Send a GET request to the given URL. Specify how to decode the response.

import Rails


getHats : Cmd msg
getHats =
    Rails.get
        { url = "https://example.com/hats"
        , expect = Rails.expectJson HandleGetHatsResponse hatsDecoder
        }
{ url : String
, body : Body
, expect : Expect msg
}
-> Cmd msg

Send a POST request to the given URL. Specify how to decode the response.

import Http
import Rails


createHat : Hat -> Cmd msg
createHat =
    Rails.post
        { url = "https://example.com/hats"
        , body = Http.jsonBody (encodeHat hat)
        , expect = Rails.expectJson HandleNewHatResponse hatDecoder
        }

NOTE: Rails typically expects an X-CSRF-Token header for POST requests, which this does not include. To have this header included automatically, add csrf-xhr to the page, before your Elm program gets initialized.

put :
{ url : String
, body : Body
, expect : Expect msg
}
-> Cmd msg

Send a PUT request to the given URL. Specify how to decode the response.

import Http
import Rails


updateHat : Hat -> Cmd msg
updateHat hat =
    Rails.put
        { url = "https://example.com/hats/" ++ String.fromInt hat.id
        , body = Http.jsonBody (encodeHat hat)
        , expect = Rails.expectJson HandleUpdatedHatResponse hatDecoder
        }

NOTE: Rails typically expects an X-CSRF-Token header for PUT requests, which this does not include. To have this header included automatically, add csrf-xhr to the page, before your Elm program gets initialized.

{ url : String
, body : Body
, expect : Expect msg
}
-> Cmd msg

Send a PATCH request to the given URL. Specify how to decode the response.

import Http
import Json.Encode exposing (object, string)
import Rails


updateHatDescription : Int -> String -> Cmd msg
updateHatDescription id description =
    Rails.patch
        { url = "https://example.com/hats/" ++ String.fromInt hat.id
        , body = Http.jsonBody (object [ ( "description", string description ) ])
        , expect = Rails.expectJson HandleUpdatedHatResponse hatDecoder
        }

NOTE: Rails typically expects an X-CSRF-Token header for PATCH requests, which this does not include. To have this header included automatically, add csrf-xhr to the page, before your Elm program gets initialized.

{ url : String
, body : Body
, expect : Expect msg
}
-> Cmd msg

Send a DELETE request to the given URL. Specify how to decode the response.

import Http
import Rails


destroyHat : Hat -> Cmd msg
destroyHat =
    Rails.delete
        { url = "https://example.com/hats/" ++ String.fromInt hat.id
        , body = Http.emptyBody
        , expect = Rails.expectEmptyBody HandleDeletedHatResponse
        }

NOTE: Rails typically expects an X-CSRF-Token header for DELETE requests, which this does not include. To have this header included automatically, add csrf-xhr to the page, before your Elm program gets initialized.

{ method : String
, headers : List Header
, url : String
, body : Body
, expect : Expect msg
, timeout : Maybe Float
, tracker : Maybe String
}
-> Cmd msg

Wraps Http.request while adding the following default headers:

  • Accept
    • for JSON: "application/json, text/javascript, */*; q=0.01"
    • for string: "*/*"
  • X-Requested-With - "XMLHttpRequest"

You can specify additional headers in the headers field of the configuration record. The delete example above would look lik this:

import Http
import Rails


destroyHat : Hat -> Cmd msg
destroyHat =
    Rails.request
        { method = "DELETE"
        , headers = []
        , url = "https://example.com/hats/" ++ String.fromInt hat.id
        , body = Http.emptyBody
        , expect = Rails.expectEmptyBody HandleDeletedHatResponse
        , timeout = Nothing
        , tracker = Nothing
        }

NOTE: Rails typically expects an X-CSRF-Token header for requests other than GET, which this does not include. One way to have this header included automatically is to add csrf-xhr to the page, before your Elm program gets initialized.

Expectations

type Expect msg

Expect that the response body will look a certain way. Similar to Http.Expect, but wrapped here so we know what headers to set in requests.

type Response error success
= Success success
| HttpError Error
| AppError Metadata error

The kinds of responses a Rails server may return.

expectString : (Result Error String -> msg) -> Expect msg

Expect Rails to return some string data. If this will be JSON, use expectJson instead!

expectEmptyBody : (Result Error () -> msg) -> Expect msg

Expect Rails to return an empty body. Note that we don't actually enforce the body is empty; we just discard it. Pairs well with delete.

expectJson : (Result Error success -> msg) -> Decoder success -> Expect msg

Expect Rails to return JSON.

(Response error success -> msg)
-> Decoder error
-> Decoder success
-> Expect msg

Decode Rails-specific error information from a BadStatus_ response (that is, a response whose status code is outside the 200 range.)

import Dict
import Http
import Json.Decode exposing (at, string)
import Json.Encode as Encode
import Rails
import Rails.Decode


createHat : Hat -> Cmd Msg
createHat hat =
    let
        errorsDecoder =
            Rails.Decode.decodeErrors (at [ "errors", "style" ] string)
    in
    Rails.post
        { url = "https://example.com/hats"
        , body = Http.jsonBody (encodeHat hat)
        , expect = Rails.expectJsonErrors HandleNewHatResponse errorsDecoder hatDecoder
        }