sarunw
sarunw

Reputation: 8176

How rails issue put method on update?

On edit.html.erb file, scaffold created, I don't see any code specified PUT method. How come the post form call update action with PUT method.

Upvotes: 0

Views: 7112

Answers (3)

Ahmad Ramdani
Ahmad Ramdani

Reputation: 208

You can see put method on edit when you run

rake routes


new_account  GET    /account/new(.:format)  {:action=>"new", :controller=>"accounts"}
edit_account GET    /account/edit(.:format) {:action=>"edit", :controller=>"accounts"}
account      GET    /account(.:format)      {:action=>"show", :controller=>"accounts"}
             PUT    /account(.:format)      {:action=>"update", :controller=>"accounts"} //this is your method needed
             DELETE /account(.:format)      {:action=>"destroy", :controller=>"accounts"}
             POST   /account(.:format)      {:action=>"create", :controller=>"accounts"}

<% form_for @user do |form| %> can be <% form_for :user, :url => user_url(:user), :method => :put do |form| %>

Upvotes: 1

Doug Neiner
Doug Neiner

Reputation: 66201

The form_for function will make its best guess in where to send the form:

Take the following code for instance:

<% form_for @user do |form| %>
  1. If @user is a new record, it will send a POST request to /users
  2. If @user is an existing record, it will send a PUT request to /users/12 (If 12 = @user.id)

Upvotes: 4

zetetic
zetetic

Reputation: 47548

The standard Rails way to handle updates is to have an edit template (your edit.html.erb), which generates a form that is filled in by the user and then submitted as an HTTP PUT request. Your Rails app should have a model for the resource that is being updated, with a controller that has an 'update' action to accept the form parameters.

If you want to see the data that is being sent in the PUT request, you can use Firebug.

Upvotes: 0

Related Questions