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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T04:58:46+00:00 2026-06-10T04:58:46+00:00

If we create the default scaffold in Rails, both the edit.html.erb and new.html.erb render

  • 0

If we create the default scaffold in Rails, both the edit.html.erb and new.html.erb render the same _form.html.erb within. Both create forms with certain similarities and differences.

Such as:

  • Both create <form method="post" ...>
  • The submit buttons have different texts <input value='Create model'
    ..
    and <input value='New model' ..

My questions:

  1. How does the conditional rendering work?
  2. How to display form elements conditionally? E.g., show this
    <input> only if it is called via edit.html.erb, but do not show
    it if called via new.html.erb.
  3. If the method in q.2 possible, is it the right way? We are reusing
    code instead of replicating the form all over again, isn’t it?
  • 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-10T04:58:48+00:00Added an answer on June 10, 2026 at 4:58 am

    Assuming you’re following RESTful conventions, the differences you see between edit and new are based on the state of the object that you pass to the form. Rails can tell the difference between a new object and one that has been persisted by using the #new_record? method.

    Model.new.new_record? # => true
    Model.first.new_record? # => false
    

    In your #new controller action, you probably have something like:

    @model = Model.new
    

    In your #edit action, you probably have something like:

    @model = Mode.find(params[:id])
    

    This @model object is then passed to the form, which handles the conditional logic internally. Another difference in the form you should notice is that the #edit version has a hidden input field that tells the server to use the PUT HTTP method.

    Update

    It looks like Rails actually uses the persisted? method internally as opposed to new_record?. The difference is that persisted? checks whether the record has been deleted. Otherwise, they are identical (but opposite)

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

Sidebar

Related Questions

Under rails 3.1 If I create a fresh new rails project, and scaffold a
I use play2 to create a default scala app: play new test Then: play
I am new in MVC3. When i create a new default ASP.NET MVC 3
I'm trying to create a mutable Map with a default that creates a new
When I use Rails scaffold, I can't access the page to edit fields. It
The default scaffold command creates code like the following: respond_to do |format| format.html #
If I want to change the default controller template created by scaffold in Rails
I have a brand new Rails 3.1rc4 project. After generating a scaffold, migrating and
I have all the default settings from paperclip's website. When I create a new
I am trying to set a default value for the scaffold create view that

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.