0

私はMongoid、awesome_nested_fields gem、rails3.2.8を使用しています。

クライアントで機能している(複数のフィールドを追加する)機能がありますが、保存しようとすると、「nil:NilClassの未定義のメソッド`update_attributes'」エラーが発生します。

関連するすべての情報は次のとおりです。

profile.rb

class Profile
    include Mongoid::Document
    include Mongoid::Timestamps

    has_many :skills, :autosave => true
    accepts_nested_attributes_for :skills, allow_destroy: true  
    attr_accessible :skills_attributes

end

スキル.rb

class Skill
    include Mongoid::Document
    belongs_to :profile 
    field :skill_tag, :type => String
end

意見

<%= simple_form_for(@profile) do |f| %>
  <%= f.error_notification %>

  <div class="form-inputs">

            <div class="items">
                <%= f.nested_fields_for :skills do |f| %>
                <fieldset class="item">
                    <%= f.input :skill_tag, :label => 'Skills:' %>
                    <a href="#" class="remove">Remove Skill</a>

                    <%= f.hidden_field :id %>
                    <%= f.hidden_field :_destroy %>
                </fieldset>
                <% end %>
            </div>
            <a href="#" class="add">Add Skill</a>
      </div>

  <div class="form-actions">
    <%= f.button :submit, :class => 'btn' %>
  </div>
<% end %>

profiles_controller.rb

  # PUT /profiles/1
  # PUT /profiles/1.json
  def update
    @profile = Profile.find(params[:id])

    respond_to do |format|
      if @profile.update_attributes(params[:profile])
        format.html { redirect_to @profile, notice: 'Profile was successfully updated.' } # Notice
        format.json { head :no_content }
      else
        format.html { render action: "edit" }
        format.json { render json: @profile.errors, status: :unprocessable_entity }
      end
    end
  end

** * ** * ** * **アップデート** * ** * ** * ***

結局、ライアンのnested_form gemに切り替えることになり、それは魅力のように機能します。

4

1 に答える 1

0

メッセージは、失敗した実行行を直接指しています。つまり、次の行です。

if @profile.update_attributes(params[:profile])

問題は、@profileインスタンス変数があり、Rails がオブジェクトのメソッドをnil見つけられないことです。update_attributesnil

アプリを起動したターミナルに移動することで、サーバーログでparamsハッシュを簡単にチェックして、何を確認できますかparams[:id](おそらくまったく定義されていません)。

または、アプリ フォルダーにいるときに開発ログを確認できます。

tail -n 1000 development.log | egrep params
于 2012-10-26T21:00:39.527 に答える