简体   繁体   中英

Rails displaying validation messages on form

I am trying to validate a data model that belongs_to a project model. The validation works as data does not get saved if it fails the validation.

However, I can't get the data validation to display the correct error message from the validation on the new form.

How do I display the correct error validation messages on the data form?

Form URL http://localhost:9080/projects/7/data/new

Data Model:

class Datum < ApplicationRecord
  belongs_to :project
  validates :supplier, :item, :presence => true #need this to display error message on data form
end

Project Model:

class Project < ApplicationRecord
  belongs_to :user
  validates :name,:presence => true #works perfectly, displays error message on project form
  has_many :data, dependent: :destroy
  accepts_nested_attributes_for :data
end

Data Controller

def create
  @datum = Datum.new(datum_params)
  @datum.project_id = params[:project_id]

  respond_to do |format|
    if @datum.save
      format.html { redirect_to project_data_path, notice: 'Created.' }
    else
      format.html {#do I need something here to redisplay the form with the errors }
    end
  end
end

Data Form:

<%= bootstrap_form_for([@project, @project.data.build]) do |f| %>
  <div class="field">
    <%= f.text_field :supplier %>
  </div>

  <div class="field">
    <%= f.text_field :item %>
  </div>

  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

EDIT The closest I got to a solution is if the object doesn't save:

format.html { redirect_to new_project_datum_path, alert: @datum.errors }

But this isn't exactly what I need. Because it just returns the error msg, and the empty fields so the user has to fill out all fields again. For some reason the redirect works, but the render 'new' does not work.

You should render the 'new' action if the saving fails:

def create
  @project = Project.find(params[:project_id])
  @datum = @project.build(datum_params)

  respond_to do |format|
    if @datum.save
      format.html { redirect_to project_data_path, notice: 'Created.' }
    else
      format.html { render action: 'new' }
    end
  end
end

Also you should change your form to use the @datum instance variable instead building it in the form:

<%= bootstrap_form_for([@project, @datum]) do |f| %>

and the controller:

def new
  @project = Project.find(params[:project_id])
  @datum = @project.data.build
end

The basic idea is that if the user arrives to the new page you create a new empty datum instance variable. The user fills out the form and posts it to the create action. If the saving fails in the create action, you keep your filled-out @datum object and render that back to the form. The @datum object will have the errors ( you can check by @datum.errors ) which you can display to the user.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM