Valor_
Valor_

Reputation: 3591

Rails devise gem partial form submit button text value based on action

I have created a partial form which serves for register new or editing a user. The problem is in submit button text value. How to do a proper check if user is creating or editing a form, so button text value could be Sign up OR Update

I'm using

Ruby 2.1.5
Rails 4
Devise gem

This is my form:

<%= form_for(resource, as: resource_name, url: registration_path(resource_name))  do |f| %>
    <%= devise_error_messages! %>

    <div class="form-group">
      <%= f.label :email, class: 'control-label' %>*<br />
      <%= f.email_field :email, autofocus: true, class: 'form-control' %>
      <span class="help-block"></span>
    </div>

    <div class="form-group">
      <%= f.label :username, class: 'control-label' %>*<br />
      <%= f.text_field :username, class: 'form-control' %>
      <span class="help-block"></span>
    </div>

    <div class="form-group">
      <%= f.label :password, class: 'control-label' %>*
      <% if @validatable %>
          <em>(<%= @minimum_password_length %> characters minimum)</em>
      <% end %><br />
      <%= f.password_field :password, autocomplete: 'off', class: 'form-control' %>
      <span class="help-block"></span>
    </div>

    <div class="form-group">
      <%= f.label :password_confirmation, class: 'control-label' %>*<br />
      <%= f.password_field :password_confirmation, autocomplete: 'off', class: 'form-control' %>
      <span class="help-block"></span>
    </div>

    <hr>

    <div class="form-group">
      <%= f.label :name, class: 'control-label' %>*<br />
      <%= f.text_field :name, autocomplete: 'on', class: 'form-control' %>
      <span class="help-block"></span>
    </div>

    <div class="form-group">
      <%= f.label :last_name, class: 'control-label' %>*<br />
      <%= f.text_field :last_name, autocomplete: 'on', class: 'form-control' %>
      <span class="help-block"></span>
    </div>
    <div class="form-group">
      <%= f.label :address, class: 'control-label' %>*<br />
      <%= f.text_field :address, autocomplete: 'on', class: 'form-control' %>
      <span class="help-block"></span>
    </div>
    <div class="form-group">
      <%= f.label :post_number, class: 'control-label' %>*<br />
      <%= f.text_field :post_number, autocomplete: 'on', class: 'form-control' %>
      <span class="help-block"></span>
    </div>
    <div class="form-group">
      <%= f.label :city, class: 'control-label' %>*<br />
      <%= f.text_field :city, autocomplete: 'on', class: 'form-control' %>
      <span class="help-block"></span>
    </div>
    <div class="form-group">
      <%= f.label :mobile, class: 'control-label' %>*<br />
      <%= f.text_field :mobile, autocomplete: 'on', class: 'form-control' %>
      <span class="help-block"></span>
    </div>
    <div>
      <%= f.label :country_id, class: 'control-label' %><br />
      <%= f.select(:country_id, options_from_collection_for_select(Country.all, :id, :name), {}, { :class => 'form-control'}) %>
      <span class="help-block"></span>
    </div>
    <br>
    <div class="actions">
      <%= f.submit 'Sign up', class: 'btn btn-default' %>
    </div>
<% end %>

So i would like to check if this is going to be new record or user just edits the record like this example:

<%= f.submit @user.new_record? ? 'Sign up' : 'Update', class: 'btn btn-default' %>

Upvotes: 0

Views: 428

Answers (2)

Andrey Deineko
Andrey Deineko

Reputation: 52357

I think you are on the right way. You can also check the current action to pick the name of the button:

<%= f.submit "#{['edit', 'update'].include? params[:action] ? 'Update' : 'Sign up'}", class: 'btn btn-default' %>

EDIT

You can actually just leave it as <%= f.submit class: 'btn btn-default' %> and Rails will know what action is it and call the Submit as 'Create User' or 'Update User'.

Upvotes: 1

Valor_
Valor_

Reputation: 3591

I found out how to do that. You just simply call resource.new_record?

  <%= f.submit resource.new_record? ? 'Sign up' : 'Update', class: 'btn btn-default' %>

Upvotes: 0

Related Questions