0

I have one template for all forms in the app, however, I want to define different names for forms' submit buttons in different actions (for example, when I'm editing an article I want from submit button to show text Update article, and when I'm adding an article I want from submit button to show text Add article). Is there any way to do this but keep rendering same form template?

<%= form_for @article do |f| %>

    <% if @article.errors.any? %>
        <div id="error_explanation">
            <h2>
                <%= pluralize(@article.errors.count, "error") %>
                prohibited this article from saving
            </h2>
            <ul>
                <% @article.errors.full_messages.each do |msg| %>
                    <li><%= msg %></li>
                <% end %>
            </ul>
        </div>
    <% end %>

    <p>
        <%= f.label :title %>
        <%= f.text_field :title %>
    </p>

    <p>
        <%= f.label :text %>
        <%= f.text_area :text %>
    </p>

    <p>
        <%= f.submit %>
    </p>

<% end %>

This is ArticlesController:

class ArticlesController < ApplicationController

    http_basic_authenticate_with name: "username", password: "pass", except: [:index, :show]

    def index
        @article = Article.all
    end

    def show
        @article = Article.find(params[:id])
    end

    def new
        @article = Article.new
    end

    def edit
        @article = Article.find(params[:id])
    end

    def create
        @article = Article.new(article_params)

        if @article.save
            redirect_to @article
        else
            render 'new'
        end
    end

    def update
        @article = Article.find(params[:id])

        if @article.update(article_params)
            redirect_to @article
        else
            render 'edit'
        end
    end

    def destroy
        @article = Article.find(params[:id])
        @article.destroy

        redirect_to articles_path
    end

    private
        def article_params
            params.require(:article).permit(:title, :text)
        end

end
Nikola Stojaković
  • 2,257
  • 4
  • 27
  • 49

2 Answers2

2

Define a helper method to check controller & action name and then return button text as per you need

e.g

module ApplicationHelper
  def button_text
    if controller.action_name == "new"
       return "Add"
    elsif controller.action_name == "edit"
       return "Update"
    else
       return "Submit"
    end
  end
end

Then use the defined helper method in the button

<%= f.submit button_text %>

Also you can use controller_name, action_name helper to get controller and action name as of Rails4. See here

Community
  • 1
  • 1
Rahul Singh
  • 3,417
  • 2
  • 25
  • 32
  • 1
    no its a helper file, and ApplicationHelper is default helper file which generates when you run rails new command to create a new rails app. You can find it in app/helpers folder – Rahul Singh Mar 05 '16 at 15:44
0
<%= f.submit "My Submit Text" %>

Should work.

also, yo should also be able to pass class attrs via

<%= f.submit "My Submit Text", class: "class class class" %>

If you wish to use the same form, but have different actions, you will need to create a partial _form.html.erb

Inside you'll need to pass "local" variables.

in the _form... you'll have

<%= f.submit button_id, class: "" %> 
R.J. Robinson
  • 2,180
  • 3
  • 21
  • 33
  • I know how to name button and provide classes, but I want to show different text on buttons for different controller's actions (add / edit / show). If I provide one parameter on f.submit, it will be showed on all actions. – Nikola Stojaković Mar 05 '16 at 15:25