Ruby On Rails - Reusing Partial Error Message Submission

Problem

I tried to reuse the error message block in my views.

Below was a block written in the positions /_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 that I have to create a similar partial view in each model that repeats the same code with another object, i.e. @user, @client, etc.

Elimination

I created one erb in the shared folder shared / _error_messages.html.erb and wrote the code below.

 <% 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 the view file. position / new.html.erb I wrote the code below

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

This means that now I can use the same code in all my Create and Update operations.

I want to know is the right way to do this? Or is there another option?

+6
source share
3 answers

No, defining methods in views is the wrong way to do this. I think you should replace @position from your first partial with a local variable called in a more general way, like object , and make it partial with:

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

which passes @position as a local variable object to partial.

+8
source
 <%= 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.

See http://api.rubyonrails.org/classes/ActionView/PartialRenderer.html for more details.

+3
source

This is not the best way to do this. replace @position with something more general, like "obj" (as an example) in your first block of code. So it will look.

 <% 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 have done has replaced @position with obj. Now take the above code and put it in your shared folder as '_error_messages.html.erb'

Now for each file where you need error messages, you can display partial and replace obj with any instance variable in this file. (at this moment you will replace any error message code in your files with the code below, depending on the instance variable you are using. examples below) in the provisions:

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

at the user:

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

in the client:

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

etc .... obj: @client # or any instance variable disables the partial obj and puts the instance variable in it. Hope this helps!

+1
source

Source: https://habr.com/ru/post/952649/


All Articles