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 8668409
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T18:12:25+00:00 2026-06-12T18:12:25+00:00

I’m new to rails, and after completing Michael Hartl’s Ruby on Rails Tutorial, I

  • 0

I’m new to rails, and after completing Michael Hartl’s Ruby on Rails Tutorial, I am trying to expand some of the functionality. I started by changing the microposts so that they collect more data.

When running a rspec test, I can’t figure out why I am getting this error:

$ bundle exec rspec spec/models/micropost_spec.rb
...............F.....

Failures:

  1) Micropost 
     Failure/Error: it { should be_valid }
       expected valid? to return true, got false
     # ./spec/models/micropost_spec.rb:46:in `block (2 levels) in <top (required)>'

Finished in 1.57 seconds
21 examples, 1 failure

Failed examples:

rspec ./spec/models/micropost_spec.rb:46 # Micropost 

micropost_spec.rb

require 'spec_helper'

describe Micropost do

  let(:user) { FactoryGirl.create(:user) }
  before { @micropost = user.microposts.build(content: "Lorem ipsum", 
    title: "This is a test title", 
    privacy: "1", 
    groups: "This is a test Group", 
    loc1T: "21 Bond St. Toronto, Ontario",
    loc1Lat: "43.654653",
    loc1Lon: "-79.377627",
    loc2T: "21 Bond St. Toronto, Ontario",
    loc2Lat: "43.654653",
    loc2Lon: "-79.377627",
    startTime: "Jan 1, 2000 12:01:01",
    endTime: "Jan 2, 2000 12:01:01",
    imageURL: "http://i.cdn.turner.com/cnn/.e/img/3.0/global/header/hdr-main.gif") 

    puts @micropost.errors.messages
   }

  #before { @micropost = user.microposts.build(title: "This is a test title") }
  #before { @micropost = user.microposts.build(privacy: "1") }
  #before { @micropost = user.microposts.build(groups: "This is a test Group") }
  #before { @micropost = user.microposts.build(loc1T: "21 Bond St. Toronto, Ontario") }
  #before { @micropost = user.microposts.build(loc1Lat: "43.654653") }
  #before { @micropost = user.microposts.build(loc1Lon: "-79.377627") }
  #before { @micropost = user.microposts.build(loc2T: "21 Bond St. Toronto, Ontario") }
  #before { @micropost = user.microposts.build(loc2Lat: "43.654653") }
  #before { @micropost = user.microposts.build(loc2Lon: "-79.377627") }
  #before { @micropost = user.microposts.build(startTime: "Jan 1, 2000 12:01:01") }
  #before { @micropost = user.microposts.build(endTime: "Jan 2, 2000 12:01:01") }
  #before { @micropost = user.microposts.build(imageURL: "http://i.cdn.turner.com/cnn/.e/img/3.0/global/header/hdr-main.gif") }



  subject { @micropost }

  it { should respond_to(:content) }
  it { should respond_to(:user_id) }
  it { should respond_to(:user) }

  it { should respond_to(:title) }
  it { should respond_to(:privacy) }
  it { should respond_to(:groups) }
  it { should respond_to(:loc1T) }
  it { should respond_to(:loc1Lat) }
  it { should respond_to(:loc1Lon) }
  it { should respond_to(:loc2T) }
  it { should respond_to(:loc2Lat) }
  it { should respond_to(:loc2Lon) }
  it { should respond_to(:startTime) }
  it { should respond_to(:endTime) }
  it { should respond_to(:imageURL) }



  its(:user) { should == user }

  it { should be_valid }

  describe "accessible attributes" do
    it "should not allow access to user_id" do
      expect do
        Micropost.new(user_id: user.id)
      end.to raise_error(ActiveModel::MassAssignmentSecurity::Error)
    end    
  end

  describe "when user_id is not present" do
    before { @micropost.user_id = nil }
    it { should_not be_valid }
  end

  describe "with blank content" do
    before { @micropost.content = " " }
    it { should_not be_valid }
  end

  describe "with content that is too long" do
    before { @micropost.content = "a" * 141 }
    it { should_not be_valid }
  end
end

micropost.rb

class Micropost < ActiveRecord::Base
  attr_accessible :content, :title,:privacy,:groups,:loc1T,:loc1Lat,:loc1Lon,:loc2T,:loc2Lat,:loc2Lon,:startTime,:endTime,:imageURL



  belongs_to :user

  validates :user_id, presence: true
  validates :title, presence: true
  validates :privacy, presence: true
  validates :groups, presence: true
  validates :loc1T, presence: true
  validates :loc1Lat, presence: true
  validates :loc1Lon, presence: true
  validates :loc2T, presence: true
  validates :loc2Lat, presence: true
  validates :loc2Lon, presence: true
  validates :startTime, presence: true
  validates :endTime, presence: true
  validates :imageURL, presence: true

  validates :content, presence: true, length: { maximum: 140 }

  default_scope order: 'microposts.created_at DESC'

  def self.from_users_followed_by(user)
    followed_user_ids = "SELECT followed_id FROM relationships
                         WHERE follower_id = :user_id"
    where("user_id IN (#{followed_user_ids}) OR user_id = :user_id", 
          user_id: user.id)
  end
end

factories.rb

    FactoryGirl.define do
  factory :user do
    #name     "Michael Hartl"
    #email    "michael@example.com"
    sequence(:name)  { |n| "Person #{n}" }
    sequence(:email) { |n| "person_#{n}@example.com"} 
    password "foobar"
    password_confirmation "foobar"

    factory :admin do
      admin true
    end
  end

   factory :micropost do
    content "Lorem ipsum"
    title "This is a test title"
    privacy "1"
    groups "This is a test Group"
    loc1T "21 Bond St. Toronto, Ontario"
    loc1Lat "43.654653"
    loc1Lon "-79.377627"
    loc2T "21 Bond St. Toronto, Ontario"
    loc2Lat "43.654653"
    loc2Lon "-79.377627"
    startTime "Jan 1, 2000 12:01:01"
    endTime "Jan 2, 2000 12:01:01"
    imageURL "http://i.cdn.turner.com/cnn/.e/img/3.0/global/header/hdr-main.gif"

    user
  end
end

I’m not sure if there is anything else you need me to post.

  • 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-06-12T18:12:26+00:00Added an answer on June 12, 2026 at 6:12 pm

    This got rid of the errors.

    micropost_spec.rb

    describe Micropost do
    
      let(:user) { FactoryGirl.create(:user) }
      before { @micropost = user.microposts.build(content: "Lorem ipsum", 
        title: "This is a test title", 
        privacy: "1", 
        groups: "This is a test Group", 
        loc1T: "21 Bond St. Toronto, Ontario",
        loc1Lat: "43.654653",
        loc1Lon: "-79.377627",
        loc2T: "21 Bond St. Toronto, Ontario",
        loc2Lat: "43.654653",
        loc2Lon: "-79.377627",
        startTime: "Jan 1, 2000 12:01:01",
        endTime: "Jan 2, 2000 12:01:01",
        imageURL: "http://i.cdn.turner.com/cnn/.e/img/3.0/global/header/hdr-main.gif") 
    
    puts @micropost.errors.messages
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I want use html5's new tag to play a wav file (currently only supported
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build

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.