0

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

0

1 Answer 1

2

you can render to index action instead redirect in else part

Sign up to request clarification or add additional context in comments.

1 Comment

You will get the object errors in index view also. Its like same when we save object in create action and render to new if error comes

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.