Home RoR Render errors partial if edit fails
Reply: 0

RoR Render errors partial if edit fails

user2381 Published in May 21, 2018, 12:40 pm

the problem I have is probably easy to solve, although I did a lot of search and cant find a solution.

I have in my _errors.html.erb

<% if obj.errors.any? %>
 <div class="row">
  <div class="col-md-8 col-md-offset-2 col-xs-12">
   <div class="panel panel-danger">
    <div class="panel-heading">
      <h2 class="panel-title">
        <%= pluralize(obj.errors.count, "error") %>
        prohibided this form from being saved:
      <div class="panel-body">
          <% obj.errors.full_messages.each do |msg| %>
           <li><%= msg %></li>
          <% end %>
<% end %>

Then I have in my edit.html.erb that has a form and:

<%= render 'layouts/errors', obj: @my_obj_here %>

Then in the controller update/create (lets use the update as example):

def update
    if @my_obj_here.update(params[:my_obj_here].permit(:body))
        redirect_to my_path_here_path(@my_obj_here), notice: "Something."
        render 'edit'

The issue happens when I try to update and the submit info is invalid, which will fall into the "render 'edit'"

The errors show correctly (in this case max length 100) but my url changes from: my_obj_here/1/edit to my_obj_here/1

which should not happen.

So next I tried replacing the "render 'edit'" with "redirect_to :back" but this just ignores the <%= render 'layouts/errors', obj: @my_obj_here %> in the edit.html.erb.

Can someone help me out figuring out how to render the same my_obj_here/1/edit? I believe I need to use the "render" my method because the redirect will just skip the errors partial.

Also, in my update method you probably noticed this "if true": redirect_to my_path_here_path(@my_obj_here), notice: "Something." I could the the same and just change my code to: redirect_to :back, :notice => "something." This would work, but does not show the errors as I want them to shown when my the errors partial is used.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.345622 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO