I want to solve

I want to send a value with the new action so that an error message is displayed if the value is not normal.
As a method, we use a common template.
In addition, the information posted by the new action was not saved only by the ID by the create action. It could be solved with otional: true. I don't think it has anything to do with this one ...

Below is the code that seems to be related to what i am using this time.



<% if @ training.errors.any?%>

  <% model.errors.full_messages.each do | message |%>
  <li class ='error-message'><% = message%></li>
  <% end%>

<% end%>


Practice content 
 <% = form_with model: @training, id:'new_training', class:'new_training', local: true do | f |%>
 <% = render'share/error_messages', model: f.object%>
      <% = f.label: Select date, class:'date_lavel'%><br>
      <% = f.date_field: date, class:'select_date'%>
      <% = f.label: "Practice menu"%><br>
      <% = f.text_area: training_menu, autofocus: true, class: "training_menu"%>
      <% = f.label: "Purpose"%><br>
      <% = f.text_area: purpose, autofocus: true, class: "purpose"%>
      <% = f.label: "Reflection"%><br>
      <% = f.text_area: introspection, autofocus: true, class: "introspection"%>
          <% = f.submit "register", class: "register_btn"%>
 <% end%>


class TrainingsController


class Training

Migration file

class CreateTrainings

That's it.

We apologize for the inconvenience, but thanks for your understanding.

  • Answer # 1

    def create
        @training = Training.create (training_params)
        if @ training.save
        else else
          --direct_to request.referer
          + render: new #

    May be helpful: https://qiita.com/S-U-G-I/items/ce642e517a61315a99fe

Related articles