简体   繁体   English

Ruby On Rails - 重用错误消息部分视图

[英]Ruby On Rails - Reusing the error message partial view

Problem 问题

I was trying to reuse the Error Message block in my Views. 我试图在我的视图中重用错误消息块。

Below was the block written in positions/_error_messages.html.erb 下面是位置/ _error_messages.html.erb中写的块

<% if @position.errors.any? %>
  <div id="error_explanation">
   <div class="alert alert-error">
     The form contains <%= pluralize(@position.errors.count, "error") %>.
   </div>
   <ul>
    <% @position.errors.full_messages.each do |msg| %>
    <li>* <%= msg %></li>
   <% end %>
   </ul>
 </div>
<% end %>

The problem was I have to created similar partial view in every Model which is kind of repeating the same code with different object ie @user, @client etc. 问题是我必须在每个模型中创建类似的局部视图,它是用不同的对象重复相同的代码,即@ user,@ client等。

Remedy 补救

I have created one erb in shared folder shared/_error_messages.html.erb and wrote the below code. 我在共享文件夹shared / _error_messages.html.erb中创建了一个erb,并编写了以下代码。

<% def error_message(active_object) %>
 <% if active_object.errors.any? %>
  <div id="error_explanation">
   <div class="alert alert-error">
    The form contains <%= pluralize(active_object.errors.count, "error") %>.
   </div>
   <ul>
    <% active_object.errors.full_messages.each do |msg| %>
     <li>* <%= msg %></li>
    <% end %>
   </ul>
  </div>
 <% end %>
<% end %>

and then in view file. 然后在视图文件中。 positions/new.html.erb I wrote the below code positions / new.html.erb我写了下面的代码

<div id="errorbox"> 
 <%= render "shared/error_messages" %>
 <%= error_message(@position) %>
</div>

It means now I can use the same code in all my Create and Update operations. 这意味着现在我可以在所有创建和更新操作中使用相同的代码。

I want to know, Is that a correct way to do it? 我想知道,这是一种正确的方法吗? Or is there any other option? 或者还有其他选择吗?

No, defining methods in views is not correct way to do it. 不,在视图中定义方法不是正确的方法。 I think you should rather substitute @position from your first partial with local variable named in more generic way, for example object and render this partial with: 我认为你应该用你的第一个部分替换@position用更通用的方式命名的局部变量,例如object并用以下方法渲染这个部分:

<%= render 'shared/error_messages', object: @position %>

which passes @position as local variable object to the partial. 它将@position作为局部变量object传递给partial。

<%= render partial: 'shared/error_messages', locals: {position: @position} %>

Now in your partial _error_messages.html.erb in the shared folder you can use the position variable. 现在,在共享文件夹中的部分_error_messages.html.erb中,您可以使用position变量。

Refer to http://api.rubyonrails.org/classes/ActionView/PartialRenderer.html for more help. 有关更多帮助,请参阅http://api.rubyonrails.org/classes/ActionView/PartialRenderer.html

It's not the best way to do it. 这不是最好的方法。 replace @position with something more generic like 'obj' (as an example) in your first block of code. 在第一个代码块中用@ obj(作为示例)更通用的东西替换@position。 So it will look like this. 所以它看起来像这样。

<% if obj.errors.any? %>
<div id="error_explanation">
<div class="alert alert-error">
The form contains <%= pluralize(obj.errors.count, "error") %>.
</div>
<ul>
<% obj.errors.full_messages.each do |msg| %>
<li>* <%= msg %></li>
<% end %>
 </ul>
</div>
<% end %>

All you did there was replace @position with obj. 你在那里所做的就是用obj替换@position。 Now take the above code, and put it inside your shared folder as '_error_messages.html.erb' 现在使用上面的代码,并将其作为'_error_messages.html.erb'放在共享文件夹中

Now for every file where you need the error messages you can render the partial and replace obj with whatever instance variable is used in that file. 现在,对于需要错误消息的每个文件,您可以呈现部分并将obj替换为该文件中使用的任何实例变量。 (at this point you would replace any error message code in your files with the code below, depending on which instance variable you are using. examples below) in positions: (此时,您将使用以下代码替换文件中的任何错误消息代码,具体取决于您使用的实例变量。下面的示例)位置:

<%= render 'shared/error_messages', obj: @position %>

in user: 在用户中:

<%= render 'shared/error_messages', obj: @user %>

in client: 在客户端:

<%= render 'shared/error_messages', obj: @client %>

etc... the obj: @client #or any instance variable switches out the 'obj' of the partial and puts in the instance variable in it's place. 等等... obj:@client #or任何实例变量都会切换出partial的'obj',并将实例变量放入其中。 Hope that helps! 希望有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM