简体   繁体   English

使用:symbol的form_for不起作用,错误[仅允许获取,放置和删除请求]

[英]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? 当我使用相同的形式但使用实例变量时,一切似乎都正常了,任何提示为什么用:symbol提交失败?

The error message says: Only get, put, and delete requests are allowed. 错误消息显示:只允许获取,放置和删除请求。

The code for the new form with :symbol is: 带:symbol的表单的代码是:

<% 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. 如果第一个参数是符号,则它描述表单所涉及的对象并命名实例变量,然后您需要提供URL。

The actual object can be used and then it will use your routes to try and determine the URL. 可以使用实际的对象,然后它将使用您的路由来尝试确定URL。 This means that it must know of a new_ecard_path and edit_ecard_path . 这意味着它必须知道new_ecard_pathedit_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. 如果您需要指定表单使用的URL,请使用符号并指定URL。

There are a few examples at http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html 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. 查看正在发生的事情的一个好方法是使用Firebug并检查生成的HTML。 If you don't have Firebug, get it now, it's really invaluable. 如果您没有Firebug,请立即获取,它真的是无价之宝。

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). 因此,这将向/ecards操作创建一个POST请求,该请求是create方法(顺便说ecard_url ,除非您另行定义,否则在create方法上方的注释应为POST ecards_url ,而不是ecard_url )。

However if you only use the :ecard symbol instead of the instance variable, you'll get: 但是,如果仅使用:ecard符号而不是实例变量,则会得到:

<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. 所有这些都是由于所谓的Magic Rails所做的-约定优于配置。 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. 但是正如danivo所说,如果您不想让这种魔术发生,您可以手动指定URL并显式声明表单的每个参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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