The text of the submit button comes from (Ruby on rails)?

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

# called by the posts_path function
def create
    @post = Post.new(post_params)
    if @post.save
        # Using redirect_to creates a new request.
        redirect_to @post
    else
        # Using render sends back the @post variable data!
        # i.e. uses same request.
        render 'new'
    end
end

# Can only have one instance of render of redirect_to.
#render text: params[:post].inspect
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

# For SQL injection prevention.
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 %>
+4
4

.submit.

, * -tag, , f , . , , .

yml , ,

en:
  helpers:
    submit:
      create: "Create a %{model}"
      update: "Confirm changes to %{model}"

.

I18n.

: http://apidock.com/rails/ActionView/Helpers/FormBuilder/submit

+6

:

<%= f.submit ( f.object.new_record? ? "Create" : "Update"), class: "btn" %>
+15

:

<%= f.submit %>

, :

<%= f.submit "My button text" %>

+1

move the submit button from the part of the form

the form

<%= 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>

New

 <%= render 'form' %>
 <%= f.submit "Create"%>
 <%= link_to 'Back', posts_path %>
 <% end %>

Edit

 <%= render 'form' %>
 <%= f.submit "Update"%>
 <%= link_to 'Back', posts_path %>
 <% end %>
+1
source

All Articles