我创建了一个部分表单,用于注册新用户或编辑用户。 问题出在提交按钮文本值。如何正确检查用户是否正在创建或编辑表单,因此按钮文本值可以是“注册或更新”
我正在使用
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' %>
答案 0 :(得分:1)
我认为你是在正确的道路上。您还可以检查当前操作以选择按钮的名称:
<%= 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'
。
答案 1 :(得分:0)
我发现了如何做到这一点。你只需要调用resource.new_record?
<%= f.submit resource.new_record? ? 'Sign up' : 'Update', class: 'btn btn-default' %>