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

时间:2021-07-28 07:35:10

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 i.e. @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?

3 个解决方案

#1


8  

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作为局部变量对象传递给partial。

#2


3  

<%= 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中,您可以使用位置变量。

Refer to http://api.rubyonrails.org/classes/ActionView/PartialRenderer.html for more help.

有关更多帮助,请参阅http://api.rubyonrails.org/classes/ActionView/PartialRenderer.html。

#3


1  

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. So it will look like this.

这不是最好的方法。在第一个代码块中用@ obj(作为示例)更通用的东西替换@position。所以它看起来像这样。

<% 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. Now take the above code, and put it inside your shared folder as '_error_messages.html.erb'

你在那里所做的就是用obj替换@position。现在使用上面的代码,并将其作为'_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. (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:

现在,对于需要错误消息的每个文件,您可以呈现部分并将obj替换为该文件中使用的任何实例变量。 (此时,您将使用以下代码替换文件中的任何错误消息代码,具体取决于您使用的实例变量。下面的示例)位置:

<%= 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. Hope that helps!

等等... obj:@client #or任何实例变量都会切换出partial的'obj',并将实例变量放入其中。希望有所帮助!

#1


8  

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作为局部变量对象传递给partial。

#2


3  

<%= 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中,您可以使用位置变量。

Refer to http://api.rubyonrails.org/classes/ActionView/PartialRenderer.html for more help.

有关更多帮助,请参阅http://api.rubyonrails.org/classes/ActionView/PartialRenderer.html。

#3


1  

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. So it will look like this.

这不是最好的方法。在第一个代码块中用@ obj(作为示例)更通用的东西替换@position。所以它看起来像这样。

<% 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. Now take the above code, and put it inside your shared folder as '_error_messages.html.erb'

你在那里所做的就是用obj替换@position。现在使用上面的代码,并将其作为'_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. (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:

现在,对于需要错误消息的每个文件,您可以呈现部分并将obj替换为该文件中使用的任何实例变量。 (此时,您将使用以下代码替换文件中的任何错误消息代码,具体取决于您使用的实例变量。下面的示例)位置:

<%= 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. Hope that helps!

等等... obj:@client #or任何实例变量都会切换出partial的'obj',并将实例变量放入其中。希望有所帮助!