Reputation: 1855
I can't get error messages to display on this page. They display fine for any other page but not this one
mod_approval.index.html.erb
<% @check_category.each do |category| %>
<%= form_for([@guide, Guide.friendly.find(@guide.id).categories.new], url: guide_mod_panel_approve_category_path, method: :post) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<%= hidden_field_tag :check_id, category.id %>
<%= f.hidden_field :name, :value => category.name %>
<%= f.submit "Approve" %>
<% end %>
<% end %>
mod_approval_controller
def mod_add_category
@guide = Guide.friendly.find(params[:guide_id])
@check_category = CheckCategory.where(guide_id: @guide.id).all
@category = Guide.friendly.find(@guide.id).categories.new(category_params)
if @category.save
flash[:success] = "Game category added succesfully!"
redirect_to guide_mod_panel_mod_approval_index_path(@guide)
else
render 'mod_approval/index'
end
end
routes
match '/guides/:guide_id/mod-panel/approve/category' => 'mod_approval#mod_add_category', :via => :post, as: :guide_mod_panel_approve_category
match '/guides/:guide_id/mod-panel/approve' => 'mod_approval#index', :via => :get, as: :guide_mod_panel_mod_approval_index
Not too sure why they aren't rendering I tried changing <%= render 'shared/error_messages', object: f.object %>
to <%= render partial: 'shared/error_messages', object: f.object %>
but that gives the error
undefined local variable or method `object' for #<#<Class:0x007ffdbcd1c3a8>:0x007ffdbcbdd320>
on this line <% if object.errors.any? %>
This error rendering setup was made from Michael Hartls rails tutorial and as I said I works fine for every other form but this one.
Upvotes: 1
Views: 290
Reputation: 24551
The reason is that when you render the form, you instantiate a new Category:
form_for([@guide, Guide.friendly.find(@guide.id).categories.new],
You need to give form_for
the Category instance that has the errors (@category
from your controller). So I would change your form to this:
form_for([@guide, @category],
And then in your #new
method make sure you set it up:
@category = @guide.categories.build
Upvotes: 1