我的表单是对象驱动的并且使用form_for。我正在寻找2个窗体的1个对象的编辑页面。更具体地说,我期望让用户更改他的密码以及在同一页面上更改他的电子邮件。根据提交的表格,验证等应采取正确的措施。到目前为止,我无法做到这一点。2个表单,一个页面,一个对象在Rails中
我使用Rails 4
问题: 我的验证特效似乎是无法检测到邮件的改变/密码更改(见的观点隐藏字段),因为我不能在使用隐藏域PROC。所以我无法检测到我提交的表单类型。
请告诉我,如果有更多像Rails一样干净利落地完成这项工作,或者如果方法合适,我完全错过了一些东西。
以下是文件:
控制器/ accounts_controller.rb
def edit
set_pagedata('Account Informationen bearbeiten')
@account = @current_user
end
def update
@account = User.find(@current_user.id)
if @account.update(account_params)
redirect_to account_path
else
render 'edit'
end
end
private
def account_params
params.require(:user).permit(:email, :email_confirmation, :password, :password_confirmation)
end
型号/ user.rb
class User < ActiveRecord::Base
has_one :admin
has_many :customerships
has_many :customers, :through => :customerships
validates :salutation, presence: { message: 'Die Anrede wird benötigt!', if: :new_record? }
validates :prename, presence: { message: 'Der Vorname wird benötigt!', if: :new_record? }
validates :surname, presence: { message: 'Der Nachname wird benötigt!', if: :new_record? }
validates :email,
presence: { message: 'Die E-Mail Adresse wird benötigt!', if: Proc.new { | c | c.new_record? || c.email.present? } },
uniqueness: { message: 'Diese E-Mail existiert bereits in unserem System!', if: Proc.new { | c | c.new_record? || c.email.present? } }
validates :password,
confirmation: { message: 'Das Passwort wiederholen Feld muss dem Passwort Feld entsprechen!', if: [:new_record?, :password_change?, '!password.nil?'] },
length: { minimum: 5, maximum: 16, message: 'Das Passwort muss mindestens 5 und darf höchstens 16 Zeichen lang sein!', if: [:new_record?, :password_change?, '!password.nil?'] },
presence: { message: 'Das Passwort wird benötigt!', if: [:new_record?, :password_change?, '!password.nil?'] }
has_secure_password :validations => false
def admin?
Admin.find_by_user_id(self.id) != nil
end
def password_change?
:edit_type == 'password-change'
end
def mail_change?
:edit_type == 'mail-change'
end
end
的意见/帐号/ edit.html.erb
<div class="flash-stack">
<%= "<p class='text-danger'>#{@account.errors.full_messages.first}</p>".html_safe if @account.errors.any? %>
</div>
<div class="mysd-panel">
<div class="panel-heading">
<h3 class="panel-title">E-Mail Adresse ändern</h3>
</div>
<div class="panel-body">
<%= form_for @account, url: account_path, html: { :class => 'mysd-form' } do |f| %>
<ul class="form-container">
<li>
<%= f.text_field :email, :value => nil, :class => 'form-control', :placeholder => 'Neue E-Mail Adresse' %>
</li>
<li>
<%= f.text_field :password_confirmation, :value => nil, :class => 'form-control', :placeholder => 'Neue E-Mail Adresse wiederholen' %>
</li>
<%= f.hidden_field :edit_type, :value => 'password-change' %>
<%= submit_tag 'Absenden', :class => 'mysd-btn btn-primary' %>
</ul>
<% end %>
</div>
</div>
<div class="mysd-panel">
<div class="panel-heading">
<h3 class="panel-title">Passwort ändern</h3>
</div>
<div class="panel-body">
<%= form_for @account, url: account_path, html: { :class => 'mysd-form' } do |f| %>
<ul class="form-container">
<li>
<%= f.password_field :password, :value => nil, :class => 'form-control', :placeholder => 'Neues Passwort' %>
</li>
<li>
<%= f.password_field :password_confirmation, :value => nil, :class => 'form-control', :placeholder => 'Neues Passwort wiederholen' %>
</li>
<%= f.hidden_field :edit_type, :value => 'mail-change' %>
<%= f.submit 'Absenden', :class => 'mysd-btn btn-primary' %>
</ul>
<% end %>
</div>
</div>
':edit_type =='password-change''正在比较一个符号的字符串文字 - 这将永远是错误的。 e –