I use ruby ββon guide rails here
http://guides.rubyonrails.org/getting_started.html
In section 5.13: I get two different text values ββdisplayed on the submit button, but in the partial "_form" file, the code is exactly the same. Rails seems to automatically change text values. Where the code that does this happens in two views: new.html.erb and edit.html.erb.
(My question is for manual text management, but rather, I'm trying to figure out where this automatic behavior comes from in Rails.)
_partial
<%= form_for @post do |f| %>
<% if @post.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@post.errors.count, "error") %> prohibited
this post from being saved:</h2>
<ul>
<% @post.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<p>
<%= f.label :title %><br>
<%= f.text_field :title %>
</p>
<p>
<%= f.label :text %><br>
<%= f.text_area :text %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
posts_controller
class PostsController < ApplicationController
def new
@posts = Post.all
@post = Post.new
end
def show
@post = Post.find(params[:id])
end
def index
@posts = Post.all
end
def create
@post = Post.new(post_params)
if @post.save
redirect_to @post
else
render 'new'
end
end
def update
@post = Post.find(params[:id])
if @post.update(params[:post].permit(:title, :text))
redirect_to @post
else
render 'edit'
end
end
def edit
@post = Post.find(params[:id])
end
private
def post_params
params.require(:post).permit(:title, :text)
end
end
new.html.erb
A new message
<%= form_for :post, url: posts_path do |f| %>
<% if @post.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@post.errors.count, "error") %> prohibited
this post from being saved:</h2>
<ul>
<% @post.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<p>
<%= f.label :title %><br>
<%= f.text_field :title %>
</p>
<p>
<%= f.label :text %><br>
<%= f.text_area :text %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
<%= form_for :post do|f| %>
<% end %>
<%= link_to "List of Posts", posts_path %>
edit.html.erb
Edit message
<%= render 'form' %>
<%= link_to 'Back', posts_path %>