Rails Post, Put, Get

Rails POST, PUT, GET

I believe it's specified by REST. Here's a list for ya:

GET    /items        #=> index
GET /items/1 #=> show
GET /items/new #=> new
GET /items/1/edit #=> edit
PUT /items/1 #=> update
POST /items #=> create
DELETE /items/1 #=> destroy

Edited to add to get all those routes, in config/routes.rb, simply add map.resources :items

Same Rails 4 routes for GET and POST requests

From the match documentation, you can use match as long as you have via:

match "user/account" => "user#account", as: :user_account, via: [:get, :post]

Edit: Added a as: parameter so that it will be accessible via a url helper. user_account_path or user_account_url in this case.

Rails POST (patch) in development becomes GET in production

This usually happens because the Rails UJS JavaScript library failed to load and the data attributes it injects are ignored.

Check that the UJS library loaded. If you're using Sprockets, ensure that the rails-ujs "comment" line in application.js is present.

If you're running in production mode you need to ensure that your assets are properly compiled. This can be done with rake assets:precompile in the prouction deployment location for Rails 3 through 5.2.

GET or POST, which method to use for submitting a form?

GET requests are always added to the URL, where as POST is submitted with the body of the request. As you note both can be used to retrieve and send data, but there are some distinctions:

  1. As GET is sent with the URL you are limited in size to the maximum length of the query string. This varies from browser to browser, but is usually at least around 2000 characters (on modern browsers). This usually makes it inappropriate for sending large text fields (eg email).

  2. AS the GET command is exposed in the query string it can be easily modified by the user

  3. As the GET command is in the query string it does make it easier for users to bookmark a specific page, assuming your page will work with some state variables stored.

  4. POST is usually more appropriate for sending data, as it is suits the nature of a request, mostly because of the limitations of the above.

GET and POST for same route in Rails

In your sample code you have directed both the get and the post to usergroups#new, but you should send the post to usergroups#create.

Ruby example of Net::HTTP for GET, POST, PUT, DELETE

You would just namespace it:

Net::HTTP::Put.new(uri)

Same with delete:

Net::HTTP::Delete.new(uri)

You can even do that with your existing calls:

conn = Net::HTTP.new(uri)
con.get(path)

that is equivalent to:

Net::HTTP::Get.new(uri)

:method put: not working instead Get method

You are using correct format and your code is should generate something like this:

<a data-method="put" href="...">

From your routes error message we can conclude that it is not being sent using POST with _method=put params. So, the problem must be that you did not include jQuery and Rails jQuery extension javascript files.

An easy fix would be to include application.js file (jquery and rails js extension are included by default) in your page.



Related Topics



Leave a reply



Submit