Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1005629
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T08:19:42+00:00 2026-05-16T08:19:42+00:00

I have a message form which uses validates_length_of in the model to limit the

  • 0

I have a message form which uses validates_length_of in the model to limit the length of the message.

validates_length_of   :content, :maximum => 1024

I “tested” this by entering 5k or so characters and submitted the form. But I get this 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 %>
....

Messages#index:

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

Messages#create:

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: (super ugly and needs major refactor)

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

I am not sure what exactly is going on here. Any help would be appreciated (even with the partial refactor). Thanks for reading my question.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T08:19:43+00:00Added an answer on May 16, 2026 at 8:19 am

    Hi When you entered 5k or so characters the Message become invalid record so on create worked else condition

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

    you have render here render doesn’t execute code inside action it just renders text file with your current data You haven’t assigned @messages inside your create action (I suppose you also haven’t done it in new) so it is nil when you render the index
    You should initialize it inside new or create or change render 'index' to render 'new'

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.