Home>
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.

code

share/_errors.message.html.erb

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

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

<% end%>


trainings/new.html.erb

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%>

trainings_controller

class TrainingsController

trainings.rb

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
          redirect_to'/'
        else else
          --direct_to request.referer
          + render: new #
        end
      end


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

Related articles