2016-09-27 85 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**