2012-04-12 2 views
3

をのform_for使用しようとすると、私はこのエラーを取得していリターンのform_forの使用:未定義のメソッドが `nilには参加:</p> <p>_feedback_form.html.erb:NilClass

がこのエラーを返し実行
<%= form_for @support, :html => { :method => :post } do |f| %> 
    <%= f.text_area :content, :class => "quick_feedback_form", :input_html => { :maxlength => 450 } %> 
    <%= f.hidden_field :email, :value => current_user.email %> 
    <div><%= f.submit "Send!", :disable_with => "Sending...", :class => "button white" %></div> 
<% end %> 

NoMethodError in Pages#guardian 

Showing /home/alex/myapp/app/views/supports/_feedback_form.html.erb where line #1 raised: 

undefined method `join' for nil:NilClass 

@support変数が作成され、nilではない - ログから確認できます。これはRails 3.0でもうまく機能していましたが、今はRails 3.2に移行した後、何とかしなくなりました。

ここには、サポート変数のクラスがあります。これは、管理者に電子メールフィードバックを送信するために使用されます。

class Support < ActiveRecord::Base 
include ActiveModel::Validations 

    validates_presence_of :content 
    # to deal with form, you must have an id attribute 
    attr_accessor :id, :email, :sender_name, :content 

    def initialize(attributes = {}) 
    attributes.each do |key, value| 
     self.send("#{key}=", value) 
    end 
    @attributes = attributes 
    end 

    def read_attribute_for_validation(key) 
    @attributes[key] 
    end 

    def to_key 
    end 

    def save 
    if self.valid? 
     Feedback.support_notification(self).deliver 
     return true 
    end 
    return false 
    end 
end 

は異なるものを試しましたが、それでもここに間違っているものを見ることができません。 Rails 3.2.1。

答えて

2

私は問題があなたのto_keyメソッドだと思います。ここでこのメソッドを定義する理由はありますか?既にActiveRecord::Baseに定義されているため、内部メソッドをオーバーライドしています。あなたのモデルから取り除くだけで、それはあなたの問題を解決するはずです。

あなたはpersistedモデル(例えばActiveRecordモデル)のために、それがモデルのキー属性を持つ配列を返す必要があることを覚えて、いくつかの理由で、ここto_keyを定義したい場合。

def to_key 
    [self.id] 
end 

そして、なぜあなたはActiveModel::Validationsを含めている。だから、次のようになりますか?これはActiveRecord::Baseで行われます。

+0

うわー、ありがとう、私はちょうどそのto_keyメソッドをコメントし、それは働いた!おそらく、古いRailsのいくつかの例からのものでした。 –

+0

フォームオブジェクトで遊んでいたときにこの問題に遭遇しました。私の場合、フォームオブジェクトにto_keyメソッドを追加することで問題は解決しました。ありがとう! –

関連する問題