0

我将 Rails 4.2.3 与 MySql 5.5.37 一起使用。我最近更改了列的名称和类型,现在当我提交 Rails 表单时,表单字段“selection_id”没有被捕获。下面是我的 Rails 表格...

      <div class="field">
        <%= f.label :day %><br>
        <%= f.hidden_field :day, :validate => true %>
        <div id="datepicker"></div>
      </div>
      <div class="field">
        <%= f.label :selection_id %><br>
        <div class="styled-select"><%= collection_select(:user_selection, :selection_id, @selections, :id, :description, {:prompt => true}) %></div>
      </div>
      <div class="field">
        <%= f.label :total %><br>
        <%= f.text_field :total, :size => 4, :validate => true %>
      </div>

这是输出到浏览器的 HTML ...

    <div class="field">
       <label for="user_selection_day">Day</label><br>
       <input validate="true" type="hidden" value="02/07/2016" name="user_selection[day]" id="user_selection_day" />
       <div id="datepicker"></div>
     </div>
     <div class="field">
       <div class="field_with_errors"><label for="user_selection_selection_id">selection</label></div><br>
       <div class="styled-select"><div class="field_with_errors"><select name="user_selection[selection_id]" id="user_selection_selection_id"><option value="">Please select</option>
    <option value="3">option 1</option>
    <option value="4">option 2</option></select></div></div>
      </div>
      <div class="field">
        <label for="user_selection_total">Total</label><br>
        <input size="4" validate="true" type="text" value="1" name="user_selection[total]" id="user_selection_total" />
      </div>

我可以看到这些数据在 Chrome 中提交...

    user_selection[day]:02/19/2016
    user_selection[selection_id]:3
    user_selection[total]:9

但是在我的控制器方面,当我尝试输出参数时,三个中只有两个正在打印出来。这个“放”

      def create
        @current_user = User.find(session["user_id"])
        ...
        puts user_selection_params

印刷

    {"day"=>"02/19/2016", "total"=>"9"}

为什么这个其他领域会丢失,我该如何解决?

4

2 回答 2

1

您遇到此问题是因为#object_id它是在 Object 上定义的一个方法,Object 是所有 Ruby 对象扩展的类。换句话说,它是 Ruby 自己保留的,不能在命名数据库字段/模型属性时使用。您将不得不将该列/关联重命名为其他名称。

作为旁注,在你的类名中包含单词“Object”不是惯用的 Ruby。可以说这个类应该被命名User,而不是UserObject.

参考:http ://ruby-doc.org/core-2.3.0/Object.html#method-i-object_id

于 2016-03-01T00:14:08.177 回答
1

请不要在您的模型中使用object_id 。

数据库中不应有名为object_id 的列。

object_id是所有(Ruby 1.9 中的 BasicObject 除外)对象都具有的默认方法...

(见文档)。

于 2016-03-01T02:46:01.630 回答