我的表單是對象驅動的並且使用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 –