Alexander Savin
Alexander Savin

Reputation: 6442

Using form_for returns: undefined method `join' for nil:NilClass

I'm getting this error when trying to use form_for:

_feedback_form.html.erb:

<%= 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 %>

Running this returns this error:

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

The @support variable is created and is not nil - I can see it from the log. This was also working nicely in Rails 3.0, and now after migrating to Rails 3.2 this is somehow broken.

Here is a class of Support variable. This is used for sending email feedback to the admin:

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

Tried different things but still can't see what is wrong here. Rails 3.2.1.

Upvotes: 3

Views: 1151

Answers (1)

tsherif
tsherif

Reputation: 11710

I think the problem is your to_key method. Is there a reason you're defining this method here? It's already defined in ActiveRecord::Base, so you're overriding the internal method. If you just take it out of your model, that should fix your problem.

If you do want to define to_key here for some reason, remember that for persisted models (e.g. ActiveRecord models), it should return an array with the key attributes of the model. So it should look something like this:

def to_key
  [self.id]
end

And why are you including ActiveModel::Validations? This is done for you in ActiveRecord::Base.

Upvotes: 2

Related Questions