Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby Rails Routes and Resources Routes to Update Actions An Edit Form

Andrew Merritt
Andrew Merritt
1,753 Points

f.submit button text

When the form partial is rendered from the new.html.erb file the submit button reads "Create Page". When it is rendered from the edit.html.erb file the submit button reads "Update Page". How does rails determine the difference when rendering the file? Is there a way to change the button text? And why does one button send a Post, while the other button sends a Patch when the code for the button is exactly the same? Namely <%= f.submit %>

2 Answers

Maciej Czuchnowski
Maciej Czuchnowski
36,441 Points

Rails determines whether the form applies to a new object or an existing, persisted object. There's an ActiveRecord method for checking this if you want to play around: http://apidock.com/rails/ActiveRecord/Persistence/new_record%3F

You can change this text if you want. It's as simple as:

<%= f.submit "Save" %>

If you want this to be a different text based on whether the object is new or persisted, I guess you'd have to write a helper that checks the object using the new_record? method.

Andrew Merritt
Andrew Merritt
1,753 Points

Thanks, that makes a lot of sense!