GrégoireC
GrégoireC

Reputation: 279

Can't render validations errors in form partial

I try to render validation errors in a form partial.

I have :

resas/_form.html.erb

<%= form_for ([@resa, Resa.new]) do |f| %>
<% if @resa.errors.any? %>
     <% @resa.errors.full_messages.each do |msg| %>
   <h3><%= msg %></h3>
  <% end %>
<% end %>
 ...
<div class="actions">
   <%= f.submit %>
</div>

I dislay this form in the index view, through another partial called _agenda :

resas/index.html.erb

<%= render "shared/agenda" %>


shared/_agenda.html.erb

<%= render "resas/form" %>

In the controller :

def create
    @resa = Resa.new(params[:resa])


    respond_to do |format|
      if @resa.save
        format.html { redirect_to resas_path, notice: 'Resa was successfully created.' }
        format.json { render json: @resa, status: :created, location: @resa }
      else
        format.html { redirect_to resas_path }
        format.json { render json: @resa.errors, status: :unprocessable_entity }
      end
    end
  end

I would like to redirect to the index action, and render validation errors, but I can't. I have :

NoMethodError in Resas#index

undefined method `errors' for nil:NilClass

how could I do ? Thanks for your help

Upvotes: 0

Views: 227

Answers (1)

userxyz
userxyz

Reputation: 1110

you can render to index action instead redirect in else part

Upvotes: 2

Related Questions