2015-04-18 67 views
1

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

':edit_type =='password-change''正在比較一個符號的字符串文字 - 這將永遠是錯誤的。 e –

回答

0

Rails這樣做的方式是隻使用一種形式:

<%= 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(leave blank if unchanged)' %> 
       </li> 
       <li> 
        <%= f.password_field :password_confirmation, :value => nil, :class => 'form-control', :placeholder => 'Neues Passwort wiederholen' %> 
       </li> 
<li> 
        <%= f.text_field :email, :value => @account.email, :class => 'form-control', :placeholder => 'Neue E-Mail Adresse' %> 
       </li> 

       <%= f.submit 'Absenden', :class => 'mysd-btn btn-primary' %> 
      </ul> 
     <% end %> 

因此,您使用兩種形式的任何特定原因?

+0

好的 - 但我如何區分驗證?例如如何說password_confirmation是必要的**在這種形式**如果密碼填寫並在同一時間不需要電子郵件? – Freddy

+0

如果您正在使用設計,它會添加驗證,這將需要密碼和password_confirmation字段被正確填寫。如果你使用'has_secure_password'來做你自己的驗證,它也會這樣做。進一步'@ account.update(account_params)'將批量更新給定的參數,只需將'value'從'nil'更改爲當前值('@ account.email') – CuriousMind

+0

感謝您的幫助。我不得不清除其他空白的參數。一種形式工作得很好:) – Freddy

相關問題