简单的validates_length_of会产生意外的NoMethodError

时间:2010-08-13 06:16:44

标签: ruby-on-rails validation model controller

我有一个消息表单,它使用模型中的validates_length_of来限制消息的长度。

validates_length_of   :content, :maximum => 1024

我通过输入5k左右的字符“测试”了这个并提交了表格。但是我得到了NoMethodError:

NoMethodError in Messages#create

Showing app/views/messages/_messages.html.erb where line #1 raised:

You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.each
Extracted source (around line #1):

1: <%- for message in @messages -%>
2:   <div class="message">
3:     <%= link_to message.title, message %>
....

消息#指数:

def index
  @title = "chat"
  @messages = Message.paginate :page => params[:page], :per_page => 8, :order => "updated_at DESC" 
  respond_to do |format|
    format.html
  end
end

消息#创建:

def create
  @message = Message.new(params[:message])
  respond_to do |format|
    if @message.save
      record_post_time
      flash[:notice] = "Created \"#{@message.title}\""
      format.html { redirect_to(messages_url) }
    else
      format.html { render :action => "index" }
    end
  end
end

_messages.html.erb :(超级丑陋,需要主要的重构)

<%- for message in @messages -%>
  <div class="message">
    <%= link_to message.title, message %>

    <%- if message.name.empty? -%>

    <%- else -%>   
      <span class="name">
        by 
        <%- if message.email.blank? -%>
          <%=h message.name %>
        <%- else -%>
          <a href="mailto:<%= message.email %>"><%=h message.name %></a>
        <%- end -%>
      </span>
    <%- end -%>

    <span class="time">
      active &#32;<%= timeago(message.updated_at) %>
    </span>

    <%- if message.comments.empty? -%>
      <span class="reply">
        <%= link_to 'reply', :controller => 'messages', :action => 'show', :id => message %>
      </span>
    <% else %>
      <span class="reply">
        <%= link_to pluralize(message.comments.count, 'reply'), :controller => 'messages', :action => 'show', :id => message %>
      </span>
    <%- end -%>

    <p><%= sanitize message.content,
 :tags => %w(a embed img object p param),
 :attributes => %w(allowfullscreen allowscriptaccess href name src type value) %></p>

    <% unless controller.controller_name == "tags" %>
      <%- unless message.tag_list.nil? || message.tag_list.empty? -%>
        <% message.tags.each do |t| %>
          <div class="tag"><%= link_to t.name.titleize, tag_path(t) %></div>
        <% end %>
      <%- end -%>
    <% end %>

    <%- unless message.comments.empty? -%>
      <div class="comments">
        <%= render :partial => message.firstcomments %>
        <%- if message.comments.count >= 4 -%>
          <%= link_to 'more...', :action => 'show', :id => message %>
        <%- end -%>
      </div>
    <%- end -%>
  </div>
<%- end -%>

我不确定这里到底发生了什么。任何帮助将不胜感激(即使使用部分重构)。感谢您阅读我的问题。

1 个答案:

答案 0 :(得分:1)

嗨当您输入5k左右的字符时,Message会变为无效记录,因此创建有效else条件

else
  format.html { render :action => "index" }
end

您在此处进行渲染render不执行代码内部操作它只是使用您当前数据呈现文本文件您尚未在@messages操作中分配create(我想你还没有在new中完成,所以当你渲染nil时它是index 您应该在newcreate内进行初始化,或将render 'index'更改为render 'new'