Question

This is a stupid example but I'm trying to understand how thing get passed around behind the scenes in Rails. And there's probably a better, "Rails", way to approach this...if so please let me know. I have:

Warren has_many Rabbits
Rabbit belongs_to Warren

I'm listing existing rabbits and then providing a form for adding a new rabbit.

warrens/show.html.haml

%h1 This Warren

%h2 Rabbits
= render @warren.rabbits, :target => @warren

%h2 Add a rabbit:
= render "rabbits/form", :parent => @warren, :foreign_key => :warren_id

= link_to 'Edit', edit_warren_path(@warren)
\|
= link_to 'Back', warrens_path

It works. I can add and destroy rabbits while looking at the Warren's show view. The problem is that the flash messages (:success, :notice) and/or the rabbit.errors aren't being shown on/in the view. Now, I am not using accepts_nested_attributes_for in my warren.rb model. but this is mostly working. Learning Rails so wondering what's going on and how to get my messages flowing smoothly...

rabbits/_form.html.haml

= form_for ([parent, parent.rabbits.build]) do |f| 
    = render "shared/error_messages", :target => parent

    .field
        = f.label :name
        = f.text_field :name
    .field
        = f.label :color
        = f.text_field :color
    .field
        = f.hidden_field foreign_key
    .actions
        = f.submit 'Save'

layouts/application.html.haml

    !!!
    %html
        %head
               ...
        %body
            .container
                - flash.each do |key, value|
                    %div{ :class => ['flash', key] }= value
                = yield

rabbits_controller.rb

class RabbitsController < ApplicationController

  def create
    @warren = Warren.find(params[:warren_id])
    @rabbit = @warren.rabbits.create(params[:rabbit])
    redirect_to @warren
  end

  def destroy
    @warren = Warren.find(params[:warren_id])
    @rabbit = @warren.rabbits.find(params[:id])
    @rabbit.destroy
    redirect_to @warren
  end

shared/_error_messages.html.haml

-if target.errors.any?
  #error_explanation
    %h2= "#{pluralize(target.errors.count, "error")} prohibited this record from being saved:"
    %ul
      - target.errors.full_messages.each do |msg|
        %li= msg
Was it helpful?

Solution

Seems that you are not setting the flash messages in your controller. Try the following:

def create
  @warren = Warren.find(params[:warren_id])
  @rabbit = @warren.rabbits.create(params[:rabbit])
  redirect_to @warren, :flash => { :info => "Rabbit was just born." }
end

def destroy
  @warren = Warren.find(params[:warren_id])
  @rabbit = @warren.rabbits.find(params[:id])
  @rabbit.destroy
  redirect_to @warren, :flash => { :info => "Rabbit was brutally murdered!" }
end

Alternatively, I believe you can use :success / notice:

redirect_to @warren, :success => "Rabbit was brutally murdered!"

Hope this helps!

UPDATE

For your error messages, you need something like target.error_messages. You can find more details on customizing the output here.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top