2016-09-27 9 views
0

私はコントローラに電子メールがデータベーステーブルに既に存在するかどうかをチェックするメソッドを持っています。 画像と似た、フラッシュを使用しない電子メールテキストボックスの下に検証メッセージを表示する必要があります[: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

何番目eの問題?あなたのコードdoesnt仕事ですか?何かエラーがありますか? – mrvncaragay

+0

@mrvncaragay上記のコードは動作します。しかし、それは "フラッシュ"メッセージを与える。電子メールアドレスがデータベースに存在しない場合、画像のようにテキストボックスの下の吹き出しに「メール変更に失敗しました!」というメッセージを表示する必要があります。 –

+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** 
関連する問題