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

しかし、コントローラー側でパラメーターを出力しようとすると、3つのうち2つだけが出力されます。これは「置く」

      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のは、すべての Ruby オブジェクトが拡張するクラスである Object で定義されたメソッドであるためです。つまり、Ruby 自体によって予約されており、データベース フィールド/モデル属性の名前付けには使用できません。その列/関連付けの名前を別のものに変更する必要があります。

補足として、クラス名に「オブジェクト」という単語を含めることは、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 に答える