Run the Shiny application like so. The result is assigned to an
objected called results
so that you can save the results
that are produced in the web interface.
results <- specify_graph()
A new window in your system’s default web browser is opened with the following interface:
The instructions at the top provide details on how to specify the graph. The graph is divided into a left side and a right side. There is no confounding allowed between the left and right sides. All variables on the right side are confounded. Connections between the left and right sides must originate from the left. I.e, no connections from the right to left are allowed. On the left side, arbitrary connections are allowed and the model assumes that all nodes are observed and connected. On the right side, unmeasured variables are allowed, and the procedure assumes unmeasured confounding between all nodes. These restrictions ensure that the problem can be solved analytically using linear optimization.
In the main window below the text, Shift+click to add nodes. When a node is added, you are prompted to give it a name. Make sure that you use a valid R variable name, but without underscores. This means it cannot start with a number, and cannot contain spaces, underscores or special characters. Single capital letters are good choices, possibly with trailing numbers.
Click on an existing node to select it, and shift+click on a node to change the name. Shift+drag to connect nodes. Remember than no edges may go from the right side to the left side. Click a node to select and then press ‘u’ to mark it as unobserved/latent. Select a node and press a digit to set that number of possible categorical values (all variables default to binary), or press ‘c’ and enter a number into the prompt. Click to select nodes/edges and press delete to remove. If there are problems selecting nodes, try restarting your browser.
Once we have our graph specified, we can optionally make some annotations. You may specify the outcome of interest, and the exposure of interest. Select the outcome node and press ‘y’ to mark it as the outcome of interest, and then select the exposure node and press ‘e’ to mark it as the exposure of interest. The default causal contrast is the total effect. The exposure will be colored blue, and the outcome colored red.
We now have a graph specified that is ready to be analyzed.
Optionally, click an edge and press ‘m’ to enforce monotonicity for that
connection. If you are ready to analyze the object click on the “Press
to analyze the graph button”. The program interprets the graph, converts
it to an igraph
object with annotations, and prints the
result from R. Inspect the resulting graph carefully so that you
understand what is being analyzed. Note that if a side contains at least
two variables then a confounder has been automatically added for
them.
The next step is to use the text box to specify the causal effect of interest. In our current example, we are interested in the causal risk difference in Y comparing X = 1 to X = 0, i.e., Y(X = 1) - Y(X = 0). The optimizer will compute bounds for the expected value of this quantity.
Use the text box to describe your causal effect of interest. The effects must be of the form
p{V11(X=a)=a; V12(X=a)=b;...} op1 p{V21(X=b)=a; V22(X=c)=b;...} op2 ...
where Vij
are names of variables in the graph,
a, b
are corresponding values, and op
are
either -
or +
. You can specify a single
probability statement (i.e., no operator). Note that the probability
statements begin with little p, and use curly braces, and items inside
the probability statements are separated by ;
. The
variables may be potential outcomes which are denoted by parentheses.
Variables may also be nested inside potential outcomes. Pure
observations such as p{Y = 1}
are not allowed if the left
side contains any variables. If the left side contains any variables,
then they mush be ancestors of the intervention set variables (or the
intervention variables themselves). All of the following are valid
effect statements:
p{Y(X = 1) = 1} - p{Y(X = 0) = 1}
p{X(Z = 1) = 1; X(Z = 0) = 0}
p{Y(M(X = 0), X = 1) = 1} - p{Y(M(X = 0), X = 0) = 1}
Back to our example, we specify the effect as
p{Y(X = 1) = 1} - p{Y(X = 0) = 1}
then click the “parse” button to fix the effect.
Once the causal effect of interest has been specified, you may optionally specify some constraints. Click the button to start the constraint interface.
Here you can specify potential outcomes to constrain by writing the potential outcomes, values of their parents, and operators that determine the constraint (equalities or inequalities). In our example, we can specify monotonicity of the instrument Z by defining the constraint X(Z = 1) \(\geq\) X(Z = 0):
X(Z = 1) >= X(Z = 0)
then click parse.
Once you have inspected the graph, specified the causal effect, and understand what is being analyzed, press the final button to compute the bounds.
Note that the computation may take a significant amount of time.
The results are summarized in plain text. The bounds are expressed as
the minimum/maximum of a series of expressions involving the conditional
probabilities of the observed variables. For more details on the method
see this
paper. The final button allows you to return all of the objects back
to the R environment and store them in results
.
The results
object contains the graph, the
parameterization and interpretation of the graph in terms of the
variables and parameters, the bounds and log information about the
optimization procedure, and an R function that implements the
bounds:
names(results)
#> [1] "graphres" "obj" "bounds.obs" "constraints"
#> [5] "effect" "boundsFunction"
print(results$bounds.obs)
#> lower bound =
#> MAX {
#> p00_0 - p00_1 - p10_1 - p01_1
#> }
#> ----------------------------------------
#> upper bound =
#> MIN {
#> 1 - p10_1 - p01_0
#> }
print(results$boundsFunction)
#> function (p00_0 = NULL, p00_1 = NULL, p10_0 = NULL, p10_1 = NULL,
#> p01_0 = NULL, p01_1 = NULL, p11_0 = NULL, p11_1 = NULL)
#> {
#> lb <- pmax(p00_0 - p00_1 - p10_1 - p01_1)
#> ub <- pmin(1 - p10_1 - p01_0)
#> if (any(ub < lb)) {
#> warning("Invalid bounds! Data probably does not satisfy the assumptions in the DAG!")
#> }
#> data.frame(lower = lb, upper = ub)
#> }
The results object can also be used to numerically simulate the
bounds. Try using the simulate_bounds
function.
sim <- simulate_bounds(results$obj, results$bounds.obs, nsim = 100)
head(sim)
#> objective bound.lower bound.upper
#> 1 -0.121237265 -0.40481926 0.2048871
#> 2 0.050994423 -0.36204998 0.4019316
#> 3 0.168411283 -0.30054236 0.2586737
#> 4 0.224625020 -0.07575797 0.4496428
#> 5 -0.003303575 -0.35451552 0.3395537
#> 6 0.089360728 -0.27591653 0.3641212
We also provide a helper function to print the bounds as latex
equations. Use the latex_bounds
function.
cat(latex_bounds(results$bounds.obs$bounds, results$obj$parameters))
\[ \mbox{Lower bound} = P(X = 0, Y = 0 | Z = 0) - P(X = 0, Y = 0 | Z = 1) - P(X = 1, Y = 0 | Z = 1) - P(X = 0, Y = 1 | Z = 1) \] \[ \mbox{Upper bound} = 1 - P(X = 1, Y = 0 | Z = 1) - P(X = 0, Y = 1 | Z = 0) \]
Please sends any bugs or feedback to<EMAIL_ADDRESS>or file an issue on https://github.com/sachsmc/causaloptim .


Class PhoneControllerApi
Represents a collection of functions to interact with the API endpoints
Inherited Members
Namespace: mailslurp.Api
Assembly: mailslurp.dll
Syntax
public class PhoneControllerApi : IPhoneControllerApi, IPhoneControllerApiSync, IPhoneControllerApiAsync, IApiAccessor
Constructors
View SourcePhoneControllerApi()
Initializes a new instance of the PhoneControllerApi class.
Declaration
public PhoneControllerApi()
PhoneControllerApi(Configuration)
Initializes a new instance of the PhoneControllerApi class using Configuration object
Declaration
public PhoneControllerApi(Configuration configuration)
Parameters
| Type | Name | Description |
|---|---|---|
| Configuration | configuration | An instance of Configuration |
PhoneControllerApi(ISynchronousClient, IAsynchronousClient, IReadableConfiguration)
Initializes a new instance of the PhoneControllerApi class using a Configuration object and client instance.
Declaration
public PhoneControllerApi(ISynchronousClient client, IAsynchronousClient asyncClient, IReadableConfiguration configuration)
Parameters
| Type | Name | Description |
|---|---|---|
| ISynchronousClient | client | The client interface for synchronous API access. |
| IAsynchronousClient | asyncClient | The client interface for asynchronous API access. |
| IReadableConfiguration | configuration | The configuration object. |
PhoneControllerApi(string)
Initializes a new instance of the PhoneControllerApi class.
Declaration
public PhoneControllerApi(string basePath)
Parameters
| Type | Name | Description |
|---|---|---|
| string | basePath |
Properties
View SourceAsynchronousClient
The client for accessing this underlying API asynchronously.
Declaration
public IAsynchronousClient AsynchronousClient { get; set; }
Property Value
| Type | Description |
|---|---|
| IAsynchronousClient |
Client
The client for accessing this underlying API synchronously.
Declaration
public ISynchronousClient Client { get; set; }
Property Value
| Type | Description |
|---|---|
| ISynchronousClient |
Configuration
Gets or sets the configuration object
Declaration
public IReadableConfiguration Configuration { get; set; }
Property Value
| Type | Description |
|---|---|
| IReadableConfiguration | An instance of the Configuration |
ExceptionFactory
Provides a factory method hook for the creation of exceptions.
Declaration
public ExceptionFactory ExceptionFactory { get; set; }
Property Value
| Type | Description |
|---|---|
| ExceptionFactory |
Methods
View SourceCreateEmergencyAddress(CreateEmergencyAddressOptions, int)
Declaration
public EmergencyAddress CreateEmergencyAddress(CreateEmergencyAddressOptions createEmergencyAddressOptions, int operationIndex = 0)
Parameters
| Type | Name | Description |
|---|---|---|
| CreateEmergencyAddressOptions | createEmergencyAddressOptions | |
| int | operationIndex | Index associated with the operation. |
Returns
| Type | Description |
|---|---|
| EmergencyAddress | EmergencyAddress |
Exceptions
| Type | Condition |
|---|---|
| ApiException | Thrown when fails to make API call |
CreateEmergencyAddressAsync(CreateEmergencyAddressOptions, int, CancellationToken)
Declaration
public Task<EmergencyAddress> CreateEmergencyAddressAsync(CreateEmergencyAddressOptions createEmergencyAddressOptions, int operationIndex = 0, CancellationToken cancellationToken = default)
Parameters
| Type | Name | Description |
|---|---|---|
| CreateEmergencyAddressOptions | createEmergencyAddressOptions | |
| int | operationIndex | Index associated with the operation. |
| CancellationToken | cancellationToken | Cancellation Token to cancel the request. |
Returns
| Type | Description |
|---|---|
| Task<EmergencyAddress> | Task of EmergencyAddress |
Exceptions
| Type | Condition |
|---|---|
| ApiException | Thrown when fails to make API call |
CreateEmergencyAddressWithHttpInfo(CreateEmergencyAddressOptions, int)
Declaration
public ApiResponse<EmergencyAddress> CreateEmergencyAddressWithHttpInfo(CreateEmergencyAddressOptions createEmergencyAddressOptions, int operationIndex = 0)
Parameters
| Type | Name | Description |
|---|---|---|
| CreateEmergencyAddressOptions | createEmergencyAddressOptions | |
| int | operationIndex | Index associated with the operation. |
Returns
| Type | Description |
|---|---|
| ApiResponse<EmergencyAddress> | ApiResponse of EmergencyAddress |
Exceptions
| Type | Condition |
|---|---|
| ApiException | Thrown when fails to make API call |
CreateEmergencyAddressWithHttpInfoAsync(CreateEmergencyAddressOptions, int, CancellationToken)
Declaration
public Task<ApiResponse<EmergencyAddress>> CreateEmergencyAddressWithHttpInfoAsync(CreateEmergencyAddressOptions createEmergencyAddressOptions, int operationIndex = 0, CancellationToken cancellationToken = default)
Parameters
| Type | Name | Description |
|---|---|---|
| CreateEmergencyAddressOptions | createEmergencyAddressOptions | |
| int | operationIndex | Index associated with the operation. |
| CancellationToken | cancellationToken | Cancellation Token to cancel the request. |
Returns
| Type | Description |
|---|---|
| Task<ApiResponse<EmergencyAddress>> | Task of ApiResponse (EmergencyAddress) |
Exceptions
| Type | Condition |
|---|---|
| ApiException | Thrown when fails to make API call |
DeleteEmergencyAddress(Guid, int)
Declaration
public EmptyResponseDto DeleteEmergencyAddress(Guid addressId, int operationIndex = 0)
Parameters
| Type | Name | Description |
|---|---|---|
| Guid | addressId | |
| int | operationIndex | Index associated with the operation. |
Returns
| Type | Description |
|---|---|
| EmptyResponseDto | EmptyResponseDto |
Exceptions
| Type | Condition |
|---|---|
| ApiException | Thrown when fails to make API call |
DeleteEmergencyAddressAsync(Guid, int, CancellationToken)
Declaration
public Task<EmptyResponseDto> DeleteEmergencyAddressAsync(Guid addressId, int operationIndex = 0, CancellationToken cancellationToken = default)
Parameters
| Type | Name | Description |
|---|---|---|
| Guid | addressId | |
| int | operationIndex | Index associated with the operation. |
| CancellationToken | cancellationToken | Cancellation Token to cancel the request. |
Returns
| Type | Description |
|---|---|
| Task<EmptyResponseDto> | Task of EmptyResponseDto |
Exceptions
| Type | Condition |
|---|---|
| ApiException | Thrown when fails to make API call |
DeleteEmergencyAddressWithHttpInfo(Guid, int)
Declaration
public ApiResponse<EmptyResponseDto> DeleteEmergencyAddressWithHttpInfo(Guid addressId, int operationIndex = 0)
Parameters
| Type | Name | Description |
|---|---|---|
| Guid | addressId | |
| int | operationIndex | Index associated with the operation. |
Returns
| Type | Description |
|---|---|
| ApiResponse<EmptyResponseDto> | ApiResponse of EmptyResponseDto |
Exceptions
| Type | Condition |
|---|---|
| ApiException | Thrown when fails to make API call |
DeleteEmergencyAddressWithHttpInfoAsync(Guid, int, CancellationToken)
Declaration
public Task<ApiResponse<EmptyResponseDto>> DeleteEmergencyAddressWithHttpInfoAsync(Guid addressId, int operationIndex = 0, CancellationToken cancellationToken = default)
Parameters
| Type | Name | Description |
|---|---|---|
| Guid | addressId | |
| int | operationIndex | Index associated with the operation. |
| CancellationToken | cancellationToken | Cancellation Token to cancel the request. |
Returns
| Type | Description |
|---|---|
| Task<ApiResponse<EmptyResponseDto>> | Task of ApiResponse (EmptyResponseDto) |
Exceptions
| Type | Condition |
|---|---|
| ApiException | Thrown when fails to make API call |
DeletePhoneNumber(Guid, int)
Declaration
public void DeletePhoneNumber(Guid phoneNumberId, int operationIndex = 0)
Parameters
| Type | Name | Description |
|---|---|---|
| Guid | phoneNumberId | |
| int | operationIndex | Index associated with the operation. |
Exceptions
| Type | Condition |
|---|---|
| ApiException | Thrown when fails to make API call |
DeletePhoneNumberAsync(Guid, int, CancellationToken)
Declaration
public Task DeletePhoneNumberAsync(Guid phoneNumberId, int operationIndex = 0, CancellationToken cancellationToken = default)
Parameters
| Type | Name | Description |
|---|---|---|
| Guid | phoneNumberId | |
| int | operationIndex | Index associated with the operation. |
| CancellationToken | cancellationToken | Cancellation Token to cancel the request. |
Returns
| Type | Description |
|---|---|
| Task | Task of void |
Exceptions
| Type | Condition |
|---|---|
| ApiException | Thrown when fails to make API call |
DeletePhoneNumberWithHttpInfo(Guid, int)
Declaration
public ApiResponse<object> DeletePhoneNumberWithHttpInfo(Guid phoneNumberId, int operationIndex = 0)
Parameters
| Type | Name | Description |
|---|---|---|
| Guid | phoneNumberId | |
| int | operationIndex | Index associated with the operation. |
Returns
| Type | Description |
|---|---|
| ApiResponse<object> | ApiResponse of Object(void) |
Exceptions
| Type | Condition |
|---|---|
| ApiException | Thrown when fails to make API call |
DeletePhoneNumberWithHttpInfoAsync(Guid, int, CancellationToken)
Declaration
public Task<ApiResponse<object>> DeletePhoneNumberWithHttpInfoAsync(Guid phoneNumberId, int operationIndex = 0, CancellationToken cancellationToken = default)
Parameters
| Type | Name | Description |
|---|---|---|
| Guid | phoneNumberId | |
| int | operationIndex | Index associated with the operation. |
| CancellationToken | cancellationToken | Cancellation Token to cancel the request. |
Returns
| Type | Description |
|---|---|
| Task<ApiResponse<object>> | Task of ApiResponse |
Exceptions
| Type | Condition |
|---|---|
| ApiException | Thrown when fails to make API call |
GetBasePath()
Gets the base path of the API client.
Declaration
public string GetBasePath()
Returns
| Type | Description |
|---|---|
| string | The base path |
GetEmergencyAddress(Guid, int)
Declaration
public EmergencyAddress GetEmergencyAddress(Guid addressId, int operationIndex = 0)
Parameters
| Type | Name | Description |
|---|---|---|
| Guid | addressId | |
| int | operationIndex | Index associated with the operation. |
Returns
| Type | Description |
|---|---|
| EmergencyAddress | EmergencyAddress |
Exceptions
| Type | Condition |
|---|---|
| ApiException | Thrown when fails to make API call |
GetEmergencyAddressAsync(Guid, int, CancellationToken)
Declaration
public Task<EmergencyAddress> GetEmergencyAddressAsync(Guid addressId, int operationIndex = 0, CancellationToken cancellationToken = default)
Parameters
| Type | Name | Description |
|---|---|---|
| Guid | addressId | |
| int | operationIndex | Index associated with the operation. |
| CancellationToken | cancellationToken | Cancellation Token to cancel the request. |
Returns
| Type | Description |
|---|---|
| Task<EmergencyAddress> | Task of EmergencyAddress |
Exceptions
| Type | Condition |
|---|---|
| ApiException | Thrown when fails to make API call |
GetEmergencyAddresses(int)
Declaration
public List<EmergencyAddressDto> GetEmergencyAddresses(int operationIndex = 0)
Parameters
| Type | Name | Description |
|---|---|---|
| int | operationIndex | Index associated with the operation. |
Returns
| Type | Description |
|---|---|
| List<EmergencyAddressDto> | List<EmergencyAddressDto> |
Exceptions
| Type | Condition |
|---|---|
| ApiException | Thrown when fails to make API call |
GetEmergencyAddressesAsync(int, CancellationToken)
Declaration
public Task<List<EmergencyAddressDto>> GetEmergencyAddressesAsync(int operationIndex = 0, CancellationToken cancellationToken = default)
Parameters
| Type | Name | Description |
|---|---|---|
| int | operationIndex | Index associated with the operation. |
| CancellationToken | cancellationToken | Cancellation Token to cancel the request. |
Returns
| Type | Description |
|---|---|
| Task<List<EmergencyAddressDto>> | Task of List<EmergencyAddressDto> |
Exceptions
| Type | Condition |
|---|---|
| ApiException | Thrown when fails to make API call |
GetEmergencyAddressesWithHttpInfo(int)
Declaration
public ApiResponse<List<EmergencyAddressDto>> GetEmergencyAddressesWithHttpInfo(int operationIndex = 0)
Parameters
| Type | Name | Description |
|---|---|---|
| int | operationIndex | Index associated with the operation. |
Returns
| Type | Description |
|---|---|
| ApiResponse<List<EmergencyAddressDto>> | ApiResponse of List<EmergencyAddressDto> |
Exceptions
| Type | Condition |
|---|---|
| ApiException | Thrown when fails to make API call |
GetEmergencyAddressesWithHttpInfoAsync(int, CancellationToken)
Declaration
public Task<ApiResponse<List<EmergencyAddressDto>>> GetEmergencyAddressesWithHttpInfoAsync(int operationIndex = 0, CancellationToken cancellationToken = default)
Parameters
| Type | Name | Description |
|---|---|---|
| int | operationIndex | Index associated with the operation. |
| CancellationToken | cancellationToken | Cancellation Token to cancel the request. |
Returns
| Type | Description |
|---|---|
| Task<ApiResponse<List<EmergencyAddressDto>>> | Task of ApiResponse (List<EmergencyAddressDto>) |
Exceptions
| Type | Condition |
|---|---|
| ApiException | Thrown when fails to make API call |
GetEmergencyAddressWithHttpInfo(Guid, int)
Declaration
public ApiResponse<EmergencyAddress> GetEmergencyAddressWithHttpInfo(Guid addressId, int operationIndex = 0)
Parameters
| Type | Name | Description |
|---|---|---|
| Guid | addressId | |
| int | operationIndex | Index associated with the operation. |
Returns
| Type | Description |
|---|---|
| ApiResponse<EmergencyAddress> | ApiResponse of EmergencyAddress |
Exceptions
| Type | Condition |
|---|---|
| ApiException | Thrown when fails to make API call |
GetEmergencyAddressWithHttpInfoAsync(Guid, int, CancellationToken)
Declaration
public Task<ApiResponse<EmergencyAddress>> GetEmergencyAddressWithHttpInfoAsync(Guid addressId, int operationIndex = 0, CancellationToken cancellationToken = default)
Parameters
| Type | Name | Description |
|---|---|---|
| Guid | addressId | |
| int | operationIndex | Index associated with the operation. |
| CancellationToken | cancellationToken | Cancellation Token to cancel the request. |
Returns
| Type | Description |
|---|---|
| Task<ApiResponse<EmergencyAddress>> | Task of ApiResponse (EmergencyAddress) |
Exceptions
| Type | Condition |
|---|---|
| ApiException | Thrown when fails to make API call |
GetPhoneNumber(Guid, int)
Declaration
public PhoneNumberDto GetPhoneNumber(Guid phoneNumberId, int operationIndex = 0)
Parameters
| Type | Name | Description |
|---|---|---|
| Guid | phoneNumberId | |
| int | operationIndex | Index associated with the operation. |
Returns
| Type | Description |
|---|---|
| PhoneNumberDto | PhoneNumberDto |
Exceptions
| Type | Condition |
|---|---|
| ApiException | Thrown when fails to make API call |
GetPhoneNumberAsync(Guid, int, CancellationToken)
Declaration
public Task<PhoneNumberDto> GetPhoneNumberAsync(Guid phoneNumberId, int operationIndex = 0, CancellationToken cancellationToken = default)
Parameters
| Type | Name | Description |
|---|---|---|
| Guid | phoneNumberId | |
| int | operationIndex | Index associated with the operation. |
| CancellationToken | cancellationToken | Cancellation Token to cancel the request. |
Returns
| Type | Description |
|---|---|
| Task<PhoneNumberDto> | Task of PhoneNumberDto |
Exceptions
| Type | Condition |
|---|---|
| ApiException | Thrown when fails to make API call |
GetPhoneNumbers(string, int?, int?, string, DateTime?, DateTime?, int)
Declaration
public PagePhoneNumberProjection GetPhoneNumbers(string phoneCountry = null, int? page = null, int? size = null, string sort = null, DateTime? since = null, DateTime? before = null, int operationIndex = 0)
Parameters
| Type | Name | Description |
|---|---|---|
| string | phoneCountry | Optional phone country (optional) |
| int? | page | Optional page index for list pagination (optional, default to 0) |
| int? | size | Optional page size for list pagination (optional, default to 20) |
| string | sort | Optional createdAt sort direction ASC or DESC (optional, default to ASC) |
| DateTime? | since | Filter by created at after the given timestamp (optional) |
| DateTime? | before | Filter by created at before the given timestamp (optional) |
| int | operationIndex | Index associated with the operation. |
Returns
| Type | Description |
|---|---|
| PagePhoneNumberProjection | PagePhoneNumberProjection |
Exceptions
| Type | Condition |
|---|---|
| ApiException | Thrown when fails to make API call |
GetPhoneNumbersAsync(string, int?, int?, string, DateTime?, DateTime?, int, CancellationToken)
Declaration
public Task<PagePhoneNumberProjection> GetPhoneNumbersAsync(string phoneCountry = null, int? page = null, int? size = null, string sort = null, DateTime? since = null, DateTime? before = null, int operationIndex = 0, CancellationToken cancellationToken = default)
Parameters
| Type | Name | Description |
|---|---|---|
| string | phoneCountry | Optional phone country (optional) |
| int? | page | Optional page index for list pagination (optional, default to 0) |
| int? | size | Optional page size for list pagination (optional, default to 20) |
| string | sort | Optional createdAt sort direction ASC or DESC (optional, default to ASC) |
| DateTime? | since | Filter by created at after the given timestamp (optional) |
| DateTime? | before | Filter by created at before the given timestamp (optional) |
| int | operationIndex | Index associated with the operation. |
| CancellationToken | cancellationToken | Cancellation Token to cancel the request. |
Returns
| Type | Description |
|---|---|
| Task<PagePhoneNumberProjection> | Task of PagePhoneNumberProjection |
Exceptions
| Type | Condition |
|---|---|
| ApiException | Thrown when fails to make API call |
GetPhoneNumbersWithHttpInfo(string, int?, int?, string, DateTime?, DateTime?, int)
Declaration
public ApiResponse<PagePhoneNumberProjection> GetPhoneNumbersWithHttpInfo(string phoneCountry = null, int? page = null, int? size = null, string sort = null, DateTime? since = null, DateTime? before = null, int operationIndex = 0)
Parameters
| Type | Name | Description |
|---|---|---|
| string | phoneCountry | Optional phone country (optional) |
| int? | page | Optional page index for list pagination (optional, default to 0) |
| int? | size | Optional page size for list pagination (optional, default to 20) |
| string | sort | Optional createdAt sort direction ASC or DESC (optional, default to ASC) |
| DateTime? | since | Filter by created at after the given timestamp (optional) |
| DateTime? | before | Filter by created at before the given timestamp (optional) |
| int | operationIndex | Index associated with the operation. |
Returns
| Type | Description |
|---|---|
| ApiResponse<PagePhoneNumberProjection> | ApiResponse of PagePhoneNumberProjection |
Exceptions
| Type | Condition |
|---|---|
| ApiException | Thrown when fails to make API call |
GetPhoneNumbersWithHttpInfoAsync(string, int?, int?, string, DateTime?, DateTime?, int, CancellationToken)
Declaration
public Task<ApiResponse<PagePhoneNumberProjection>> GetPhoneNumbersWithHttpInfoAsync(string phoneCountry = null, int? page = null, int? size = null, string sort = null, DateTime? since = null, DateTime? before = null, int operationIndex = 0, CancellationToken cancellationToken = default)
Parameters
| Type | Name | Description |
|---|---|---|
| string | phoneCountry | Optional phone country (optional) |
| int? | page | Optional page index for list pagination (optional, default to 0) |
| int? | size | Optional page size for list pagination (optional, default to 20) |
| string | sort | Optional createdAt sort direction ASC or DESC (optional, default to ASC) |
| DateTime? | since | Filter by created at after the given timestamp (optional) |
| DateTime? | before | Filter by created at before the given timestamp (optional) |
| int | operationIndex | Index associated with the operation. |
| CancellationToken | cancellationToken | Cancellation Token to cancel the request. |
Returns
| Type | Description |
|---|---|
| Task<ApiResponse<PagePhoneNumberProjection>> | Task of ApiResponse (PagePhoneNumberProjection) |
Exceptions
| Type | Condition |
|---|---|
| ApiException | Thrown when fails to make API call |
GetPhoneNumberWithHttpInfo(Guid, int)
Declaration
public ApiResponse<PhoneNumberDto> GetPhoneNumberWithHttpInfo(Guid phoneNumberId, int operationIndex = 0)
Parameters
| Type | Name | Description |
|---|---|---|
| Guid | phoneNumberId | |
| int | operationIndex | Index associated with the operation. |
Returns
| Type | Description |
|---|---|
| ApiResponse<PhoneNumberDto> | ApiResponse of PhoneNumberDto |
Exceptions
| Type | Condition |
|---|---|
| ApiException | Thrown when fails to make API call |
GetPhoneNumberWithHttpInfoAsync(Guid, int, CancellationToken)
Declaration
public Task<ApiResponse<PhoneNumberDto>> GetPhoneNumberWithHttpInfoAsync(Guid phoneNumberId, int operationIndex = 0, CancellationToken cancellationToken = default)
Parameters
| Type | Name | Description |
|---|---|---|
| Guid | phoneNumberId | |
| int | operationIndex | Index associated with the operation. |
| CancellationToken | cancellationToken | Cancellation Token to cancel the request. |
Returns
| Type | Description |
|---|---|
| Task<ApiResponse<PhoneNumberDto>> | Task of ApiResponse (PhoneNumberDto) |
Exceptions
| Type | Condition |
|---|---|
| ApiException | Thrown when fails to make API call |
GetPhonePlans(int)
Declaration
public List<PhonePlanDto> GetPhonePlans(int operationIndex = 0)
Parameters
| Type | Name | Description |
|---|---|---|
| int | operationIndex | Index associated with the operation. |
Returns
| Type | Description |
|---|---|
| List<PhonePlanDto> | List<PhonePlanDto> |
Exceptions
| Type | Condition |
|---|---|
| ApiException | Thrown when fails to make API call |
GetPhonePlansAsync(int, CancellationToken)
Declaration
public Task<List<PhonePlanDto>> GetPhonePlansAsync(int operationIndex = 0, CancellationToken cancellationToken = default)
Parameters
| Type | Name | Description |
|---|---|---|
| int | operationIndex | Index associated with the operation. |
| CancellationToken | cancellationToken | Cancellation Token to cancel the request. |
Returns
| Type | Description |
|---|---|
| Task<List<PhonePlanDto>> | Task of List<PhonePlanDto> |
Exceptions
| Type | Condition |
|---|---|
| ApiException | Thrown when fails to make API call |
GetPhonePlansWithHttpInfo(int)
Declaration
public ApiResponse<List<PhonePlanDto>> GetPhonePlansWithHttpInfo(int operationIndex = 0)
Parameters
| Type | Name | Description |
|---|---|---|
| int | operationIndex | Index associated with the operation. |
Returns
| Type | Description |
|---|---|
| ApiResponse<List<PhonePlanDto>> | ApiResponse of List<PhonePlanDto> |
Exceptions
| Type | Condition |
|---|---|
| ApiException | Thrown when fails to make API call |
GetPhonePlansWithHttpInfoAsync(int, CancellationToken)
Declaration
public Task<ApiResponse<List<PhonePlanDto>>> GetPhonePlansWithHttpInfoAsync(int operationIndex = 0, CancellationToken cancellationToken = default)
Parameters
| Type | Name | Description |
|---|---|---|
| int | operationIndex | Index associated with the operation. |
| CancellationToken | cancellationToken | Cancellation Token to cancel the request. |
Returns
| Type | Description |
|---|---|
| Task<ApiResponse<List<PhonePlanDto>>> | Task of ApiResponse (List<PhonePlanDto>) |
Exceptions
| Type | Condition |
|---|---|
| ApiException | Thrown when fails to make API call |
TestPhoneNumberSendSms(Guid, TestPhoneNumberOptions, string, int)
Declaration
public void TestPhoneNumberSendSms(Guid phoneNumberId, TestPhoneNumberOptions testPhoneNumberOptions, string xTestId = null, int operationIndex = 0)
Parameters
| Type | Name | Description |
|---|---|---|
| Guid | phoneNumberId | |
| TestPhoneNumberOptions | testPhoneNumberOptions | |
| string | xTestId | (optional) |
| int | operationIndex | Index associated with the operation. |
Exceptions
| Type | Condition |
|---|---|
| ApiException | Thrown when fails to make API call |
TestPhoneNumberSendSmsAsync(Guid, TestPhoneNumberOptions, string, int, CancellationToken)
Declaration
public Task TestPhoneNumberSendSmsAsync(Guid phoneNumberId, TestPhoneNumberOptions testPhoneNumberOptions, string xTestId = null, int operationIndex = 0, CancellationToken cancellationToken = default)
Parameters
| Type | Name | Description |
|---|---|---|
| Guid | phoneNumberId | |
| TestPhoneNumberOptions | testPhoneNumberOptions | |
| string | xTestId | (optional) |
| int | operationIndex | Index associated with the operation. |
| CancellationToken | cancellationToken | Cancellation Token to cancel the request. |
Returns
| Type | Description |
|---|---|
| Task | Task of void |
Exceptions
| Type | Condition |
|---|---|
| ApiException | Thrown when fails to make API call |
TestPhoneNumberSendSmsWithHttpInfo(Guid, TestPhoneNumberOptions, string, int)
Declaration
public ApiResponse<object> TestPhoneNumberSendSmsWithHttpInfo(Guid phoneNumberId, TestPhoneNumberOptions testPhoneNumberOptions, string xTestId = null, int operationIndex = 0)
Parameters
| Type | Name | Description |
|---|---|---|
| Guid | phoneNumberId | |
| TestPhoneNumberOptions | testPhoneNumberOptions | |
| string | xTestId | (optional) |
| int | operationIndex | Index associated with the operation. |
Returns
| Type | Description |
|---|---|
| ApiResponse<object> | ApiResponse of Object(void) |
Exceptions
| Type | Condition |
|---|---|
| ApiException | Thrown when fails to make API call |
TestPhoneNumberSendSmsWithHttpInfoAsync(Guid, TestPhoneNumberOptions, string, int, CancellationToken)
Declaration
public Task<ApiResponse<object>> TestPhoneNumberSendSmsWithHttpInfoAsync(Guid phoneNumberId, TestPhoneNumberOptions testPhoneNumberOptions, string xTestId = null, int operationIndex = 0, CancellationToken cancellationToken = default)
Parameters
| Type | Name | Description |
|---|---|---|
| Guid | phoneNumberId | |
| TestPhoneNumberOptions | testPhoneNumberOptions | |
| string | xTestId | (optional) |
| int | operationIndex | Index associated with the operation. |
| CancellationToken | cancellationToken | Cancellation Token to cancel the request. |
Returns
| Type | Description |
|---|---|
| Task<ApiResponse<object>> | Task of ApiResponse |
Exceptions
| Type | Condition |
|---|---|
| ApiException | Thrown when fails to make API call |


The NP241 transfer case is a chain driven transfer case in an aluminum case used in 1988+ Dodge trucks and Ramchargers. This transfer case is also used in GM trucks after 1988.


FSM Designer
Your browser does not support
the HTML5 <canvas> element
Selected
+
Apply
Reset
Clear
Compile
×


{% extends "base.html" %} {% block content %}
Yes, it was a drawing.
The drawing was . More luck next time.


Australia: Rural Victoria Is Covered In Orange Glow After Being Hit With Dust Storm
by Bhakti HargunaniRest of the World News
Pictures of rural Victoria in Australia have been uploaded to social media after a dust storm hit parts of the region on November 21
Written By Bhakti Hargunani | Mumbai | Updated On: November 23, 2019 06:02 IST
Pictures of rural Victoria in Australia have been uploaded to social media after a dust storm hit parts of the region on 21st November. The area now looks like Mars or some scene from an apocalyptic sci-fi movie after being covered in an orange glow.
According to the Bureau of Meteorology, Mildura witnessed an eight-degree drop in its temperature in just 30 minutes as the area dived into an orange glow. The visibility of the area also dropped considerably as the dust storm hit, with the prevailing visibility at the Airport dropping from four kilometres to 500 metres.
Also Read: Smoke Blankets Sydney As Wildfires Spread Across Australia
Code Red alarm
A Code Red alarm has been alerted in parts of the state. Code Red is an alarming fire threat level that has last been seen in 2010.
Country Fire Authority Chief Officer Steve Warrington has warned people to be prepared as it will be too late to make a decision once the fire reaches homes. Areas like Mallee and Northern Country regions have also been asked to prepare for the worst.
Warrington warned people that if they were there when a fire occurs, they definitely will not survive. He added that once the fire occurs, there was a high chance that homes would be destroyed.
 Crisp added that the fires, when they happen, would be fast-moving, unpredictable, and uncontrollable.
Also Read: Woman Risks Her Life To Save Koala From Wildfire In Australia
Evacuate your homes
The Commissioner warned people that the houses in the region are not strong enough to withstand such severe conditions, emphasizing on the need for people to evacuate their homes. He also laid out some options for people to consider going to in order to leave their homes, like the nearest regional city centre, movie theatre, the pool, or the shopping centre.
(With inputs from agencies)
Also Read: Online Fundraiser For Bushfire-hit Koalas Reaches Aus$1 Million


{% include libraries.html %}
Always wanted to have your own start up? Here’s your chance.
E-Cell IIIT-H presents to you the first event in Epoch-Startup Aid. Startup Aid is a 4-sprint program aimed to help IIIT students polish raw ideas in their minds into something which they can take to an investor and probably convert the idea into a profitable startup. The program provides a golden opportunity to learn the different aspects of a startup and make connections with industry experts and entreprenurs.
Additionally, top three teams get a cash prize of 20K each.
For registration and further details: Click Here
ECell's newest initiative seamlessly connects startups with a passionate student workforce through an online internship portal. Exclusive to IIIT students, Campus works lets startups recruit students year round - connecting IIIT's finest with budding entreprenurship ventures. ECell guarantees that experiences through the campus works will be extremely enriching and rewarding, with monetary benefits as well, for the next generation of leaders.
E-Summit 2020 is a platform that brings together influential names in the industry, investors and people who are a part of India’s rapidly growing startup-ecosystem, with the objective of providing a great networking experience as well as a good opportunity to connect and discuss ideas for a better tomorrow. The two-day event also aims to connect and inspire budding entrepreneurs, emerging startups and people with innovative ideas. So, if you are someone who is looking to take your business idea to the next level, E-Summit 2020 is the place for you to be.


My focus was always on software engineering.
I acquired knowledge in different methodologies and strategies of development, architecture of solutions and management of projects.
I learned the mobile platforms, their interaction with web services and develop in each of them.


FROM TEMA WILLIAMS, JOHANNESBURG,SOUTH AFRICA. TELL:+27-732112866. REPLY TO<EMAIL_ADDRESS>OR CALL ME: +27 73 211 28 66 Dear sir, I am MR, TEMA WILLIAMS, the son of late MR.PATRICK WILLIAMS, who was murdered in a land dispute in ZIMBABWE.I got your contact during my search for reliable & compitent foreigner to handle a confidential financial transaction, then I decided to write you. My late father was among the few black Zimbabwean rich farmers murdered in cold blood by the agent of the ruling government of President ROBERT MUGABE for his alleged support and sympathy for Zimbabwean opposition party. Before the death of my father, he took me to South Africa to deposit the sum of Twelve million US Dollars ($12m) with a security and finance company, as if he knew the looming danger in Zimbabwe. The money was deposited as a gem/precious stone to avoid much dumourage from the security firm. This money was earmarked for the purchase of new machinery and chemicals for the farms and the establishment of new farms in Lesotho and Swaziland. This land problem arose when President R. MUGABE introduced a new land Act. which wholly affects the White rich farmers and some few blacks vehemently condemned the 'MODUS OPERANDI' adopted by the government. This resulted to rampant killing and mob actions. I and my family who are currently staying in South Africa as refugees or asylum seekers have decided to enter your name as foreigner/beneficiary of the Trunck Cases containing the Cash (USD12M) for futher transfer of the money to a foreign country where we can invest it. I am faced with the dilemma of claiming/investing this amount of money in South Africa for fear of encountering the same experience in future since both countries have the same political history. Moreover, the South African monetary policy/law does not allow such investment hence I am seeking for an asylum or refugee. I must let you know that this business is 100% risk free and the nature of your business does not necessary matter. So if you are willing to assist us claim the ownership of this money,I and my family have agreed to give you 20% of the total money, 40% will be for a joint business venture I will be doing with you and another 35% will be for me and my family which we shall also invest in your country and the remaining 5% will be mapped out for all expenses we may incure during the transaction. Therefor, if you are willing and interested to render the needed assistance. Please funish me your private phone and Fax number for easy and better communication. Remember that this is highly confidential and the success of this business depends on how secret it is kept. PLEASE KINDLY REPLY TO<EMAIL_ADDRESS>Expecting your reply soonest. Best Regard. TEMA WILLIAMS


LAX launches new holiday shuttle to Santa Monica, Century City to help alleviate traffic
LOS ANGELES (KABC) -- Los Angeles International Airport's new ride-share pickup service will get its first big test this week, as millions are expected to travel for Thanksgiving.
To help alleviate traffic, LAX is launching a new holiday shuttle.
Beginning Sunday morning through Monday Dec. 2, a shuttle will take passengers between LAX and locations in Santa Monica and Century City.
The Santa Monica location is located at 1925 Arizona Ave. The Century City stop is at 2030 Century Park West.
At the airport, passengers can pick up the bus on the arrivals level at pink shuttle stops.
Each ride will cost $10 one way.


Flow diagram of study. ART antiretroviral therapy, WB western blotting. We performed this observational cohort study using data from the HIV/AIDS Comprehensive Response Information Management System (CRIMS) in Guangxi, China. Local health workers completed standardized reporting forms recording baseline information when HIV patients were newly diagnosed, and follow-up visits then conducted.


This map is the observed pedestrian and bicycle collisions in San Francisco thanks to the SWITRS database that included their latitude and longitude.
Collision locations are snapped to the nearest intersection using the latitudes and longitudes given from SWITRS to the latitude and longitudes of the intersections in San Francisco.

Data is not entirely complete according to SWITRS information for years 2015-2017.
How to use this map
Select a collision type and severity to display it on the map for all years.
Move the year slider to map historic data.
Click on an intersection to update the chart with historic data for the clicked on intersection.
Click the arrow on the top right to hide the side panel.


## UTEK 2018 Programming
![utek logo](resources/logo.svg)
2018-01-13 / 2018-01-14
---
### This year's competition will use:
- String processing
- Discrete optimization
---
### Before the competition
#### Required
- Register if you haven't
- Set up your developer environment (local, ECF, Google cloud)
- [Create a Github account](https://github.com/)
#### Optional
- [Install git](https://git-scm.com/downloads)
- [Create a Google cloud account. Required if you don't have ECF access](https://console.cloud.google.com/freetrial?page=0)
Note:
Github account required to ask questions: open an issue. I will also be tracking revision history of the competition package there
Ask who is not in engineering
--
### At the competition
You will need to create:
- A program (obviously), that will need to pass various test cases
- An 8 minute presentation
- Optional visualization component
---
### Optimization
What is discrete optimization?
What algorithms can we use?
Note:
Talk briefly about hill climbing and simulated annealing on the board
- Hill climbing
- Ant colony optimization
- Simulated annealing
--
### Exercise
You have a csv file (`exercises/optimization/terrain.txt`) containing a 100x100 2d array. Find the largest possible value in the array, inspecting fewer than 1000 values. Use one of the methods from before.
- Try using multiple seeds. Does this improve the results more than optimizing for longer?
- Now try finding the minimum value
Note:
The max is 0.492
The min is -0.246
---
### String Processing
Reading, breaking, creating, modifying strings
--
### Exercise: Pig Latin translator
igpay atinlay anslatortay
```
If the word begins with a consonant:
[first consonant cluster][rest of word]
-> [rest of word][first consonant cluster] + "ay"
pig -> ig + p + ay
train -> ain + tr + ay
Else:
[word] -> [word] + "way"
egg -> egg + way
```
--
### Exercise cont.
You have a list of input sentences (`exercises/string_processing/words.txt`). Write a program that outputs the pig latin translation of each.
- Carry any punctation e.g. `hello! -> ellohay`
- Carry the capitalization of each word (UPPER, Title or lower) e.g. `ONE two Three -> ONEWAY otway Eetray`
- Assume that "I" and "A" are in title case.
Note:
there are lots of weird cases that haven't been properly covered, but this is just an exercise
--
### Creating a Google Cloud account
- Go to the [Google cloud page](https://console.cloud.google.com/compute/instances)
- Create a project and an instance, using the default settings EXCEPT use an f1 instance (the free tier :) )
- Go to VM instances, click Connect. This will open an SSH shell
- You will need a credit card to set this up
Note:
People in engineering can leave if they are planning to use ECF
--
#### OS details
- Debian 6.3.0
- Python 2.7.13 and 3.5.3 are installed by default
#### Required
- Install git with `sudo apt-get install git`
If you install anything else, you must provide installation instructions so that I can reproduce your results.
--
- You can sync your files to the VM using:
- Git (recommended)
- Or you can use [Google's cloud storage](https://cloud.google.com/storage/docs/object-basics)
- Documentation
- https://cloud.google.com/compute/docs/instances/create-start-instance
- https://cloud.google.com/compute/docs/instances/connecting-to-instance
--
Exercise:
Create an instance and install git. Then clone this repo.
`git clone https://github.com/utek-2018/training-session.git`
Note:
The competition package will be uploaded to this project
---
## Questions?
---
### See you at Mars
2018-01-13T09:30


Form Helpers¶ ↑
Forms in web applications are an essential interface for user input. However, form markup can quickly become tedious to write and maintain because of the need to handle form control naming and its numerous attributes. Rails does away with this complexity by providing view helpers for generating form markup. However, since these helpers have different use cases, developers need to know the differences between the helper methods before putting them to use.
After reading this guide, you will know:
-
How to create search forms and similar kind of generic forms not representing any specific model in your application.
-
How to make model-centric forms for creating and editing specific database records.
-
How to generate select boxes from multiple types of data.
-
What date and time helpers Rails provides.
-
What makes a file upload form different.
-
How to post forms to external resources and specify setting an
authenticity_token
. -
How to build complex forms.
NOTE: This guide is not intended to be a complete documentation of available form helpers and their arguments. Please visit the Rails API documentation for a complete reference.
Dealing with Basic Forms¶ ↑
The most basic form helper is form_tag
.
<%= form_tag do %> Form contents <% end %>
When called without arguments like this, it creates a
<form>
tag which, when submitted, will POST to the
current page. For instance, assuming the current page is
/home/index
, the generated HTML will look like this (some line
breaks added for readability):
<form accept-charset="UTF-8" action="/" method="post"> <input name="utf8" type="hidden" value="✓" /> <input name="authenticity_token" type="hidden" value="J7CBxfHalt49OSHp27hblqK20c9PgwJ108nDHX/8Cts=" /> Form contents </form>
You'll notice that the HTML contains input
element with
type hidden
. This input
is important, because the
form cannot be successfully submitted without it. The hidden input element
has name attribute of utf8
enforces browsers to properly
respect your form's character encoding and is generated for all forms
whether their actions are “GET” or “POST”. The second input element with
name authenticity_token
is a security feature of Rails called
cross-site request forgery protection, and form helpers
generate it for every non-GET form (provided that this security feature is
enabled). You can read more about this in the Security Guide.
A Generic Search Form¶ ↑
One of the most basic forms you see on the web is a search form. This form contains:
-
a form element with “GET” method,
-
a label for the input,
-
a text input element, and
-
a submit element.
To create this form you will use form_tag
,
label_tag
, text_field_tag
, and
submit_tag
, respectively. Like this:
<%= form_tag("/search", method: "get") do %> <%= label_tag(:q, "Search for:") %> <%= text_field_tag(:q) %> <%= submit_tag("Search") %> <% end %>
This will generate the following HTML:
<form accept-charset="UTF-8" action="/search" method="get"> <input name="utf8" type="hidden" value="✓" /> Search for: <input id="q" name="q" type="text" /> <input name="commit" type="submit" value="Search" /> </form>
TIP: For every form input, an ID attribute is generated from its name
("q"
in above example). These IDs can be very useful
for CSS styling or manipulation of form controls with JavaScript.
Besides text_field_tag
and submit_tag
, there is a
similar helper for every form control in HTML.
IMPORTANT: Always use “GET” as the method for search forms. This allows users to bookmark a specific search and get back to it. More generally Rails encourages you to use the right HTTP verb for an action.
Multiple Hashes in Form Helper Calls¶ ↑
The form_tag
helper accepts 2 arguments: the path for the
action and an options hash. This hash specifies the method of form
submission and HTML options such as the form element's class.
As with the link_to
helper, the path argument doesn't have
to be a string; it can be a hash of URL parameters recognizable by
Rails' routing mechanism, which will turn the hash into a valid URL.
However, since both arguments to form_tag
are hashes, you can
easily run into a problem if you would like to specify both. For instance,
let's say you write this:
form_tag(controller: "people", action: "search", method: "get", class: "nifty_form") # => '<form accept-charset="UTF-8" action="/people/search?method=get&class=nifty_form" method="post">'
Here, method
and class
are appended to the query
string of the generated URL because even though you mean to write two
hashes, you really only specified one. So you need to tell Ruby which is
which by delimiting the first hash (or both) with curly brackets. This will
generate the HTML you expect:
form_tag({controller: "people", action: "search"}, method: "get", class: "nifty_form") # => '<form accept-charset="UTF-8" action="/people/search" method="get" class="nifty_form">'
Helpers for Generating Form Elements¶ ↑
Rails provides a series of helpers for generating form elements such as
checkboxes, text fields, and radio buttons. These basic helpers, with names
ending in _tag
(such as text_field_tag
and
check_box_tag
), generate just a single
<input>
element. The first parameter to these is always
the name of the input. When the form is submitted, the name will be passed
along with the form data, and will make its way to the params
hash in the controller with the value entered by the user for that field.
For example, if the form contains <%= text_field_tag(:query)
%>
, then you would be able to get the value of this field in the
controller with params[:query]
.
When naming inputs, Rails uses certain conventions that make it possible to
submit parameters with non-scalar values such as arrays or hashes, which
will also be accessible in params
. You can read more about
them in chapter 7 of
this guide. For details on the precise usage of these helpers, please
refer to the API
documentation.
Checkboxes¶ ↑
Checkboxes are form controls that give the user a set of options they can enable or disable:
<%= check_box_tag(:pet_dog) %> <%= label_tag(:pet_dog, "I own a dog") %> <%= check_box_tag(:pet_cat) %> <%= label_tag(:pet_cat, "I own a cat") %>
This generates the following:
<input id="pet_dog" name="pet_dog" type="checkbox" value="1" /> I own a dog <input id="pet_cat" name="pet_cat" type="checkbox" value="1" /> I own a cat
The first parameter to check_box_tag
, of course, is the name
of the input. The second parameter, naturally, is the value of the input.
This value will be included in the form data (and be present in
params
) when the checkbox is checked.
Radio Buttons¶ ↑
Radio buttons, while similar to checkboxes, are controls that specify a set of options in which they are mutually exclusive (i.e., the user can only pick one):
<%= radio_button_tag(:age, "child") %> <%= label_tag(:age_child, "I am younger than 21") %> <%= radio_button_tag(:age, "adult") %> <%= label_tag(:age_adult, "I'm over 21") %>
Output:
<input id="age_child" name="age" type="radio" value="child" /> I am younger than 21 <input id="age_adult" name="age" type="radio" value="adult" /> I'm over 21
As with check_box_tag
, the second parameter to
radio_button_tag
is the value of the input. Because these two
radio buttons share the same name (age
), the user will only be
able to select one of them, and params[:age]
will contain
either "child"
or "adult"
.
NOTE: Always use labels for checkbox and radio buttons. They associate text with a specific option and, by expanding the clickable region, make it easier for users to click the inputs.
Other Helpers of Interest¶ ↑
Other form controls worth mentioning are textareas, password fields, hidden fields, search fields, telephone fields, date fields, time fields, color fields, datetime fields, datetime-local fields, month fields, week fields, URL fields, email fields, number fields and range fields:
<%= text_area_tag(:message, "Hi, nice site", size: "24x6") %> <%= password_field_tag(:password) %> <%= hidden_field_tag(:parent_id, "5") %> <%= search_field(:user, :name) %> <%= telephone_field(:user, :phone) %> <%= date_field(:user, :born_on) %> <%= datetime_field(:user, :meeting_time) %> <%= datetime_local_field(:user, :graduation_day) %> <%= month_field(:user, :birthday_month) %> <%= week_field(:user, :birthday_week) %> <%= url_field(:user, :homepage) %> <%= email_field(:user, :address) %> <%= color_field(:user, :favorite_color) %> <%= time_field(:task, :started_at) %> <%= number_field(:product, :price, in: 1.0..20.0, step: 0.5) %> <%= range_field(:product, :discount, in: 1..100) %>
Output:
<textarea id="message" name="message" cols="24" rows="6">Hi, nice site</textarea> <input id="password" name="password" type="password" /> <input id="parent_id" name="parent_id" type="hidden" value="5" /> <input id="user_name" name="user[name]" type="search" /> <input id="user_phone" name="user[phone]" type="tel" /> <input id="user_born_on" name="user[born_on]" type="date" /> <input id="user_meeting_time" name="user[meeting_time]" type="datetime" /> <input id="user_graduation_day" name="user[graduation_day]" type="datetime-local" /> <input id="user_birthday_month" name="user[birthday_month]" type="month" /> <input id="user_birthday_week" name="user[birthday_week]" type="week" /> <input id="user_homepage" name="user[homepage]" type="url" /> <input id="user_address" name="user[address]" type="email" /> <input id="user_favorite_color" name="user[favorite_color]" type="color" value="#000000" /> <input id="task_started_at" name="task[started_at]" type="time" /> <input id="product_price" max="20.0" min="1.0" name="product[price]" step="0.5" type="number" /> <input id="product_discount" max="100" min="1" name="product[discount]" type="range" />
Hidden inputs are not shown to the user but instead hold data like any textual input. Values inside them can be changed with JavaScript.
IMPORTANT: The search, telephone, date, time, color, datetime, datetime-local, month, week, URL, email, number and range inputs are HTML5 controls. If you require your app to have a consistent experience in older browsers, you will need an HTML5 polyfill (provided by CSS and/or JavaScript). There is definitely no shortage of solutions for this, although a couple of popular tools at the moment are Modernizr and yepnope, which provide a simple way to add functionality based on the presence of detected HTML5 features.
TIP: If you're using password input fields (for any purpose), you might want to configure your application to prevent those parameters from being logged. You can learn about this in the Security Guide.
Dealing with Model Objects¶ ↑
Model Object Helpers¶ ↑
A particularly common task for a form is editing or creating a model
object. While the *_tag
helpers can certainly be used for this
task they are somewhat verbose as for each tag you would have to ensure the
correct parameter name is used and set the default value of the input
appropriately. Rails provides helpers tailored to this task. These helpers
lack the _tag
suffix, for example text_field
,
text_area
.
For these helpers the first argument is the name of an instance variable
and the second is the name of a method (usually an attribute) to call on
that object. Rails will set the value of the input control to the return
value of that method for the object and set an appropriate input name. If
your controller has defined @person
and that person's name
is Henry then a form containing:
<%= text_field(:person, :name) %>
will produce output similar to
<input id="person_name" name="person[name]" type="text" value="Henry"/>
Upon form submission the value entered by the user will be stored in
params[:person][:name]
. The params[:person]
hash
is suitable for passing to Person.new
or, if
@person
is an instance of Person, @person.update
.
While the name of an attribute is the most common second parameter to these
helpers this is not compulsory. In the example above, as long as person
objects have a name
and a name=
method Rails will
be happy.
WARNING: You must pass the name of an instance variable, i.e.
:person
or "person"
, not an actual
instance of your model object.
Rails provides helpers for displaying the validation errors associated with a model object. These are covered in detail by the Active Record Validations guide.
Binding a Form to an Object¶ ↑
While this is an increase in comfort it is far from perfect. If
Person
has many attributes to edit then we would be repeating
the name of the edited object many times. What we want to do is somehow
bind a form to a model object, which is exactly what form_for
does.
Assume we have a controller for dealing with articles
app/controllers/articles_controller.rb
:
def new @article = Article.new end
The corresponding view app/views/articles/new.html.erb
using
form_for
looks like this:
<%= form_for @article, url: {action: "create"}, html: {class: "nifty_form"} do |f| %> <%= f.text_field :title %> <%= f.text_area :body, size: "60x12" %> <%= f.submit "Create" %> <% end %>
There are a few things to note here:
-
@article
is the actual object being edited. -
There is a single hash of options. Routing options are passed in the
:url
hash, HTML options are passed in the:html
hash. Also you can provide a:namespace
option for your form to ensure uniqueness of id attributes on form elements. The namespace attribute will be prefixed with underscore on the generated HTML id. -
The
form_for
method yields a form builder object (thef
variable). -
Methods to create form controls are called on the form builder object
f
.
The resulting HTML is:
<form accept-charset="UTF-8" action="/articles/create" method="post" class="nifty_form"> <input id="article_title" name="article[title]" type="text" /> <textarea id="article_body" name="article[body]" cols="60" rows="12"></textarea> <input name="commit" type="submit" value="Create" /> </form>
The name passed to form_for
controls the key used in
params
to access the form's values. Here the name is
article
and so all the inputs have names of the form
article[attribute_name]
. Accordingly, in the
create
action params[:article]
will be a hash
with keys :title
and :body
. You can read more
about the significance of input names in the parameter_names section.
The helper methods called on the form builder are identical to the model object helpers except that it is not necessary to specify which object is being edited since this is already managed by the form builder.
You can create a similar binding without actually creating
<form>
tags with the fields_for
helper.
This is useful for editing additional model objects with the same form. For
example, if you had a Person
model with an associated
ContactDetail
model, you could create a form for creating both
like so:
<%= form_for @person, url: {action: "create"} do |person_form| %> <%= person_form.text_field :name %> <%= fields_for @person.contact_detail do |contact_details_form| %> <%= contact_details_form.text_field :phone_number %> <% end %> <% end %>
which produces the following output:
<form accept-charset="UTF-8" action="/people/create" class="new_person" id="new_person" method="post"> <input id="person_name" name="person[name]" type="text" /> <input id="contact_detail_phone_number" name="contact_detail[phone_number]" type="text" /> </form>
The object yielded by fields_for
is a form builder like the
one yielded by form_for
(in fact form_for
calls
fields_for
internally).
Relying on Record Identification¶ ↑
The Article model is directly available to users of the application, so - following the best practices for developing with Rails - you should declare it a resource:
resources :articles
TIP: Declaring a resource has a number of side-affects. See Rails Routing From the Outside In for more information on setting up and using resources.
When dealing with RESTful resources, calls to form_for
can get
significantly easier if you rely on record identification.
In short, you can just pass the model instance and have Rails figure out
model name and the rest:
## Creating a new article # long-style: form_for(@article, url: articles_path) # same thing, short-style (record identification gets used): form_for(@article) ## Editing an existing article # long-style: form_for(@article, url: article_path(@article), html: {method: "patch"}) # short-style: form_for(@article)
Notice how the short-style form_for
invocation is conveniently
the same, regardless of the record being new or existing. Record
identification is smart enough to figure out if the record is new by asking
record.new_record?
. It also selects the correct path to submit
to and the name based on the class of the object.
Rails will also automatically set the class
and
id
of the form appropriately: a form creating an article would
have id
and class
new_article
. If
you were editing the article with id 23, the class
would be
set to edit_article
and the id to
edit_article_23
. These attributes will be omitted for brevity
in the rest of this guide.
WARNING: When you're using STI (single-table inheritance) with your
models, you can't rely on record identification on a subclass if only
their parent class is declared a resource. You will have to specify the
model name, :url
, and :method
explicitly.
Dealing with Namespaces¶ ↑
If you have created namespaced routes, form_for
has a nifty
shorthand for that too. If your application has an admin namespace then
form_for [:admin, @article]
will create a form that submits to the ArticlesController
inside the admin namespace (submitting to
admin_article_path(@article)
in the case of an update). If you
have several levels of namespacing then the syntax is similar:
form_for [:admin, :management, @article]
For more information on Rails' routing system and the associated conventions, please see the routing guide.
How do forms with PATCH, PUT, or DELETE methods work?¶ ↑
The Rails framework encourages RESTful design of your applications, which means you'll be making a lot of “PATCH” and “DELETE” requests (besides “GET” and “POST”). However, most browsers don't support methods other than “GET” and “POST” when it comes to submitting forms.
Rails works around this issue by emulating other methods over POST with a
hidden input named "_method"
, which is set to
reflect the desired method:
form_tag(search_path, method: "patch")
output:
<form accept-charset="UTF-8" action="/search" method="post"> <input name="_method" type="hidden" value="patch" /> <input name="utf8" type="hidden" value="✓" /> <input name="authenticity_token" type="hidden" value="f755bb0ed134b76c432144748a6d4b7a7ddf2b71" /> ... </form>
When parsing POSTed data, Rails will take into account the special
_method
parameter and acts as if the HTTP method was the one
specified inside it (“PATCH” in this example).
Making Select Boxes with Ease¶ ↑
Select boxes in HTML require a significant amount of markup (one
OPTION
element for each option to choose from), therefore it
makes the most sense for them to be dynamically generated.
Here is what the markup might look like:
<select name="city_id" id="city_id"> <option value="1">Lisbon</option> <option value="2">Madrid</option> ... <option value="12">Berlin</option> </select>
Here you have a list of cities whose names are presented to the user. Internally the application only wants to handle their IDs so they are used as the options' value attribute. Let's see how Rails can help out here.
The Select and Option Tags¶ ↑
The most generic helper is select_tag
, which - as the name
implies - simply generates the SELECT
tag that encapsulates an
options string:
<%= select_tag(:city_id, '<option value="1">Lisbon</option>...') %>
This is a start, but it doesn't dynamically create the option tags. You
can generate option tags with the options_for_select
helper:
<%= options_for_select([['Lisbon', 1], ['Madrid', 2], ...]) %> output: <option value="1">Lisbon</option> <option value="2">Madrid</option> ...
The first argument to options_for_select
is a nested array
where each element has two elements: option text (city name) and option
value (city id). The option value is what will be submitted to your
controller. Often this will be the id of a corresponding database object
but this does not have to be the case.
Knowing this, you can combine select_tag
and
options_for_select
to achieve the desired, complete markup:
<%= select_tag(:city_id, options_for_select(...)) %>
options_for_select
allows you to pre-select an option by
passing its value.
<%= options_for_select([['Lisbon', 1], ['Madrid', 2], ...], 2) %> output: <option value="1">Lisbon</option> <option value="2" selected="selected">Madrid</option> ...
Whenever Rails sees that the internal value of an option being generated
matches this value, it will add the selected
attribute to that
option.
TIP: The second argument to options_for_select
must be exactly
equal to the desired internal value. In particular if the value is the
integer 2
you cannot pass "2"
to
options_for_select
- you must pass 2
. Be aware of
values extracted from the params
hash as they are all strings.
WARNING: when :include_blank
or :prompt
are not
present, :include_blank
is forced true if the select attribute
required
is true, display size
is one and
multiple
is not true.
You can add arbitrary attributes to the options using hashes:
<%= options_for_select( [ ['Lisbon', 1, { 'data-size' => '2.8 million' }], ['Madrid', 2, { 'data-size' => '3.2 million' }] ], 2 ) %> output: <option value="1" data-size="2.8 million">Lisbon</option> <option value="2" selected="selected" data-size="3.2 million">Madrid</option> ...
Select Boxes for Dealing with Models¶ ↑
In most cases form controls will be tied to a specific database model and
as you might expect Rails provides helpers tailored for that purpose.
Consistent with other form helpers, when dealing with models you drop the
_tag
suffix from select_tag
:
# controller: @person = Person.new(city_id: 2)
# view: <%= select(:person, :city_id, [['Lisbon', 1], ['Madrid', 2], ...]) %>
Notice that the third parameter, the options array, is the same kind of
argument you pass to options_for_select
. One advantage here is
that you don't have to worry about pre-selecting the correct city if
the user already has one - Rails will do this for you by reading from the
@person.city_id
attribute.
As with other helpers, if you were to use the select
helper on
a form builder scoped to the @person
object, the syntax would
be:
# select on a form builder <%= f.select(:city_id, ...) %>
You can also pass a block to select
helper:
<%= f.select(:city_id) do %> <% [['Lisbon', 1], ['Madrid', 2]].each do |c| -%> <%= content_tag(:option, c.first, value: c.last) %> <% end %> <% end %>
WARNING: If you are using select
(or similar helpers such as
collection_select
, select_tag
) to set a
belongs_to
association you must pass the name of the foreign
key (in the example above city_id
), not the name of
association itself. If you specify city
instead of
city_id
Active Record will raise an error along the lines of
ActiveRecord::AssociationTypeMismatch: City(#17815740) expected, got
String(#1138750)
when you pass the params
hash to
Person.new
or update
. Another way of looking at
this is that form helpers only edit attributes. You should also be aware of
the potential security ramifications of allowing users to edit foreign keys
directly.
Option Tags from a Collection of Arbitrary Objects¶ ↑
Generating options tags with options_for_select
requires that
you create an array containing the text and value for each option. But what
if you had a City
model (perhaps an Active Record one) and you
wanted to generate option tags from a collection of those objects? One
solution would be to make a nested array by iterating over them:
<% cities_array = City.all.map { |city| [city.name, city.id] } %> <%= options_for_select(cities_array) %>
This is a perfectly valid solution, but Rails provides a less verbose
alternative: options_from_collection_for_select
. This helper
expects a collection of arbitrary objects and two additional arguments: the
names of the methods to read the option value and
text from, respectively:
<%= options_from_collection_for_select(City.all, :id, :name) %>
As the name implies, this only generates option tags. To generate a working
select box you would need to use it in conjunction with
select_tag
, just as you would with
options_for_select
. When working with model objects, just as
select
combines select_tag
and
options_for_select
, collection_select
combines
select_tag
with
options_from_collection_for_select
.
<%= collection_select(:person, :city_id, City.all, :id, :name) %>
As with other helpers, if you were to use the
collection_select
helper on a form builder scoped to the
@person
object, the syntax would be:
<%= f.collection_select(:city_id, City.all, :id, :name) %>
To recap, options_from_collection_for_select
is to
collection_select
what options_for_select
is to
select
.
NOTE: Pairs passed to options_for_select
should have the name
first and the id second, however with
options_from_collection_for_select
the first argument is the
value method and the second the text method.
Time Zone and Country Select¶ ↑
To leverage time zone support in Rails, you have to ask your users what
time zone they are in. Doing so would require generating select options
from a list of pre-defined TimeZone objects using
collection_select
, but you can simply use the
time_zone_select
helper that already wraps this:
<%= time_zone_select(:person, :time_zone) %>
There is also time_zone_options_for_select
helper for a more
manual (therefore more customizable) way of doing this. Read the API
documentation to learn about the possible arguments for these two methods.
Rails used to have a country_select
helper for
choosing countries, but this has been extracted to the country_select
plugin. When using this, be aware that the exclusion or inclusion of
certain names from the list can be somewhat controversial (and was the
reason this functionality was extracted from Rails).
Using Date and Time Form Helpers¶ ↑
You can choose not to use the form helpers generating HTML5 date and time input fields and use the alternative date and time helpers. These date and time helpers differ from all the other form helpers in two important respects:
-
Dates and times are not representable by a single input element. Instead you have several, one for each component (year, month, day etc.) and so there is no single value in your
params
hash with your date or time. -
Other helpers use the
_tag
suffix to indicate whether a helper is a barebones helper or one that operates on model objects. With dates and times,select_date
,select_time
andselect_datetime
are the barebones helpers,date_select
,time_select
anddatetime_select
are the equivalent model object helpers.
Both of these families of helpers will create a series of select boxes for the different components (year, month, day etc.).
Barebones Helpers¶ ↑
The select_*
family of helpers take as their first argument an
instance of Date
, Time
or DateTime
that is used as the currently selected value. You may omit this parameter,
in which case the current date is used. For example:
<%= select_date Date.today, prefix: :start_date %>
outputs (with actual option values omitted for brevity)
<select id="start_date_year" name="start_date[year]"> ... </select> <select id="start_date_month" name="start_date[month]"> ... </select> <select id="start_date_day" name="start_date[day]"> ... </select>
The above inputs would result in params[:start_date]
being a
hash with keys :year
, :month
, :day
.
To get an actual Date
, Time
or
DateTime
object you would have to extract these values and
pass them to the appropriate constructor, for example:
Date.civil(params[:start_date][:year].to_i, params[:start_date][:month].to_i, params[:start_date][:day].to_i)
The :prefix
option is the key used to retrieve the hash of
date components from the params
hash. Here it was set to
start_date
, if omitted it will default to date
.
Model Object Helpers¶ ↑
select_date
does not work well with forms that update or
create Active Record objects as Active Record expects each element of the
params
hash to correspond to one attribute. The model object
helpers for dates and times submit parameters with special names; when
Active Record sees parameters with such names it knows they must be
combined with the other parameters and given to a constructor appropriate
to the column type. For example:
<%= date_select :person, :birth_date %>
outputs (with actual option values omitted for brevity)
<select id="person_birth_date_1i" name="person[birth_date(1i)]"> ... </select> <select id="person_birth_date_2i" name="person[birth_date(2i)]"> ... </select> <select id="person_birth_date_3i" name="person[birth_date(3i)]"> ... </select>
which results in a params
hash like
{'person' => {'birth_date(1i)' => '2008', 'birth_date(2i)' => '11', 'birth_date(3i)' => '22'}}
When this is passed to Person.new
(or update
),
Active Record spots that these parameters should all be used to construct
the birth_date
attribute and uses the suffixed information to
determine in which order it should pass these parameters to functions such
as Date.civil
.
Common Options¶ ↑
Both families of helpers use the same core set of functions to generate the
individual select tags and so both accept largely the same options. In
particular, by default Rails will generate year options 5 years either side
of the current year. If this is not an appropriate range, the
:start_year
and :end_year
options override this.
For an exhaustive list of the available options, refer to the API
documentation.
As a rule of thumb you should be using date_select
when
working with model objects and select_date
in other cases,
such as a search form which filters results by date.
NOTE: In many cases the built-in date pickers are clumsy as they do not aid the user in working out the relationship between the date and the day of the week.
Individual Components¶ ↑
Occasionally you need to display just a single date component such as a
year or a month. Rails provides a series of helpers for this, one for each
component select_year
, select_month
,
select_day
, select_hour
,
select_minute
, select_second
. These helpers are
fairly straightforward. By default they will generate an input field named
after the time component (for example, “year” for select_year
,
“month” for select_month
etc.) although this can be overridden
with the :field_name
option. The :prefix
option
works in the same way that it does for select_date
and
select_time
and has the same default value.
The first parameter specifies which value should be selected and can either
be an instance of a Date
, Time
or
DateTime
, in which case the relevant component will be
extracted, or a numerical value. For example:
<%= select_year(2009) %> <%= select_year(Time.now) %>
will produce the same output if the current year is 2009 and the value
chosen by the user can be retrieved by params[:date][:year]
.
Uploading Files¶ ↑
A common task is uploading some sort of file, whether it's a picture of
a person or a CSV file containing data to process. The most important thing
to remember with file uploads is that the rendered form's encoding
MUST be set to “multipart/form-data”. If you use
form_for
, this is done automatically. If you use
form_tag
, you must set it yourself, as per the following
example.
The following two forms both upload a file.
<%= form_tag({action: :upload}, multipart: true) do %> <%= file_field_tag 'picture' %> <% end %> <%= form_for @person do |f| %> <%= f.file_field :picture %> <% end %>
Rails provides the usual pair of helpers: the barebones
file_field_tag
and the model oriented file_field
.
The only difference with other helpers is that you cannot set a default
value for file inputs as this would have no meaning. As you would expect in
the first case the uploaded file is in params[:picture]
and in
the second case in params[:person][:picture]
.
What Gets Uploaded¶ ↑
The object in the params
hash is an instance of a subclass of
IO
. Depending on the size of the uploaded file it may in fact
be a StringIO
or an instance of File
backed by a
temporary file. In both cases the object will have an
original_filename
attribute containing the name the file had
on the user's computer and a content_type
attribute
containing the MIME type of the uploaded file. The following snippet saves
the uploaded content in #{Rails.root}/public/uploads
under the
same name as the original file (assuming the form was the one in the
previous example).
def upload uploaded_io = params[:person][:picture] File.open(Rails.root.join('public', 'uploads', uploaded_io.original_filename), 'wb') do |file| file.write(uploaded_io.read) end end
Once a file has been uploaded, there are a multitude of potential tasks, ranging from where to store the files (on disk, Amazon S3, etc) and associating them with models to resizing image files and generating thumbnails. The intricacies of this are beyond the scope of this guide, but there are several libraries designed to assist with these. Two of the better known ones are CarrierWave and Paperclip.
NOTE: If the user has not selected a file the corresponding parameter will be an empty string.
Dealing with Ajax¶ ↑
Unlike other forms making an asynchronous file upload form is not as simple
as providing form_for
with remote: true
. With an
Ajax form the serialization is done by JavaScript running inside the
browser and since JavaScript cannot read files from your hard drive the
file cannot be uploaded. The most common workaround is to use an invisible
iframe that serves as the target for the form submission.
Customizing Form Builders¶ ↑
As mentioned previously the object yielded by form_for
and
fields_for
is an instance of FormBuilder
(or a
subclass thereof). Form builders encapsulate the notion of displaying form
elements for a single object. While you can of course write helpers for
your forms in the usual way, you can also subclass FormBuilder
and add the helpers there. For example:
<%= form_for @person do |f| %> <%= text_field_with_label f, :first_name %> <% end %>
can be replaced with
<%= form_for @person, builder: LabellingFormBuilder do |f| %> <%= f.text_field :first_name %> <% end %>
by defining a LabellingFormBuilder
class similar to the
following:
class LabellingFormBuilder < ActionView::Helpers::FormBuilder def text_field(attribute, options={}) label(attribute) + super end end
If you reuse this frequently you could define a
labeled_form_for
helper that automatically applies the
builder: LabellingFormBuilder
option.
The form builder used also determines what happens when you do
<%= render partial: f %>
If f
is an instance of FormBuilder
then this will
render the form
partial, setting the partial's object to
the form builder. If the form builder is of class
LabellingFormBuilder
then the labelling_form
partial would be rendered instead.
Understanding Parameter Naming Conventions¶ ↑
As you've seen in the previous sections, values from forms can be at
the top level of the params
hash or nested in another hash.
For example, in a standard create
action for a Person model,
params[:person]
would usually be a hash of all the attributes
for the person to create. The params
hash can also contain
arrays, arrays of hashes and so on.
Fundamentally HTML forms don't know about any sort of structured data, all they generate is name-value pairs, where pairs are just plain strings. The arrays and hashes you see in your application are the result of some parameter naming conventions that Rails uses.
TIP: You may find you can try out examples in this section faster by using the console to directly invoke Rack's parameter parser. For example,
Rack::Utils.parse_query "name=fred&phone=0123456789" # => {"name"=>"fred", "phone"=>"0123456789"}
Basic Structures¶ ↑
The two basic structures are arrays and hashes. Hashes mirror the syntax
used for accessing the value in params
. For example, if a form
contains:
<input id="person_name" name="person[name]" type="text" value="Henry"/>
the params
hash will contain
{'person' => {'name' => 'Henry'}}
and params[:person][:name]
will retrieve the submitted value
in the controller.
Hashes can be nested as many levels as required, for example:
<input id="person_address_city" name="person[address][city]" type="text" value="New York"/>
will result in the params
hash being
{'person' => {'address' => {'city' => 'New York'}}}
Normally Rails ignores duplicate parameter names. If the parameter name
contains an empty set of square brackets []
then they will be
accumulated in an array. If you wanted users to be able to input multiple
phone numbers, you could place this in the form:
<input name="person[phone_number][]" type="text"/> <input name="person[phone_number][]" type="text"/> <input name="person[phone_number][]" type="text"/>
This would result in params[:person][:phone_number]
being an
array containing the inputted phone numbers.
Combining Them¶ ↑
We can mix and match these two concepts. One element of a hash might be an array as in the previous example, or you can have an array of hashes. For example, a form might let you create any number of addresses by repeating the following form fragment
<input name="addresses[][line1]" type="text"/> <input name="addresses[][line2]" type="text"/> <input name="addresses[][city]" type="text"/>
This would result in params[:addresses]
being an array of
hashes with keys line1
, line2
and
city
. Rails decides to start accumulating values in a new hash
whenever it encounters an input name that already exists in the current
hash.
There's a restriction, however, while hashes can be nested arbitrarily, only one level of “arrayness” is allowed. Arrays can usually be replaced by hashes; for example, instead of having an array of model objects, one can have a hash of model objects keyed by their id, an array index or some other parameter.
WARNING: Array parameters do not play well with the check_box
helper. According to the HTML specification unchecked checkboxes submit no
value. However it is often convenient for a checkbox to always submit a
value. The check_box
helper fakes this by creating an
auxiliary hidden input with the same name. If the checkbox is unchecked
only the hidden input is submitted and if it is checked then both are
submitted but the value submitted by the checkbox takes precedence. When
working with array parameters this duplicate submission will confuse Rails
since duplicate input names are how it decides when to start a new array
element. It is preferable to either use check_box_tag
or to
use hashes instead of arrays.
Using Form Helpers¶ ↑
The previous sections did not use the Rails form helpers at all. While you
can craft the input names yourself and pass them directly to helpers such
as text_field_tag
Rails also provides higher level support.
The two tools at your disposal here are the name parameter to
form_for
and fields_for
and the
:index
option that helpers take.
You might want to render a form with a set of edit fields for each of a person's addresses. For example:
<%= form_for @person do |person_form| %> <%= person_form.text_field :name %> <% @person.addresses.each do |address| %> <%= person_form.fields_for address, index: address.id do |address_form|%> <%= address_form.text_field :city %> <% end %> <% end %> <% end %>
Assuming the person had two addresses, with ids 23 and 45 this would create output similar to this:
<form accept-charset="UTF-8" action="/people/1" class="edit_person" id="edit_person_1" method="post"> <input id="person_name" name="person[name]" type="text" /> <input id="person_address_23_city" name="person[address][23][city]" type="text" /> <input id="person_address_45_city" name="person[address][45][city]" type="text" /> </form>
This will result in a params
hash that looks like
{'person' => {'name' => 'Bob', 'address' => {'23' => {'city' => 'Paris'}, '45' => {'city' => 'London'}}}}
Rails knows that all these inputs should be part of the person hash because
you called fields_for
on the first form builder. By specifying
an :index
option you're telling Rails that instead of
naming the inputs person[address][city]
it should insert that
index surrounded by [] between the address and the city. This is often
useful as it is then easy to locate which Address record should be
modified. You can pass numbers with some other significance, strings or
even nil
(which will result in an array parameter being
created).
To create more intricate nestings, you can specify the first part of the
input name (person[address]
in the previous example)
explicitly:
<%= fields_for 'person[address][primary]', address, index: address do |address_form| %> <%= address_form.text_field :city %> <% end %>
will create inputs like
<input id="person_address_primary_1_city" name="person[address][primary][1][city]" type="text" value="bologna" />
As a general rule the final input name is the concatenation of the name
given to fields_for
/form_for
, the index value and
the name of the attribute. You can also pass an :index
option
directly to helpers such as text_field
, but it is usually less
repetitive to specify this at the form builder level rather than on
individual input controls.
As a shortcut you can append [] to the name and omit the
:index
option. This is the same as specifying index:
address
so
<%= fields_for 'person[address][primary][]', address do |address_form| %> <%= address_form.text_field :city %> <% end %>
produces exactly the same output as the previous example.
Forms to External Resources¶ ↑
Rails' form helpers can also be used to build a form for posting data
to an external resource. However, at times it can be necessary to set an
authenticity_token
for the resource; this can be done by
passing an authenticity_token: 'your_external_token'
parameter to the form_tag
options:
<%= form_tag 'http://farfar.away/form', authenticity_token: 'external_token' do %> Form contents <% end %>
Sometimes when submitting data to an external resource, like a payment
gateway, the fields that can be used in the form are limited by an external
API and it may be undesirable to generate an
authenticity_token
. To not send a token, simply pass
false
to the :authenticity_token
option:
<%= form_tag 'http://farfar.away/form', authenticity_token: false do %> Form contents <% end %>
The same technique is also available for form_for
:
<%= form_for @invoice, url: external_url, authenticity_token: 'external_token' do |f| %> Form contents <% end %>
Or if you don't want to render an authenticity_token
field:
<%= form_for @invoice, url: external_url, authenticity_token: false do |f| %> Form contents <% end %>
Building Complex Forms¶ ↑
Many apps grow beyond simple forms editing a single object. For example,
when creating a Person
you might want to allow the user to (on
the same form) create multiple address records (home, work, etc.). When
later editing that person the user should be able to add, remove or amend
addresses as necessary.
Configuring the Model¶ ↑
Active Record provides model level support via the
accepts_nested_attributes_for
method:
class Person < ActiveRecord::Base has_many :addresses accepts_nested_attributes_for :addresses end class Address < ActiveRecord::Base belongs_to :person end
This creates an addresses_attributes=
method on
Person
that allows you to create, update and (optionally)
destroy addresses.
Nested Forms¶ ↑
The following form allows a user to create a Person
and its
associated addresses.
<%= form_for @person do |f| %> Addresses:
 <%= f.fields_for :addresses do |addresses_form| %>
 <%= addresses_form.label :kind %> <%= addresses_form.text_field :kind %> <%= addresses_form.label :street %> <%= addresses_form.text_field :street %> ...
 <% end %>
 <% end %>
When an association accepts nested attributes fields_for
renders its block once for every element of the association. In particular,
if a person has no addresses it renders nothing. A common pattern is for
the controller to build one or more empty children so that at least one set
of fields is shown to the user. The example below would result in 2 sets of
address fields being rendered on the new person form.
def new @person = Person.new 2.times { @person.addresses.build} end
The fields_for
yields a form builder. The parameters' name
will be what accepts_nested_attributes_for
expects. For
example, when creating a user with 2 addresses, the submitted parameters
would look like:
{ 'person' => { 'name' => 'John Doe', 'addresses_attributes' => { '0' => { 'kind' => 'Home', 'street' => '221b Baker Street' }, '1' => { 'kind' => 'Office', 'street' => '31 Spooner Street' } } } }
The keys of the :addresses_attributes
hash are unimportant,
they need merely be different for each address.
If the associated object is already saved, fields_for
autogenerates a hidden input with the id
of the saved record.
You can disable this by passing include_id: false
to
fields_for
. You may wish to do this if the autogenerated input
is placed in a location where an input tag is not valid HTML or when using
an ORM where children do not have an id
.
The Controller¶ ↑
As usual you need to whitelist the parameters in the controller before you pass them to the model:
def create @person = Person.new(person_params) # ... end private def person_params params.require(:person).permit(:name, addresses_attributes: [:id, :kind, :street]) end
Removing Objects¶ ↑
You can allow users to delete associated objects by passing
allow_destroy: true
to
accepts_nested_attributes_for
class Person < ActiveRecord::Base has_many :addresses accepts_nested_attributes_for :addresses, allow_destroy: true end
If the hash of attributes for an object contains the key
_destroy
with a value of 1
or true
then the object will be destroyed. This form allows users to remove
addresses:
<%= form_for @person do |f| %> Addresses:
 <%= f.fields_for :addresses do |addresses_form| %>
 <%= addresses_form.check_box :_destroy%> <%= addresses_form.label :kind %> <%= addresses_form.text_field :kind %> ...
 <% end %>
 <% end %>
Don't forget to update the whitelisted params in your controller to
also include the _destroy
field:
def person_params params.require(:person). permit(:name, addresses_attributes: [:id, :kind, :street, :_destroy]) end
Preventing Empty Records¶ ↑
It is often useful to ignore sets of fields that the user has not filled
in. You can control this by passing a :reject_if
proc to
accepts_nested_attributes_for
. This proc will be called with
each hash of attributes submitted by the form. If the proc returns
false
then Active Record will not build an associated object
for that hash. The example below only tries to build an address if the
kind
attribute is set.
class Person < ActiveRecord::Base has_many :addresses accepts_nested_attributes_for :addresses, reject_if: lambda {|attributes| attributes['kind'].blank?} end
As a convenience you can instead pass the symbol :all_blank
which will create a proc that will reject records where all the attributes
are blank excluding any value for _destroy
.
Adding Fields on the Fly¶ ↑
Rather than rendering multiple sets of fields ahead of time you may wish to add them only when a user clicks on an 'Add new address' button. Rails does not provide any built-in support for this. When generating new sets of fields you must ensure the key of the associated array is unique - the current JavaScript date (milliseconds after the epoch) is a common choice.