2016-09-27 86 views
0

我在控制器中有一個方法來檢查數據庫表中是否已經存在電子郵件。 我需要在類似於圖像的電子郵件文本框下方顯示驗證消息,而不是使用flash [:error] =「メール変更に失敗しました!」。查看文件中的自定義字段驗證消息

驗證在控制器完成的,在這裏我們檢查電子郵件-ID

enter image description here

控制器的方法:

def update_email 
    @user_login = UserLogin.find(params[:id]) 

    if @email_check = UserLogin.where(:email => params[:update_email]).first 
    redirect_to "/works?utf8=%E2%9C%93&search=&commit=%E6%A4%9C%E7%B4%A2", notice: params[:id] + 'idの メールは既に存在します!' 
    else 
     @update_email = UserLogin.where(:user_id => @user_login).update_all(:email => params[:update_email]) 

    if @update_email 
    UserLogin.where(:user_id => params[:id]).update_all(:updated_at => Time.zone.now) 
    flash[:success] = "メール変更!" 
    redirect_to works_path 
    else 
    flash[:error] = "メール変更に失敗しました!" 
    redirect_to works_path 
    end 
    end 
    end 

index.html.erb:

<%= form_for :works,:id => "email_validate", :url => {:action => 'update_email',:id => userdetails.user_id} do %> 
<td><%= email_field_tag :update_email,nil,:id =>"email",:class => "form-control" ,:placeholder =>'新しいメールアドレス。。。。', required: true %><br/></td> 
<td><%= button_to("変更", {}, {:onclick => "return confirm('メールをリセットします。よろしいでしょうか?')", :class => "btn btn-primary", :method => :update_email, :remote => true}) %></td> 
<% end %> 
+0

什麼是th問題?你的代碼不工作?有沒有錯誤? – mrvncaragay

+0

@mrvncaragay以上代碼有效。但它提供了一個「FLASH」消息。如果電子郵件地址在數據庫中不存在,我需要在文本框下方的氣球中顯示「メール変更に失敗しました!」的信息。 –

+0

所以它確實重定向,但通知沒有顯示? – mrvncaragay

回答

0
In controller: 

    flash[:notice] = 'メールは既に存在します!!' 

    redirect_to works_path 

In view: 

    <%= form_for :maintanance_works, :url => {:action => 'update_email',:id => userdetails.user_id} do %> 
     <td> 
     <%= email_field_tag :update_email, nil,:class => "form-control" ,:placeholder =>'新しいメールアドレス。。。。', required: true %><br/> 
     <p class="notice" style="color:red;font-size:12pt;"> 
     <%= flash[:notice] %></p> 
     </td> 
     <td> 
     <%= button_to("変更", {}, {:onclick => "return confirm('メールをリセットします。よろしいでしょうか?')", :class => "btn btn-primary", :method => :update_email, :remote => true}) %></td> 
    <% end %> 

**Display flash message below your textbox**