Reputation: 1415
Im using Simple Form gem.
When I try to update item with INVALID Data for instancing missing out some information like the Price. Rails is suppose to redirect to the edit view. As you can see by the controller.
Blow is the Update Method for the Items Controller
def update
@item = Item.find(params[:id])
if @item.update(item_params)
redirect_to @item
flash[:success] = 'Item was successfully updated.'
else
flash[:danger] = "Item didn't update"
render "edit"
end
end
However it doesn't. I just get this error.
Showing
/Users/josep/Documents/Safsy/Website/Safsy/Safsy/app/views/shared/_error_messages.html.erb where line #1 raised:
undefined method `errors' for nil:NilClass
<% if @user.errors.any? %>
<div id="error_explanation">
<div class="alert alert-danger">
The form contains <%= pluralize(@user.errors.count, "error") %>.
</div>
<ul>
Below is _error.messages.html.erb
<% if @user.errors.any? %>
<div id="error_explanation">
<div class="alert alert-danger">
The form contains <%= pluralize(@user.errors.count, "error") %>.
</div>
<ul>
<% @user.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
Partial is rendered inside my applications.html.erb
<!DOCTYPE html>
<html>
<head>
<%= stylesheet_link_tag 'application', media: 'all',
'data-turbolinks-track' => true %>
<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
<%= csrf_meta_tags %>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href='https://fonts.googleapis.com/css?family=Raleway:500,900,200' rel='stylesheet' type='text/css'>
<!--[if lt IE 9]>
<script src="//cdnjs.cloudflare.com/ajax/libs/html5shiv/r29/html5.min.js">
</script>
<![endif]-->
<title>Safsy - The Safest Way To Shop</title>
</head>
<body>
<%= render 'layouts/header' %>
<%= yield :sliderhome %>
<div class="container">
<% flash.each do |message_type, message| %>
<div class="alert alert-<%= message_type %>"><%= message %></div>
<% end %>
<%= yield %>
</div>
<%= render 'layouts/footer' %>
</body>
</html>
Views/Items/edit.html.erb
<div class="page-header">
<h1><%[email protected] %> <small> Edit</small></h1>
</div>
<div class="container">
<div class=“row”>
<div class="col-md-6 col-md-offset-3">
<div class="panel panel-primary">
<div class="panel-body">
<%= render 'shared/error_messages' %>
<%= simple_form_for @item do |f| %>
<%= f.input :image%>
<%= f.input :title%>
<%= f.input :price %>
<%= f.input :description %>
<%= f.button :submit, "Save changes", class: "btn btn-primary" %>
<% end %>
</div>
</div>
</div>
</div>
</div>
Thanks for any help in advance
Upvotes: 0
Views: 1227
Reputation: 17834
The error is coming because your error
partial has @user
object, but your form has @item
object, below is the solution
Make your error
partial work for every object, in your item form change the render 'shared/error_messages'
line to this
<%= render 'shared/error_messages', form_object: @item %>
and change your _error_messages.html.erb
to
<% if form_object.errors.any? %>
<div id="error_explanation">
<div class="alert alert-danger">
The form contains <%= pluralize(form_object.errors.count, "error") %>.
</div>
<ul>
<% form_object.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
So as you are using this partial in application
layout, pass the form object in the partial as I have done above! It will resolve your problem!
In this way, your error will be fixed and your code will dry up. Hope that helps!
Upvotes: 1