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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T04:13:05+00:00 2026-06-11T04:13:05+00:00

I finished Michael Hartl’s Ruby on Rails Tutorial. Now I’m working on the suggested

  • 0

I finished Michael Hartl’s Ruby on Rails Tutorial. Now I’m working on the suggested exercises. The application he builds is basically a Twitter clone where one can post Microposts and they appear in your feed http://ruby.railstutorial.org/chapters/user-microposts#fig-micropost_created

The main page is in home.html.erb from the StaticPagesController and features a Micropost textbox where one can post Microposts. The code for the textbox looks like so:

<%= form_for(@micropost) do |f| %>
  <%= render 'shared/error_messages', object: f.object %>
  <div class="field">
    <%= f.text_area :content, placeholder: "Compose new micropost..." %>
  </div>
  <%= f.submit "Post", class: "btn btn-large btn-primary" %>
<% end %>

The @micropost variable is initialized in the StaticPagesController like so:

class StaticPagesController < ApplicationController
  def home
    if signed_in?
      @micropost = current_user.microposts.build
    end
  end

Now inside the MicropostsController there’s a create action like so:

def create
  @micropost = current_user.microposts.build(params[:micropost])
  if @micropost.save
    flash[:success] = "Micropost created!"
    redirect_to root_url
  else
    @feed_items = []
    render 'static_pages/home'
  end
end

My question is what is the purpose of the first @micropost variable as opposed to the second?

thanks,
mike

  • 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-11T04:13:06+00:00Added an answer on June 11, 2026 at 4:13 am

    The first @micropost becomes available to the view rendered by the first controller method; the second @micropost becomes available to the view rendered by the second controller method. And it just so happens that the two methods are rendering the same view.

    The only wrinkle is that since the second controller is conditional. If the create succeeds (passes validation and saves) then there’s a redirect, so there’s no proper view (although there will be in a moment, after the client-side redirect). But if it fails, then the view gets an object that contains the user-entered values as well as the validation errors which the view can then show to the user.

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

Sidebar

Related Questions

I am new to Rails and finished Michael Hartl's Ruby on Rails 3 Tutorial.
i m following michael hartl's ruby on rails online tutorial book and finished chapter
I'm trying to follow Michael Hartl's Ruby on Rails Tutorial in http://ruby.railstutorial.org/chapters/sign-in-sign-out , but
enter code hereI'm trying to finish up Michael Hartl's Ruby on Rails v3.2 Tutorial
In Michael Hartl's Ruby on Rails Tutorial, ch 7.2.3 , rspec is returns the
I was doing chapter 8 of Ruby on Rails by Michael Hartl, I finished
I finished the English version of my application and now I am working on
I finished my first ruby on rails application and I would like to put
I finished the English version of my application and now I am working on
I finished Hartl's tutorial and have been trying to add twitter-like @replies for the

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.