plots2 icon indicating copy to clipboard operation
plots2 copied to clipboard

Refactor error messages in Ruby on Rails forms to get rid of "dynamic_form" gem

Open jywarren opened this issue 6 years ago • 14 comments

We use the dynamic_form gem in many places for reasons that we can't even remember -- so if we try to remove that gem, as we did in https://github.com/publiclab/plots2/pull/4575, tests fail. So let's fix this all and then circle back to that pull request to finish up!

Let's go through the uses of this gem everywhere we can find it and replace it with a more modern and standard form syntax! They look like this, using the error_messages property of the form_for block:

<%= form_for :revision, :as => :revision, :url => url, :html => {:class => "form"} do \|f\| %>
  <% if f.error_messages != "" %>
    <div class="alert alert-danger"><%= f.error_messages :header_message => "Your note couldn't be saved." %></div>
  <% end %>
<% end %>

But they should look like this:

  <% if @model.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@model.errors.count, "error") %> prohibited this model from being saved:</h2>
      <ul>
      <% @model.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

We can search to find where changes should be made, and the simpler of these may potentially be good first-timers-only issues, although if updating these cause tests to need updating too, that could be a potential barrier for a newcomer, so let's be sure to support people well in these!

Here are many instances that could be broken out into their own issues:

https://github.com/publiclab/plots2/search?q=error_messages&unscoped_q=error_messages

  • [ ] https://github.com/publiclab/plots2/blob/c60aaabb56c018962c76abf3f5f2d114f8492d15/app/views/features/new.html.erb#L10
  • [ ] https://github.com/publiclab/plots2/blob/c60aaabb56c018962c76abf3f5f2d114f8492d15/app/views/users/_edit_form.html.erb#L5
  • [ ] https://github.com/publiclab/plots2/blob/c60aaabb56c018962c76abf3f5f2d114f8492d15/app/views/editor/question.html.erb#L68

(we can keep adding links to instances in this list, as they are completed and checked off!)

jywarren avatar Oct 12 '20 17:10 jywarren

I've made a first-timers-only issue from one of these! See how it looks to perform one of these updates by seeing this commit:

https://github.com/publiclab/plots2/commit/eada0c0970ebf1bcce1e6dcf384dc43813e9384a

jywarren avatar Oct 12 '20 17:10 jywarren

Hey can I try working on this?

stacytonui avatar Oct 12 '20 17:10 stacytonui

Hi @stacytonui -- sure, can you choose one from the checklist above, and open a pull request with that one file change, similar to https://github.com/publiclab/plots2/commit/eada0c0970ebf1bcce1e6dcf384dc43813e9384a ? And link to it from here? Thank you so much!

jywarren avatar Oct 12 '20 17:10 jywarren

Okay let me work on the first one

stacytonui avatar Oct 12 '20 17:10 stacytonui

@jywarren apologies for the first PR, my repo wasn't synced with the main one

stacytonui avatar Oct 12 '20 19:10 stacytonui

@jywarren I'd like to work on the second one in plots2/app/views/users/_edit_form.html.erb if you can reserve it for me :) Will make a PR.

noi5e avatar Oct 12 '20 21:10 noi5e

Can I give this a shot with plots2/app/views/map/edit.html.erb ?

diorshelton avatar Oct 12 '20 21:10 diorshelton

https://github.com/diorshelton/plots2/pull/1#issue-501891635

diorshelton avatar Oct 13 '20 00:10 diorshelton

Hi, @diorshelton - I'd love your help - but unfortunately the map/edit.html.erb file is the one I opened up a "first-timers-only" issue for and reserved for @asu1996 here - https://github.com/publiclab/plots2/issues/8546

Would you be willing to take on a different one from the list, or from the search results above? Your code looks great! Thank you for your understanding!

@noi5e I'll check out yours now, thank you too!

jywarren avatar Oct 13 '20 03:10 jywarren

Hi @noi5e - are you working on this in https://github.com/publiclab/plots2/pull/8553 ? Need any help with the errors you're seeing? No worries, I'm here to support!

jywarren avatar Oct 13 '20 03:10 jywarren

Hi, @jywarren can I work on question.html.erb?

Cadreia avatar Oct 13 '20 04:10 Cadreia

@jywarren Yes, will reply to you in the PR, thank you!

noi5e avatar Oct 13 '20 07:10 noi5e

Hi, @jywarren can I work on question.html.erb?

Yes, that would be great!!!

jywarren avatar Oct 13 '20 16:10 jywarren

@jywarren - Looks like there are two instances in post.html.erb:

https://github.com/publiclab/plots2/blob/a5915411c504b8074235292ab49cfe1efc6f784d/app/views/editor/post.html.erb#L66

https://github.com/publiclab/plots2/blob/a5915411c504b8074235292ab49cfe1efc6f784d/app/views/editor/post.html.erb#L72

Happy to give these a shot if that's okay!

noi5e avatar Oct 13 '20 21:10 noi5e