简体   繁体   中英

form_for using :symbol not working, error [Only get, put, and delete requests are allowed]

When submitting a form to create a new object i get an error message when submitting the form. When i use the same form but then with an instance variable everything seems to go fine, any clue why the submit with the :symbol fails?

The error message says: Only get, put, and delete requests are allowed.

The code for the new form with :symbol is:

<% form_for :ecard do |f| %>
    <%= label(:ecard, :title) %><br/>
    <%= f.text_field :title, :tabindex => "1" %>
    <%= f.submit "Create Ecard" %>
<% end %>

The form goes ok when i use

<% form_for @ecard do |f| %>
    <%= label(:ecard, :title) %><br/>
    <%= f.text_field :title, :tabindex => "1" %>
    <%= f.submit "Create Ecard" %>
<% end %>

Some code out of my controller :

# GET new_ecard_url
# return an HTML form for describing the new ecard
def new
    @ecard = Ecard.new
end

# POST ecard_url
def create
    # create an ecard
    @ecard = Ecard.new(params[:ecard])
    if @ecard.save
        flash[:notice] = "Succesfully created a new Ecard"
        redirect_to :action => 'index'
    else
        flash[:warning] = "Error when saving Ecard"
        render  :action => 'new'
    end
end

If the first argument is a symbol, it describes the object the form is about and name the instance variable, and then you need to provide the URL.

The actual object can be used and then it will use your routes to try and determine the URL. This means that it must know of a new_ecard_path and edit_ecard_path .
It will look at the object and see if it is a new record to determine which to use.

If you are using resource routes with the default restful routes, then you can probably just use the instance object. If you need to specify the URL that the form goes to, then use the symbol and specify the URL.

There are a few examples at http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html

A good way to see what's going on is to use Firebug and inspect the generated HTML. If you don't have Firebug, get it now, it's really invaluable.

If you create your form using the instance variable, you'll get something along the lines of:

<form id="new_ecard" class="new_ecard" method="post" action="/ecards">

So this will create a POST request to the /ecards action, which is the create method (by the way, your comment above the create method should be POST ecards_url , not ecard_url , unless you've defined it otherwise).

However if you only use the :ecard symbol instead of the instance variable, you'll get:

<form method="post" action="/ecards/new">

Since you haven't specified a URL, it uses the current one. This means your form will call itself in this case, and nothing will happen.

All this is due to all the so called magic Rails does - convention over configuration. But as danivo said, you can specify the URL manually and explicitly state each parameter for the form if you do not want to have this magic happen for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM