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:
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).
AS the GET command is exposed in the query string it can be easily modified by the user
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.
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
How to Make a Post Request with Open-Uri
Get Sidekiq to Execute a Job Immediately
Do I Have to Manually Uninstall All Dependent Gems
How to Overwrite a Printed Line in the Shell with Ruby
Notices for Sequence After Running Migration in Rails on Postgresql Application
Homebrew Installation on MAC Os X Failed to Connect to Raw.Githubusercontent.Com Port 443
Rails Form Validation Conditional Bypass
How to Express Infinity in Ruby
Differencebetween 'Try' and '&.' (Safe Navigation Operator) in Ruby
Create Custom HTML Helpers in Ruby on Rails
Travis Reports Odd Message of Corrupted Gemfile.Lock
Rails Active Admin CSS Conflicting with Twitter Bootstrap CSS
Ruby Ssl Error - Sslv3 Alert Unexpected Message
Functional Code Examples in Ruby