简体   繁体   English

无法在Rails中创建新模型

[英]Can't Create New Model in Rails

tl;dr : It seems like params[:commit] does not contain the actual relevant information to create a new Commit, instead, it only contains the value of the submit button for the form whose name is also "commit". tl; dr :似乎params[:commit]不包含创建新Commit的实际相关信息,而是仅包含名称也为“ commit”的表单的Submit按钮的值。 Any ideas as to why this is happening? 有什么想法为什么会这样? I did not change anything. 我什么都没改变。

I am running rails 3 with the webrick server since it displays relevant debugging information. 我正在用Webrick服务器运行rails 3,因为它显示了相关的调试信息。 I created a model with only one attribute, description:text , and everything seems to be working fine. 我创建了一个只有一个属性description:text ,并且一切似乎都工作正常。

However, when I go to create a new one using the auto-generated scaffolding form, it does not seem to care about the description text-area value. 但是,当我使用自动生成的脚手架表格创建一个新的表格时,它似乎并不关心描述文本区域的值。 In other words, after having been created, the created_at column is fine and all but the description does not show up at all. 换句话说,在创建之后, created_at列就可以了,除了description ,所有内容都不会显示。 Here is the output from the server: 这是服务器的输出:

Started POST "/commits" for 127.0.0.1 at 2010-11-03 17:24:20 -0700
Processing by CommitsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"F00A8Ttv7ceREegfZmP+T5kr+6u2YbRJrQzmfEOaT7o=", "commit"=>"Create Commit"}
SQL (0.5ms)  INSERT INTO "commits" ("created_at", "description", "updated_at") VALUES ('2010-11-04 00:24:20.986571', NULL, '2010-11-04 00:24:20.986571')
Redirected to http://0.0.0.0:3000/commits/5
Completed 302 Found in 42ms

Here is what my migration looks like, auto-generated by rails generate 这是我的迁移的样子,由rails generate自动rails generate

def self.up
  create_table :commits do |t|
    t.text :description

    t.timestamps
  end
end

So as you can see, it is seeing the description value as NULL even though I did type something into the text area. 如您所见,即使我确实在文本区域中键入了一些内容,它也将描述值视为NULL Here is what rails generated in the _form.html.erb partial: 这是_form.html.erb部分中生成的_form.html.erb

<div class="field">
  <%= f.label :description %><br />
  <%= f.text_area :description %>
</div>

Anyone have any ideas as to why this is happening? 任何人对为什么会这样有什么想法? I'm pretty sure it's some obvious thing too. 我很确定这也是一些显而易见的事情。

By the way, rails console works fine when I create one and save it manually, so I have a feeling there is a disconnect going on in the controller when it goes to save or something. 顺便说一句,当我创建并手动保存它时, rails console可以正常工作,因此我感觉到保存或其他操作时控制器中存在断开连接。

EDIT : I noticed something interesting. 编辑 :我注意到一些有趣的事情。 In the controller, the object is created as such: 在控制器中,对象的创建方式如下:

@commit = Commit.new(params[:commit])

However, as noted in the webrick output above, the parameters the server receives are only: 但是,如上面的webrick输出所示,服务器接收的参数仅为:

Parameters: {"utf8"=>"✓", "authenticity_token"=>"F00A8Ttv7ceREegfZmP+T5kr+6u2YbRJrQzmfEOaT7o=", "commit"=>"Create Commit"}

So it doesn't seem like it gets the description parameter, which I imagine should have been wrapped in the commit parameter, but then if I look at the source for the new form, it shows that the description's text area is of name commit[description] , but the submit button is of name commit . 因此,似乎它没有获得description参数,我想应该已经将其包装在commit参数中,但是如果我查看new表单的源代码,则表明该描述的文本区域的名称为commit[description] ,但“提交”按钮的名称为commit So somehow it's getting only the value of the submit button, which is indeed of value "Create Commit", and not the other information it requires. 因此,以某种方式,它仅获取提交按钮的值,而该按钮的确确实是“创建提交”的值,而不是其所需的其他信息。

I don't know that much about rails though so I don't really know if this is the case or what. 我对Rails不太了解,所以我真的不知道是这种情况还是什么。

Someone please help me out haha. 有人请帮我哈哈。

EDIT : Here is the rest of the _form.html.erb partial generated by rails. 编辑 :这是由rails生成的_form.html.erb部分的其余部分。 Perhaps you can spot the glaring issue: 也许您可以发现明显的问题:

<%= form_for(@commit) do |f| %>
  <% if @commit.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@commit.errors.count, "error") %> prohibited this commit from being saved:</h2>

      <ul>
      <% @commit.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :description %><br />
    <%= f.text_area :description %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

Answer : It seems that Rails 3 now automatically gives every submit button a name of "commit", which conflicts with the name of my model. :看来,Rails 3现在会自动为每个提交按钮赋予一个“ commit”名称,这与我的模型名称冲突。 I feared as much. 我也很担心 I'm wondering if there are any further implications on using this name. 我想知道使用此名称是否还有其他含义。 This problem was solved by explicitly changing the submit call to: 通过将submit调用显式更改为解决了此问题:

f.submit "Button Text", :name => "something_else"

By default, the rails scaffolding will create a form that will look something like the following: 默认情况下,rails脚手架将创建一个如下所示的表单:

<%= form_for @commit do |f| %>
  <%= f.label :description %>
  <%= f.text_area :description %>
  <%= submit_tag "Create" %>
<% end %>

Please note that I have collapsed the content of the partial into the form itself. 请注意,我已经将partial的内容折叠到了表单本身中。 The important thing to understand is what HTML is generated when this happens. 要了解的重要一点是,发生这种情况时会生成什么HTML。 It will look like this: 它看起来像这样:

<form action="/commits/create" method="post">
  <label for="commit_description">Description:</label>
  <textarea id="commit_description" name="commit[description]"></textarea>
  <input name="commit" type="submit" value="Create" />
</form>

The gotcha you've run into is a name collision between the submit button's name and the object's name. 您遇到的问题是提交按钮的名称和对象名称之间的名称冲突。 Normally, when rails encounters a form name like "commit[description]" it will store the results in @params so the value looks like this: 通常,当rails遇到诸如“ commit [description]”之类的表单名称时,它将结果存储在@params中,因此该值如下所示:

@params[:commit] = { :description => 'value' }

In fact, that is what rails did. 实际上,这就是Rails所做的。 The problem was that the default name generated by the "submit_tag" form helper is also named "commit". 问题在于由“ submit_tag”表单助手生成的默认名称也被命名为“ commit”。 So when rails encountered that form parameter it overwrote the results of the form like this: 因此,当rails遇到该form参数时,它将覆盖如下形式的结果:

@params[:commit] = "Create" # same name as the value of the submit button

To get around this name collision you have a couple options. 为了解决这个名称冲突,您有两种选择。 The first option is to hand write your submit button in plain old HTML. 第一种选择是用纯老式HTML手写您的提交按钮。 Sure you lose some of the options, but at least you can change the name of the submit button to something else: 当然,您会丢失一些选项,但是至少可以将“提交”按钮的名称更改为其他名称:

<input type="submit" name="who_cares" value="Create Commit" />

The other option is to use a variation of the 'form_for' helper. 另一个选择是使用'form_for'帮助程序的变体。 In this case the form's opening line would look like this: 在这种情况下,表单的开始行应如下所示:

<%= form_for :newcommit, @commit, :url => { :action => "create" } do |f| %>
  <%= f.label :description %>
  <%= f.text_area :description %>
  <%= submit_tag "Create" %>
<% end %>

That will change the resulting HTML to look like this: 这将使结果HTML看起来像这样:

<form action="/commits/create" method="post">
  <label for="newcommit_description">Description:</label>
  <textarea id="newcommit_description" name="newcommit[description]"></textarea>
  <input name="commit" type="submit" value="Create" />
</form>

That will bind your form data to the @params[:newcommit] parameter, and you can continue processing as per normal. 这会将您的表单数据绑定到@params [:newcommit]参数,您可以按照常规继续处理。

For your further reading enjoyment: 为了您的阅读乐趣:

http://guides.rubyonrails.org/form_helpers.html http://guides.rubyonrails.org/form_helpers.html

The form_for generates fields with name like commit[description] . form_for生成名称类似于commit[description]字段。 However, the last subit button have the name of commit . 但是,最后一个subit按钮的名称为commit I think the last submit button overwrites the fore commit[] s. 我认为最后一个提交按钮将覆盖最前面的commit[]

So please try <%= f.submit "Your button text", :name => "something_else" %> 因此,请尝试<%= f.submit "Your button text", :name => "something_else" %>

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

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