Submit button text comes from where (Ruby on rails)?
Asked Answered
C

4

5

I'm using the ruby on rails guide here http://guides.rubyonrails.org/getting_started.html

In section 5.13: I am getting two different text values displayed on the submit button, but in the "_form" partial file, the code is exactly the same. Rails seems to automatically change the text values somehow. Where is the code that is making this happen in the two views: new.html.erb and edit.html.erb.

(My question to control the text manually, but rather, I'm trying to understand where this automatic behavior is coming 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's 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

New Post

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

  <%= render 'form' %>

  <%= link_to 'Back', posts_path %>
Card answered 25/12, 2013 at 3:58 Comment(3)
I doubt you're still out there, but for future Googlers, this section in the Guide is pretty helpful: guides.rubyonrails.org/layouts_and_rendering.html When I understand it better myself, I'll post a real answer instead of yet another RTM. :DHyetal
which section of that page?Card
I started with Section 2.1. The more I played around with it, the more sense it started to make to me.Hyetal
C
7

The different text comes from the .submit helper method.

It's a form builder helper as opposed to a *-tag helper, which means it's called on the f, or whatever you set as your parameter in the form block. However, the tag helper outputs the dynamic text just like the form builder method as well.

It references some yml file that specifies this,

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

making the text dynamic to the model name.

Customizing it can be done with the I18n gem.

All of this is described here: http://apidock.com/rails/ActionView/Helpers/FormBuilder/submit

Card answered 7/10, 2014 at 8:31 Comment(0)
D
15

if you want to change text at form's submit button:

<%= f.submit ( f.object.new_record? ? "Create" : "Update"), class: "btn" %>
Duyne answered 6/9, 2014 at 11:58 Comment(0)
C
7

The different text comes from the .submit helper method.

It's a form builder helper as opposed to a *-tag helper, which means it's called on the f, or whatever you set as your parameter in the form block. However, the tag helper outputs the dynamic text just like the form builder method as well.

It references some yml file that specifies this,

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

making the text dynamic to the model name.

Customizing it can be done with the I18n gem.

All of this is described here: http://apidock.com/rails/ActionView/Helpers/FormBuilder/submit

Card answered 7/10, 2014 at 8:31 Comment(0)
M
1

The submit button code is as follows:

<%= f.submit %>

You can change the text of this button by simply adding the value after the element declaration:

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

You can read more about this here

Munch answered 25/12, 2013 at 10:35 Comment(1)
To keep it dry, i suppose i can define a variable in a helper or something. Would you also happen to know how / where rails controls this?Card
I
1

move the submit button from form partial

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 %>
Ikhnaton answered 25/12, 2013 at 11:17 Comment(1)
This makes perfect sense, and I would have done this too. However, it feels the form stuff should stay in the form stuff. to be semantic, and not have irregular locations for code... although for this case, it's probably not a big deal.Card

© 2022 - 2024 — McMap. All rights reserved.