Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
  • Rails: Using forms for operations like Patch, Put , Delete

    • 0
    • 1
    • 1
    • 1
    • 0
    • 0
    • 0
    • 0
    • 352
    Comment on it

    Hi friends,
    As we all know, rails always encourages RESTful (REpresentational State Transfer) design for resources, that specifies standards for using different kind of requests. Rails also supports this. Thus if you are using RESTful, you would be using so many requests like PATCH, PUT and DELETE, besides the default Get and Post. So first begin with the use case of using different kind of requests.

    patch : Used for updating an object partially, takes less bandwidth.
    put : Used for updating the whole object.
    delete : Used for removal of object.

    Now the problem with these requests are that most of the browsers support only get and post. Rails fixed this problem by emulating other methods over POST with a hidden input named "_method". It tells the controller about which type of request is coming, even if the browser doesn't support them. For example if there is a form to update the user's entry and we need to use the patch request, the form will look like this:

    form_tag(users_path, method: "patch")

    It will produce the following HTML:

    <form accept-charset="UTF-8" action="/users" method="post">
    <input name="_method" type="hidden" value="patch" />
    <input name="utf8" type="hidden" value="&#x2713;" />
    <input name="authenticity_token" type="hidden" value="f755bb0ed3847345jg45jg4j48a6d4b7a7ddf2b71" />

    For more blogs related to form helpers this click the link below:
    Form Helpers in Rails

 0 Comment(s)

Sign In

Sign up using

Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: