Rails 设计 gem 基于操作的部分表单提交按钮文本值

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

我创建了一个用于注册新用户或编辑用户的部分表单。 问题出在提交按钮文本值中。如何正确检查用户是否正在创建或编辑表单,以便按钮文本值可以是注册或更新

我正在使用

Ruby 2.1.5
Rails 4
Devise gem

这是我的表格:

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

所以我想检查一下这是新记录还是用户只是像这个例子一样编辑记录:

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

我认为你是对的。您还可以检查当前操作以选择按钮的名称:

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

编辑

您实际上可以将其保留为 <%= f.submit class: 'btn btn-default' %>,Rails 将知道它是什么操作,并将提交称为 'Create User''Update User'

我知道怎么做了。您只需调用 resource.new_record?

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